148665Sbostic /*-
2*62393Sbostic * Copyright (c) 1985, 1993
3*62393Sbostic * The Regents of the University of California. All rights reserved.
448665Sbostic *
548665Sbostic * %sccs.include.proprietary.c%
648665Sbostic */
748665Sbostic
813675Ssam #ifndef lint
9*62393Sbostic static char copyright[] =
10*62393Sbostic "@(#) Copyright (c) 1985, 1993\n\
11*62393Sbostic The Regents of the University of California. All rights reserved.\n";
1248665Sbostic #endif /* not lint */
1313675Ssam
1448665Sbostic #ifndef lint
15*62393Sbostic static char sccsid[] = "@(#)uuclean.c 8.1 (Berkeley) 06/06/93";
1648665Sbostic #endif /* not lint */
1748665Sbostic
1823678Sbloom #include <signal.h>
1913675Ssam #include "uucp.h"
2013675Ssam #include <pwd.h>
2113675Ssam #include <sys/stat.h>
2213675Ssam #ifdef NDIR
2313675Ssam #include "ndir.h"
2413675Ssam #else
2513702Ssam #include <sys/dir.h>
2613675Ssam #endif
2713675Ssam
2823678Sbloom /*
2913675Ssam *
3023678Sbloom * this program will search through the spool
3113675Ssam * directory (Spool) and delete all files with a requested
3213675Ssam * prefix which are older than (nomtime) seconds.
3313675Ssam * If the -m option is set, the program will try to
3413675Ssam * send mail to the usid of the file.
3513675Ssam *
3613675Ssam * options:
3713675Ssam * -m - send mail for deleted file
3813675Ssam * -d - directory to clean
3913675Ssam * -n - time to age files before delete (in hours)
4013675Ssam * -p - prefix for search
4113675Ssam * -x - turn on debug outputs
4213675Ssam * exit status:
4313675Ssam * 0 - normal return
4413675Ssam * 1 - can not read directory
4513675Ssam */
4613675Ssam
4713675Ssam #define NOMTIME 72 /* hours to age files before deletion */
4813675Ssam
4917840Sralph int checkprefix = 0;
5025139Sbloom struct timeb Now;
5117840Sralph
main(argc,argv)5213675Ssam main(argc, argv)
5313675Ssam char *argv[];
5413675Ssam {
5533573Srick register DIR *dirp;
5633573Srick register struct direct *dentp;
5713675Ssam time_t nomtime, ptime;
5813675Ssam struct stat stbuf;
5933573Srick int mflg = 0;
6013675Ssam
6113675Ssam strcpy(Progname, "uuclean");
6213675Ssam uucpname(Myname);
6313675Ssam nomtime = NOMTIME * (time_t)3600;
6413675Ssam
6513675Ssam while (argc>1 && argv[1][0] == '-') {
6613675Ssam switch (argv[1][1]) {
6713675Ssam case 'd':
6813675Ssam Spool = &argv[1][2];
6913675Ssam break;
7013675Ssam case 'm':
7113675Ssam mflg = 1;
7213675Ssam break;
7313675Ssam case 'n':
7413675Ssam nomtime = atoi(&argv[1][2]) * (time_t)3600;
7513675Ssam break;
7613675Ssam case 'p':
7717840Sralph checkprefix = 1;
7813675Ssam if (&argv[1][2] != '\0')
7913675Ssam stpre(&argv[1][2]);
8013675Ssam break;
8113675Ssam case 'x':
8217840Sralph chkdebug();
8313675Ssam Debug = atoi(&argv[1][2]);
8413675Ssam if (Debug <= 0)
8513675Ssam Debug = 1;
8613675Ssam break;
8713675Ssam default:
8813675Ssam printf("unknown flag %s\n", argv[1]); break;
8913675Ssam }
9013675Ssam --argc; argv++;
9113675Ssam }
9213675Ssam
9313675Ssam DEBUG(4, "DEBUG# %s\n", "START");
9417840Sralph if (chdir(Spool) < 0) { /* NO subdirs in uuclean! rti!trt */
9517840Sralph printf("%s directory inaccessible\n", Spool);
9617840Sralph exit(1);
9717840Sralph }
9813675Ssam
9913675Ssam if ((dirp = opendir(Spool)) == NULL) {
10013675Ssam printf("%s directory unreadable\n", Spool);
10113675Ssam exit(1);
10213675Ssam }
10313675Ssam
10413675Ssam time(&ptime);
10533573Srick while (dentp = readdir(dirp)) {
10633573Srick if (checkprefix && !chkpre(dentp->d_name))
10713675Ssam continue;
10813675Ssam
10933573Srick if (stat(dentp->d_name, &stbuf) == -1) {
11033573Srick DEBUG(4, "stat on %s failed\n", dentp->d_name);
11113675Ssam continue;
11213675Ssam }
11313675Ssam
11413675Ssam
11513675Ssam if ((stbuf.st_mode & S_IFMT) == S_IFDIR)
11613675Ssam continue;
11713675Ssam if ((ptime - stbuf.st_mtime) < nomtime)
11813675Ssam continue;
11933573Srick if (dentp->d_name[0] == CMDPRE)
12033573Srick notfyuser(dentp->d_name);
12133573Srick DEBUG(4, "unlink file %s\n", dentp->d_name);
12233573Srick unlink(dentp->d_name);
12317840Sralph if (mflg)
12433573Srick sdmail(dentp->d_name, stbuf.st_uid);
12513675Ssam }
12613675Ssam
12713675Ssam closedir(dirp);
12813675Ssam exit(0);
12913675Ssam }
13013675Ssam
13113675Ssam
13213675Ssam #define MAXPRE 10
13313675Ssam char Pre[MAXPRE][NAMESIZE];
13413675Ssam int Npre = 0;
13513675Ssam /***
13613675Ssam * chkpre(file) check for prefix
13713675Ssam * char *file;
13813675Ssam *
13913675Ssam * return codes:
14013675Ssam * 0 - not prefix
14113675Ssam * 1 - is prefix
14213675Ssam */
14313675Ssam
chkpre(file)14413675Ssam chkpre(file)
14513675Ssam char *file;
14613675Ssam {
14713675Ssam int i;
14813675Ssam
14913675Ssam for (i = 0; i < Npre; i++) {
15013675Ssam if (prefix(Pre[i], file))
15113675Ssam return(1);
15213675Ssam }
15313675Ssam return(0);
15413675Ssam }
15513675Ssam
15613675Ssam /***
15713675Ssam * stpre(p) store prefix
15813675Ssam * char *p;
15913675Ssam *
16013675Ssam * return codes: none
16113675Ssam */
16213675Ssam
stpre(p)16313675Ssam stpre(p)
16413675Ssam char *p;
16513675Ssam {
16613675Ssam if (Npre < MAXPRE - 2)
16713675Ssam strcpy(Pre[Npre++], p);
16813675Ssam return;
16913675Ssam }
17013675Ssam
17113675Ssam /***
17213675Ssam * notfyuser(file) - notfiy requestor of deleted requres
17313675Ssam *
17413675Ssam * return code - none
17513675Ssam */
17613675Ssam
notfyuser(file)17713675Ssam notfyuser(file)
17813675Ssam char *file;
17913675Ssam {
18013675Ssam FILE *fp;
18113675Ssam int numrq;
18213675Ssam char frqst[100], lrqst[100];
18313675Ssam char msg[BUFSIZ];
18413675Ssam char *args[10];
18513675Ssam
18613675Ssam if ((fp = fopen(file, "r")) == NULL)
18713675Ssam return;
18813675Ssam if (fgets(frqst, 100, fp) == NULL) {
18913675Ssam fclose(fp);
19013675Ssam return;
19113675Ssam }
19213675Ssam numrq = 1;
19313675Ssam while (fgets(lrqst, 100, fp))
19413675Ssam numrq++;
19513675Ssam fclose(fp);
19613675Ssam sprintf(msg,
19713675Ssam "File %s delete. \nCould not contact remote. \n%d requests deleted.\n", file, numrq);
19813675Ssam if (numrq == 1) {
19913675Ssam strcat(msg, "REQUEST: ");
20013675Ssam strcat(msg, frqst);
20117840Sralph } else {
20213675Ssam strcat(msg, "FIRST REQUEST: ");
20313675Ssam strcat(msg, frqst);
20413675Ssam strcat(msg, "\nLAST REQUEST: ");
20513675Ssam strcat(msg, lrqst);
20613675Ssam }
20717840Sralph getargs(frqst, args, 10);
20817840Sralph mailst(args[3], msg, CNULL);
20913675Ssam }
21013675Ssam
21113675Ssam
21213675Ssam /***
21313675Ssam * sdmail(file, uid)
21413675Ssam *
21513675Ssam * sdmail - this routine will determine the owner
21613675Ssam * of the file (file), create a message string and
21713675Ssam * call "mailst" to send the cleanup message.
21813675Ssam * This is only implemented for local system
21913675Ssam * mail at this time.
22013675Ssam */
22113675Ssam
sdmail(file,uid)22213675Ssam sdmail(file, uid)
22313675Ssam char *file;
22413675Ssam {
22513675Ssam static struct passwd *pwd;
22613675Ssam char mstr[40];
22713675Ssam
22813675Ssam sprintf(mstr, "uuclean deleted file %s\n", file);
22917840Sralph if (pwd != NULL && pwd->pw_uid == uid) {
23017840Sralph mailst(pwd->pw_name, mstr, CNULL);
23117840Sralph return;
23213675Ssam }
23313675Ssam
23413675Ssam setpwent();
23517840Sralph if ((pwd = getpwuid(uid)) != NULL)
23617840Sralph mailst(pwd->pw_name, mstr, CNULL);
23713675Ssam }
23813675Ssam
cleanup(code)23913675Ssam cleanup(code)
24013675Ssam int code;
24113675Ssam {
24213675Ssam exit(code);
24313675Ssam }
244