1717 */
1818
1919#define _GNU_SOURCE
20- #include <sys/types.h>
2120#include <sys/stat.h>
21+ #include <sys/types.h>
2222
2323#include <errno.h>
2424#include <fcntl.h>
3131#include <string.h>
3232#include <unistd.h>
3333
34+ #include "_usage.h"
3435#include "einfo.h"
35- #include "rc.h"
3636#include "misc.h"
37+ #include "rc.h"
3738#include "selinux.h"
38- #include "_usage.h"
3939
4040typedef enum {
4141 inode_unknown = 0 ,
@@ -45,32 +45,20 @@ typedef enum {
4545} inode_t ;
4646
4747const char * applet = NULL ;
48- const char * extraopts = "path1 [path2] [...]" ;
48+ const char * extraopts = "path1 [path2] [...]" ;
4949const char getoptstring [] = "dDfFpm:o:sW" getoptstring_COMMON ;
50- const struct option longopts [] = {
51- { "directory" , 0 , NULL , 'd' },
52- { "directory-truncate" , 0 , NULL , 'D' },
53- { "file" , 0 , NULL , 'f' },
54- { "file-truncate" , 0 , NULL , 'F' },
55- { "pipe" , 0 , NULL , 'p' },
56- { "mode" , 1 , NULL , 'm' },
57- { "owner" , 1 , NULL , 'o' },
58- { "symlinks" , 0 , NULL , 's' },
59- { "writable" , 0 , NULL , 'W' },
60- longopts_COMMON
61- };
62- const char * const longopts_help [] = {
63- "Create a directory if not exists" ,
64- "Create/empty directory" ,
65- "Create a file if not exists" ,
66- "Truncate file" ,
67- "Create a named pipe (FIFO) if not exists" ,
68- "Mode to check" ,
69- "Owner to check (user:group)" ,
50+ const struct option longopts [] = {{"directory" , 0 , NULL , 'd' },
51+ {"directory-truncate" , 0 , NULL , 'D' }, {"file" , 0 , NULL , 'f' },
52+ {"file-truncate" , 0 , NULL , 'F' }, {"pipe" , 0 , NULL , 'p' },
53+ {"mode" , 1 , NULL , 'm' }, {"owner" , 1 , NULL , 'o' },
54+ {"symlinks" , 0 , NULL , 's' }, {"writable" , 0 , NULL , 'W' },
55+ longopts_COMMON };
56+ const char * const longopts_help [] = {"Create a directory if not exists" ,
57+ "Create/empty directory" , "Create a file if not exists" ,
58+ "Truncate file" , "Create a named pipe (FIFO) if not exists" ,
59+ "Mode to check" , "Owner to check (user:group)" ,
7060 "follow symbolic links (irrelevant on linux)" ,
71- "Check whether the path is writable or not" ,
72- longopts_help_COMMON
73- };
61+ "Check whether the path is writable or not" , longopts_help_COMMON };
7462const char * usagestring = NULL ;
7563
7664static int get_dirfd (char * path , bool symlinks )
@@ -91,8 +79,8 @@ static int get_dirfd(char *path, bool symlinks)
9179 eerrorx ("%s: empty or relative path" , applet );
9280 dirfd = openat (dirfd , "/" , O_RDONLY );
9381 if (dirfd == -1 )
94- eerrorx ("%s: unable to open the root directory: %s" ,
95- applet , strerror (errno ));
82+ eerrorx ("%s: unable to open the root directory: %s" , applet ,
83+ strerror (errno ));
9684 ch = path ;
9785 while (* ch ) {
9886 if (* ch == '/' )
@@ -111,22 +99,24 @@ static int get_dirfd(char *path, bool symlinks)
11199 str = xstrdup (linkpath ? linkpath : item );
112100 new_dirfd = openat (dirfd , str , flags );
113101 if (new_dirfd == -1 )
114- eerrorx ("%s: %s: could not open %s: %s" , applet , path , str ,
115- strerror (errno ));
102+ eerrorx ("%s: %s: could not open %s: %s" , applet , path ,
103+ str , strerror (errno ));
116104 if (fstat (new_dirfd , & st ) == -1 )
117- eerrorx ("%s: %s: unable to stat %s: %s" , applet , path , item ,
118- strerror (errno ));
119- if (S_ISLNK (st .st_mode ) ) {
105+ eerrorx ("%s: %s: unable to stat %s: %s" , applet , path ,
106+ item , strerror (errno ));
107+ if (S_ISLNK (st .st_mode )) {
120108 if (st .st_uid != 0 )
121109 eerrorx ("%s: %s: symbolic link %s not owned by root" ,
122- applet , path , str );
123- linksize = st .st_size + 1 ;
110+ applet , path , str );
111+ linksize = st .st_size + 1 ;
124112 if (linkpath )
125113 free (linkpath );
126114 linkpath = xmalloc (linksize );
127115 memset (linkpath , 0 , linksize );
128- if (readlinkat (new_dirfd , "" , linkpath , linksize ) != st .st_size )
129- eerrorx ("%s: symbolic link destination changed" , applet );
116+ if (readlinkat (new_dirfd , "" , linkpath , linksize ) !=
117+ st .st_size )
118+ eerrorx ("%s: symbolic link destination changed" ,
119+ applet );
130120 /*
131121 * now follow the symlink.
132122 */
@@ -160,7 +150,7 @@ static char *clean_path(char *path)
160150 * ch2 = * ch ;
161151 ch ++ ;
162152 ch2 ++ ;
163- if (!* (ch - 1 ))
153+ if (!* (ch - 1 ))
164154 break ;
165155 while (* (ch - 1 ) == '/' && * ch == '/' )
166156 ch ++ ;
@@ -169,16 +159,16 @@ static char *clean_path(char *path)
169159 while ((ch = strrchr (str , '/' ))) {
170160 if (ch == str )
171161 break ;
172- if (!* (ch + 1 ))
162+ if (!* (ch + 1 ))
173163 * ch = 0 ;
174164 else
175165 break ;
176166 }
177167 return str ;
178168}
179169
180- static int do_check (char * path , uid_t uid , gid_t gid , mode_t mode ,
181- inode_t type , bool trunc , bool chowner , bool symlinks , bool selinux_on )
170+ static int do_check (char * path , uid_t uid , gid_t gid , mode_t mode , inode_t type ,
171+ bool trunc , bool chowner , bool symlinks , bool selinux_on )
182172{
183173 struct stat st ;
184174 char * name = NULL ;
@@ -191,8 +181,8 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode,
191181 int u ;
192182
193183 memset (& st , 0 , sizeof (st ));
194- flags = O_CREAT | O_NDELAY | O_WRONLY | O_NOCTTY ;
195- readflags = O_NDELAY | O_NOCTTY | O_RDONLY ;
184+ flags = O_CREAT | O_NDELAY | O_WRONLY | O_NOCTTY ;
185+ readflags = O_NDELAY | O_NOCTTY | O_RDONLY ;
196186#ifdef O_CLOEXEC
197187 flags |= O_CLOEXEC ;
198188 readflags |= O_CLOEXEC ;
@@ -210,7 +200,8 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode,
210200 if (type == inode_file ) {
211201 einfo ("%s: creating file" , path );
212202 if (!mode ) /* 664 */
213- mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH ;
203+ mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
204+ S_IROTH ;
214205 u = umask (0 );
215206 fd = openat (dirfd , name , flags , mode );
216207 umask (u );
@@ -233,14 +224,14 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode,
233224 if (r == -1 && errno != EEXIST ) {
234225 free (name );
235226 eerror ("%s: mkdirat: %s" , applet ,
236- strerror (errno ));
227+ strerror (errno ));
237228 return -1 ;
238229 }
239230 readfd = openat (dirfd , name , readflags );
240231 if (readfd == -1 ) {
241232 free (name );
242- eerror ("%s: unable to open directory: %s" , applet ,
243- strerror (errno ));
233+ eerror ("%s: unable to open directory: %s" ,
234+ applet , strerror (errno ));
244235 return -1 ;
245236 }
246237 } else if (type == inode_fifo ) {
@@ -253,14 +244,14 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode,
253244 if (r == -1 && errno != EEXIST ) {
254245 free (name );
255246 eerror ("%s: mkfifo: %s" , applet ,
256- strerror (errno ));
247+ strerror (errno ));
257248 return -1 ;
258249 }
259250 readfd = openat (dirfd , name , readflags );
260251 if (readfd == -1 ) {
261252 free (name );
262253 eerror ("%s: unable to open fifo: %s" , applet ,
263- strerror (errno ));
254+ strerror (errno ));
264255 return -1 ;
265256 }
266257 }
@@ -287,37 +278,43 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode,
287278
288279 if (mode && (st .st_mode & 0777 ) != mode ) {
289280 if ((type != inode_dir ) && (st .st_nlink > 1 )) {
290- eerror ("%s: chmod: Too many hard links to %s" , applet , path );
281+ eerror ("%s: chmod: Too many hard links to %s" ,
282+ applet , path );
291283 close (readfd );
292284 return -1 ;
293285 }
294286 if (S_ISLNK (st .st_mode )) {
295- eerror ("%s: chmod: %s %s" , applet , path , " is a symbolic link" );
287+ eerror ("%s: chmod: %s %s" , applet , path ,
288+ " is a symbolic link" );
296289 close (readfd );
297290 return -1 ;
298291 }
299292 einfo ("%s: correcting mode" , path );
300293 if (fchmod (readfd , mode )) {
301- eerror ("%s: chmod: %s" , applet , strerror (errno ));
294+ eerror ("%s: chmod: %s" , applet ,
295+ strerror (errno ));
302296 close (readfd );
303297 return -1 ;
304298 }
305299 }
306300
307301 if (chowner && (st .st_uid != uid || st .st_gid != gid )) {
308302 if ((type != inode_dir ) && (st .st_nlink > 1 )) {
309- eerror ("%s: chown: %s %s" , applet , "Too many hard links to" , path );
303+ eerror ("%s: chown: %s %s" , applet ,
304+ "Too many hard links to" , path );
310305 close (readfd );
311306 return -1 ;
312307 }
313308 if (S_ISLNK (st .st_mode )) {
314- eerror ("%s: chown: %s %s" , applet , path , " is a symbolic link" );
309+ eerror ("%s: chown: %s %s" , applet , path ,
310+ " is a symbolic link" );
315311 close (readfd );
316312 return -1 ;
317313 }
318314 einfo ("%s: correcting owner" , path );
319315 if (fchown (readfd , uid , gid )) {
320- eerror ("%s: chown: %s" , applet , strerror (errno ));
316+ eerror ("%s: chown: %s" , applet ,
317+ strerror (errno ));
321318 close (readfd );
322319 return -1 ;
323320 }
@@ -334,11 +331,11 @@ static int do_check(char *path, uid_t uid, gid_t gid, mode_t mode,
334331 return 0 ;
335332}
336333
337- static int parse_owner (struct passwd * * user , struct group * * group ,
338- const char * owner )
334+ static int parse_owner (
335+ struct passwd * * user , struct group * * group , const char * owner )
339336{
340- char * u = xstrdup (owner );
341- char * g = strchr (u , ':' );
337+ char * u = xstrdup (owner );
338+ char * g = strchr (u , ':' );
342339 int id = 0 ;
343340 int retval = 0 ;
344341
@@ -385,9 +382,8 @@ int main(int argc, char **argv)
385382 char * path = NULL ;
386383
387384 applet = basename_c (argv [0 ]);
388- while ((opt = getopt_long (argc , argv , getoptstring ,
389- longopts , (int * ) 0 )) != -1 )
390- {
385+ while ((opt = getopt_long (
386+ argc , argv , getoptstring , longopts , (int * ) 0 )) != -1 ) {
391387 switch (opt ) {
392388 case 'D' :
393389 trunc = true;
@@ -406,14 +402,14 @@ int main(int argc, char **argv)
406402 break ;
407403 case 'm' :
408404 if (parse_mode (& mode , optarg ) != 0 )
409- eerrorx ("%s: invalid mode `%s'" ,
410- applet , optarg );
405+ eerrorx ("%s: invalid mode `%s'" , applet ,
406+ optarg );
411407 break ;
412408 case 'o' :
413409 chowner = true;
414410 if (parse_owner (& pw , & gr , optarg ) != 0 )
415- eerrorx ("%s: owner `%s' not found" ,
416- applet , optarg );
411+ eerrorx ("%s: owner `%s' not found" , applet ,
412+ optarg );
417413 break ;
418414 case 's' :
419415#ifndef O_PATH
@@ -424,15 +420,16 @@ int main(int argc, char **argv)
424420 writable = true;
425421 break ;
426422
427- case_RC_COMMON_GETOPT
423+ case_RC_COMMON_GETOPT
428424 }
429425 }
430426
431427 if (optind >= argc )
432428 usage (EXIT_FAILURE );
433429
434430 if (writable && type != inode_unknown )
435- eerrorx ("%s: -W cannot be specified along with -d, -f or -p" , applet );
431+ eerrorx ("%s: -W cannot be specified along with -d, -f or -p" ,
432+ applet );
436433
437434 if (pw ) {
438435 uid = pw -> pw_uid ;
@@ -449,7 +446,7 @@ int main(int argc, char **argv)
449446 if (writable )
450447 exit (!is_writable (path ));
451448 if (do_check (path , uid , gid , mode , type , trunc , chowner ,
452- symlinks , selinux_on ))
449+ symlinks , selinux_on ))
453450 retval = EXIT_FAILURE ;
454451 optind ++ ;
455452 free (path );
0 commit comments