113675Ssam #ifndef lint 2*23678Sbloom static char sccsid[] = "@(#)uuclean.c 5.5 (Berkeley) 06/23/85"; 313675Ssam #endif 413675Ssam 5*23678Sbloom #include <signal.h> 613675Ssam #include "uucp.h" 713675Ssam #include <pwd.h> 813675Ssam #include <sys/stat.h> 913675Ssam #ifdef NDIR 1013675Ssam #include "ndir.h" 1113675Ssam #else 1213702Ssam #include <sys/dir.h> 1313675Ssam #endif 1413675Ssam 15*23678Sbloom /* 1613675Ssam * 17*23678Sbloom * this program will search through the spool 1813675Ssam * directory (Spool) and delete all files with a requested 1913675Ssam * prefix which are older than (nomtime) seconds. 2013675Ssam * If the -m option is set, the program will try to 2113675Ssam * send mail to the usid of the file. 2213675Ssam * 2313675Ssam * options: 2413675Ssam * -m - send mail for deleted file 2513675Ssam * -d - directory to clean 2613675Ssam * -n - time to age files before delete (in hours) 2713675Ssam * -p - prefix for search 2813675Ssam * -x - turn on debug outputs 2913675Ssam * exit status: 3013675Ssam * 0 - normal return 3113675Ssam * 1 - can not read directory 3213675Ssam */ 3313675Ssam 3413675Ssam #define NOMTIME 72 /* hours to age files before deletion */ 3513675Ssam 3617840Sralph int checkprefix = 0; 3717840Sralph 3813675Ssam main(argc, argv) 3913675Ssam char *argv[]; 4013675Ssam { 4113675Ssam DIR *dirp; 4213675Ssam char file[NAMESIZE]; 4313675Ssam time_t nomtime, ptime; 4413675Ssam struct stat stbuf; 4513675Ssam int mflg=0; 4613675Ssam 4713675Ssam strcpy(Progname, "uuclean"); 4813675Ssam uucpname(Myname); 4913675Ssam nomtime = NOMTIME * (time_t)3600; 5013675Ssam 5113675Ssam while (argc>1 && argv[1][0] == '-') { 5213675Ssam switch (argv[1][1]) { 5313675Ssam case 'd': 5413675Ssam Spool = &argv[1][2]; 5513675Ssam break; 5613675Ssam case 'm': 5713675Ssam mflg = 1; 5813675Ssam break; 5913675Ssam case 'n': 6013675Ssam nomtime = atoi(&argv[1][2]) * (time_t)3600; 6113675Ssam break; 6213675Ssam case 'p': 6317840Sralph checkprefix = 1; 6413675Ssam if (&argv[1][2] != '\0') 6513675Ssam stpre(&argv[1][2]); 6613675Ssam break; 6713675Ssam case 'x': 6817840Sralph chkdebug(); 6913675Ssam Debug = atoi(&argv[1][2]); 7013675Ssam if (Debug <= 0) 7113675Ssam Debug = 1; 7213675Ssam break; 7313675Ssam default: 7413675Ssam printf("unknown flag %s\n", argv[1]); break; 7513675Ssam } 7613675Ssam --argc; argv++; 7713675Ssam } 7813675Ssam 7913675Ssam DEBUG(4, "DEBUG# %s\n", "START"); 8017840Sralph if (chdir(Spool) < 0) { /* NO subdirs in uuclean! rti!trt */ 8117840Sralph printf("%s directory inaccessible\n", Spool); 8217840Sralph exit(1); 8317840Sralph } 8413675Ssam 8513675Ssam if ((dirp = opendir(Spool)) == NULL) { 8613675Ssam printf("%s directory unreadable\n", Spool); 8713675Ssam exit(1); 8813675Ssam } 8913675Ssam 9013675Ssam time(&ptime); 9113675Ssam while (gnamef(dirp, file)) { 9217840Sralph if (checkprefix && !chkpre(file)) 9313675Ssam continue; 9413675Ssam 9517840Sralph if (stat(file, &stbuf) == -1) { 9617840Sralph DEBUG(4, "stat on %s failed\n", file); 9713675Ssam continue; 9813675Ssam } 9913675Ssam 10013675Ssam 10113675Ssam if ((stbuf.st_mode & S_IFMT) == S_IFDIR) 10213675Ssam continue; 10313675Ssam if ((ptime - stbuf.st_mtime) < nomtime) 10413675Ssam continue; 10513675Ssam if (file[0] == CMDPRE) 10613675Ssam notfyuser(file); 10713675Ssam DEBUG(4, "unlink file %s\n", file); 10813675Ssam unlink(file); 10917840Sralph if (mflg) 11017840Sralph sdmail(file, stbuf.st_uid); 11113675Ssam } 11213675Ssam 11313675Ssam closedir(dirp); 11413675Ssam exit(0); 11513675Ssam } 11613675Ssam 11713675Ssam 11813675Ssam #define MAXPRE 10 11913675Ssam char Pre[MAXPRE][NAMESIZE]; 12013675Ssam int Npre = 0; 12113675Ssam /*** 12213675Ssam * chkpre(file) check for prefix 12313675Ssam * char *file; 12413675Ssam * 12513675Ssam * return codes: 12613675Ssam * 0 - not prefix 12713675Ssam * 1 - is prefix 12813675Ssam */ 12913675Ssam 13013675Ssam chkpre(file) 13113675Ssam char *file; 13213675Ssam { 13313675Ssam int i; 13413675Ssam 13513675Ssam for (i = 0; i < Npre; i++) { 13613675Ssam if (prefix(Pre[i], file)) 13713675Ssam return(1); 13813675Ssam } 13913675Ssam return(0); 14013675Ssam } 14113675Ssam 14213675Ssam /*** 14313675Ssam * stpre(p) store prefix 14413675Ssam * char *p; 14513675Ssam * 14613675Ssam * return codes: none 14713675Ssam */ 14813675Ssam 14913675Ssam stpre(p) 15013675Ssam char *p; 15113675Ssam { 15213675Ssam if (Npre < MAXPRE - 2) 15313675Ssam strcpy(Pre[Npre++], p); 15413675Ssam return; 15513675Ssam } 15613675Ssam 15713675Ssam /*** 15813675Ssam * notfyuser(file) - notfiy requestor of deleted requres 15913675Ssam * 16013675Ssam * return code - none 16113675Ssam */ 16213675Ssam 16313675Ssam notfyuser(file) 16413675Ssam char *file; 16513675Ssam { 16613675Ssam FILE *fp; 16713675Ssam int numrq; 16813675Ssam char frqst[100], lrqst[100]; 16913675Ssam char msg[BUFSIZ]; 17013675Ssam char *args[10]; 17113675Ssam 17213675Ssam if ((fp = fopen(file, "r")) == NULL) 17313675Ssam return; 17413675Ssam if (fgets(frqst, 100, fp) == NULL) { 17513675Ssam fclose(fp); 17613675Ssam return; 17713675Ssam } 17813675Ssam numrq = 1; 17913675Ssam while (fgets(lrqst, 100, fp)) 18013675Ssam numrq++; 18113675Ssam fclose(fp); 18213675Ssam sprintf(msg, 18313675Ssam "File %s delete. \nCould not contact remote. \n%d requests deleted.\n", file, numrq); 18413675Ssam if (numrq == 1) { 18513675Ssam strcat(msg, "REQUEST: "); 18613675Ssam strcat(msg, frqst); 18717840Sralph } else { 18813675Ssam strcat(msg, "FIRST REQUEST: "); 18913675Ssam strcat(msg, frqst); 19013675Ssam strcat(msg, "\nLAST REQUEST: "); 19113675Ssam strcat(msg, lrqst); 19213675Ssam } 19317840Sralph getargs(frqst, args, 10); 19417840Sralph mailst(args[3], msg, CNULL); 19513675Ssam } 19613675Ssam 19713675Ssam 19813675Ssam /*** 19913675Ssam * sdmail(file, uid) 20013675Ssam * 20113675Ssam * sdmail - this routine will determine the owner 20213675Ssam * of the file (file), create a message string and 20313675Ssam * call "mailst" to send the cleanup message. 20413675Ssam * This is only implemented for local system 20513675Ssam * mail at this time. 20613675Ssam */ 20713675Ssam 20813675Ssam sdmail(file, uid) 20913675Ssam char *file; 21013675Ssam { 21113675Ssam static struct passwd *pwd; 21213675Ssam struct passwd *getpwuid(); 21313675Ssam char mstr[40]; 21413675Ssam 21513675Ssam sprintf(mstr, "uuclean deleted file %s\n", file); 21617840Sralph if (pwd != NULL && pwd->pw_uid == uid) { 21717840Sralph mailst(pwd->pw_name, mstr, CNULL); 21817840Sralph return; 21913675Ssam } 22013675Ssam 22113675Ssam setpwent(); 22217840Sralph if ((pwd = getpwuid(uid)) != NULL) 22317840Sralph mailst(pwd->pw_name, mstr, CNULL); 22413675Ssam } 22513675Ssam 22613675Ssam cleanup(code) 22713675Ssam int code; 22813675Ssam { 22913675Ssam exit(code); 23013675Ssam } 231