|
Line 0
Link Here
|
|
|
1 |
/* |
| 2 |
checkdir.c |
| 3 |
Checks for the existance of a directory and creates it |
| 4 |
if necessary. It can also correct its ownership. |
| 5 |
|
| 6 |
Copyright 2007 Gentoo Foundation |
| 7 |
*/ |
| 8 |
|
| 9 |
#define APPLET "checkdir" |
| 10 |
|
| 11 |
#include <errno.h> |
| 12 |
#include <getopt.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <string.h> |
| 16 |
#include <unistd.h> |
| 17 |
#include <sys/stat.h> |
| 18 |
#include <sys/types.h> |
| 19 |
#include <pwd.h> |
| 20 |
#include <grp.h> |
| 21 |
|
| 22 |
#include "builtins.h" |
| 23 |
#include "einfo.h" |
| 24 |
|
| 25 |
#include "_usage.h" |
| 26 |
#define getoptstring "d:u:g:m:" getoptstring_COMMON |
| 27 |
static struct option longopts[] = { |
| 28 |
{ "user", 1, NULL, 'u'}, |
| 29 |
{ "group", 1, NULL, 'g'}, |
| 30 |
{ "mode", 1, NULL, 'm'}, |
| 31 |
longopts_COMMON |
| 32 |
{ NULL, 0, NULL, 0} |
| 33 |
}; |
| 34 |
#include "_usage.c" |
| 35 |
|
| 36 |
static char *progname; |
| 37 |
|
| 38 |
static int do_check(char *path, uid_t uid, gid_t gid) |
| 39 |
{ |
| 40 |
struct stat dirstat; |
| 41 |
|
| 42 |
/* create dir, if it doesn't exist */ |
| 43 |
if (mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH )) |
| 44 |
if (errno != EEXIST) |
| 45 |
eerrorx("%s: mkdir: %s", progname, strerror(errno)); |
| 46 |
else |
| 47 |
einfo("Created directory %s", path); |
| 48 |
|
| 49 |
if (stat(path, &dirstat)) |
| 50 |
eerrorx("%s: %s", progname, strerror(errno)); |
| 51 |
|
| 52 |
if (dirstat.st_uid != uid || dirstat.st_gid != gid) { |
| 53 |
einfo("Correcting directory ownership."); |
| 54 |
if (chown(path, uid, gid)) |
| 55 |
eerrorx("%s: chown: %s", progname, strerror(errno)); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
int checkdir (int argc, char **argv) |
| 60 |
{ |
| 61 |
int opt; |
| 62 |
char *user = NULL; |
| 63 |
char *group = NULL; |
| 64 |
uid_t uid = getuid(); |
| 65 |
gid_t gid = getgid(); |
| 66 |
|
| 67 |
progname = argv[0]; |
| 68 |
|
| 69 |
while ((opt = getopt_long (argc, argv, getoptstring, |
| 70 |
longopts, (int *) 0)) != -1) { |
| 71 |
switch (opt) { |
| 72 |
case 'u': |
| 73 |
user = strsep (&optarg, ":"); |
| 74 |
if (user[0] == '\0') |
| 75 |
user = NULL; |
| 76 |
group = optarg; |
| 77 |
break; |
| 78 |
|
| 79 |
case 'g': |
| 80 |
group = optarg; |
| 81 |
break; |
| 82 |
|
| 83 |
case_RC_COMMON_GETOPT |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
if (user != NULL) { |
| 88 |
struct passwd *pwd; |
| 89 |
char *endptr; |
| 90 |
|
| 91 |
uid = strtol(user, &endptr, 10); |
| 92 |
if (endptr[0] != '\0') { |
| 93 |
if ( (pwd = getpwnam(user)) == NULL) |
| 94 |
eerrorx("%s: Can't set user: %s", argv[0], |
| 95 |
user); |
| 96 |
uid = pwd->pw_uid; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if (group != NULL) { |
| 101 |
struct group *grp; |
| 102 |
char *endptr; |
| 103 |
|
| 104 |
gid = strtol(group, &endptr, 10); |
| 105 |
if (endptr[0] != '\0') { |
| 106 |
if ( (grp = getgrnam(group)) == NULL) |
| 107 |
eerrorx("%s: Can't set group: %s", argv[0], |
| 108 |
group); |
| 109 |
gid = grp->gr_gid; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
while (optind < argc) { |
| 114 |
do_check(argv[optind], uid, gid); |
| 115 |
optind++; |
| 116 |
} |
| 117 |
|
| 118 |
exit(EXIT_SUCCESS); |
| 119 |
} |