00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <stdarg.h>
00004 #include <regex.h>
00005 #include <sys/types.h>
00006 #include <sys/stat.h>
00007 #include <unistd.h>
00008 #include <pwd.h>
00009
00010 #include <dirent.h>
00011 #include <string.h>
00012
00013 #include "serverdefs.h"
00014
00015 #define PATTERN "/SUM[0-9]*/D[0-9]+$"
00016
00017 void die(const char *fmt, ...) {
00018 va_list ap;
00019 va_start(ap, fmt);
00020 vfprintf(stderr, fmt, ap);
00021 va_end(ap);
00022 exit(1);
00023 }
00024
00025 void recursive_chmown(const char *dir, uid_t owner, gid_t group) {
00026 DIR *d;
00027 struct dirent *entry;
00028 struct stat fs;
00029 char pname[1024];
00030
00031 if (!(d = opendir(dir)))
00032 die("recursive_chmown(): can't open directory %s\n", dir);
00033
00034 while (entry = readdir(d)) {
00035 if ((strcmp(".", entry->d_name) == 0) ||
00036 (strcmp("..", entry->d_name) == 0))
00037 continue;
00038
00039 if (snprintf(pname, 1024, "%s/%s", dir, entry->d_name) >= 1024)
00040 die("recursive_chmown(): pathname %s/%s too long", dir,
00041 entry->d_name);
00042
00043 if (lstat(pname, &fs) < 0)
00044 die("recursive_chmown(): can't lstat dirent %s\n", pname);
00045
00046 if (S_ISDIR(fs.st_mode)) {
00047 if (chown(pname, owner, group) < 0)
00048 die("recursive_chmown(): can't chown %s\n", pname);
00049 if (chmod(pname, 0755) < 0)
00050 die("recursive_chmown(): can't chmod %s\n", pname);
00051 recursive_chmown(pname, owner, group);
00052 } else if (S_ISREG(fs.st_mode)) {
00053 if (chown(pname, owner, group) < 0)
00054 die("recursive_chmown(): can't chown %s\n", pname);
00055 if(S_IXUSR & fs.st_mode) {
00056 if (chmod(pname, 0755) < 0)
00057 die("recursive_chmown(): can't chmod %s\n", pname);
00058 }
00059 else {
00060 if (chmod(pname, 0644) < 0)
00061 die("recursive_chmown(): can't chmod %s\n", pname);
00062 }
00063 }
00064 }
00065
00066 closedir(d);
00067 }
00068
00069 int main(int argc, char *argv[]) {
00070 regex_t reg;
00071 struct stat fs;
00072
00073
00074
00075
00076
00077
00078
00079 uid_t sumsowner = atoi(SUMS_MANAGER_UID);
00080
00081
00082
00083
00084
00085 if (argc != 2)
00086 die("Usage: %s <SUDIR>\n", argv[0]);
00087
00088 if (regcomp(®, PATTERN, REG_EXTENDED | REG_NOSUB))
00089 die("%s: bad regex %s\n", argv[0], PATTERN);
00090 if (regexec(®, argv[1], 0, 0, 0))
00091 die("%s: non SUDIR %s ignored\n", argv[0], argv[1]);
00092
00093 if (lstat(argv[1], &fs) < 0)
00094 die("%s: can't lstat directory %s\n", argv[0], argv[1]);
00095 if (!S_ISDIR(fs.st_mode))
00096 die("%s: %s is not a directory\n", argv[0], argv[1]);
00097
00098 if (chown(argv[1], sumsowner, -1) < 0)
00099 die("%s: can't chown %s\n", argv[0], argv[1]);
00100 if (chmod(argv[1], 0755) < 0)
00101 die("%s: can't chmod %s\n", argv[0], argv[1]);
00102 recursive_chmown(argv[1], sumsowner, -1);
00103
00104 return 0;
00105 }