/* Copyright 2000 by Mark Whitis */ /* http://www.freelabs.com/~whitis/ */ /* http://www.freelabs.com/~whitis/software/license.html */ #include #include #include #include #include #include #include /* * This program has no protection against symlink exploits * or bogus directory/file information that would cause * permissions to be changed on a file outside the * current directory tree. */ /* * read_ls_permissions() reads the first 10 characters of the * string pointed to by s and interprets them as a file mode * expressed in the manner of "ls -l". Any characters beyond * the first ten are ignored, so you don't need to parse the * permissions into a separate string. */ mode_t read_ls_permissions(char *s) { mode_t perm; assert(strlen(s)>=10); perm=0; if(s[1]=='r') perm |= S_IRUSR; if(s[2]=='w') perm |= S_IWUSR; if(s[3]=='x') perm |= S_IXUSR; if(s[3]=='s') perm |= S_IXUSR | S_ISUID; if(s[4]=='r') perm |= S_IRGRP; if(s[5]=='w') perm |= S_IWGRP; if(s[6]=='x') perm |= S_IXGRP; if(s[6]=='s') perm |= S_IXGRP | S_ISGID; if(s[7]=='r') perm |= S_IROTH; if(s[8]=='w') perm |= S_IWOTH; if(s[9]=='x') perm |= S_IXOTH; if(s[9]=='T') perm |= S_ISVTX; if(s[9]=='t') perm |= S_ISVTX | S_IXOTH; return(perm); } main() { FILE *infile; char line[256]; char top[256]; char dir[256]; char *p; size_t pos; mode_t perm; assert(getcwd(top, sizeof(top))); infile=fopen("ls-lR","r"); assert(infile); while(1) { fgets(line, sizeof(line), infile); line[sizeof(line)-1]=0; pos=strcspn(line, "\n\r"); line[pos]='\0'; printf("%s\n",line); if(strcspn(line," \n\r\t")==0) { printf(" blank line\n"); } else if( strncmp(line,"total",5)==0) { printf(" total line\n"); } else if( (strcspn(line,":")+1)==strlen(line)) { printf(" directory line\n"); dir[sizeof(dir)-1]=0; pos=strcspn(line,":"); line[pos]='\0'; strncpy(dir,top,sizeof(dir)); strncat(dir,"/",sizeof(dir)); assert(dir[sizeof(dir)-1]==0); strncat(dir,line,sizeof(dir)); assert(dir[sizeof(dir)-1]==0); printf(" dir=%s\n",dir); assert(chdir(dir)==0); } else { printf(" file line\n"); perm=read_ls_permissions(line); printf(" perm=%04o\n", perm); p=&line[55]; pos=strcspn(p, " /*@|=\t\n\r"); p[pos]=0; assert(strlen(p)>0); printf(" filename=%s\n",p); chmod(p, perm); } } } /*************** dead code chmod_cmd[0]=0; chmod_cmd[sizeof(chmod_cmd)-1)=0; strncpy(chmod_cmd, "chmod ", sizeof(chmod_cmd)); assert(chmod_cmd[sizeof(chmod_cmd)-1)==0); strncat(chmod_cmd, "u=", sizeof(chmod_cmd)); assert(chmod_cmd[sizeof(chmod_cmd)-1)==0); p=&line[2]; pos=strspn(line,"rwxs"); if(pos>3) pos=3; assert(strlen(chmod_cmd)+pos3) pos=3; assert(strlen(chmod_cmd)+pos3) pos=3; assert(strlen(chmod_cmd)+pos