xref: /csrg-svn/libexec/bugfiler/bugfiler.c (revision 30157)
125388Sbloom /*
2*30157Sbostic  * Copyright (c) 1983, 1986 Regents of the University of California.
325388Sbloom  * All rights reserved.  The Berkeley software License Agreement
425388Sbloom  * specifies the terms and conditions for redistribution.
525388Sbloom  */
625388Sbloom 
714552Ssam #ifndef lint
825388Sbloom char copyright[] =
9*30157Sbostic "@(#) Copyright (c) 1983, 1986 Regents of the University of California.\n\
1025388Sbloom  All rights reserved.\n";
1125388Sbloom #endif not lint
1214552Ssam 
1325388Sbloom #ifndef lint
14*30157Sbostic static char sccsid[] = "@(#)bugfiler.c	5.6 (Berkeley) 86/11/25";
1525388Sbloom #endif not lint
1625388Sbloom 
1712375Sralph /*
18*30157Sbostic  * Bug report processing program, designed to be invoked
19*30157Sbostic  * through aliases(5).
2012375Sralph  */
21*30157Sbostic #include <bug.h>
22*30157Sbostic #include <sys/time.h>
2312375Sralph #include <stdio.h>
2414809Ssam #include <pwd.h>
2514809Ssam 
26*30157Sbostic extern char	*optarg;		/* getopt arguments */
27*30157Sbostic extern int	optind;
2812375Sralph 
29*30157Sbostic int	lfd;				/* lock file descriptor */
30*30157Sbostic short	do_redist = YES;		/* redistribut BR */
31*30157Sbostic char	bfr[MAXBSIZE],			/* general I/O buffer */
32*30157Sbostic 	tmpname[sizeof(TMP_BUG) + 5];	/* temp bug file */
3314809Ssam 
34*30157Sbostic main(argc,argv)
35*30157Sbostic int	argc;
36*30157Sbostic char	**argv;
3712375Sralph {
38*30157Sbostic 	register struct passwd	*pwd;	/* bugs password entry */
39*30157Sbostic 	register int	ch;		/* getopts char */
40*30157Sbostic 	register short	do_ack = YES;	/* acknowledge bug report */
41*30157Sbostic 	struct passwd	*getpwnam();
4212375Sralph 
43*30157Sbostic 	while ((ch = getopt(argc,argv,"ar")) != EOF)
44*30157Sbostic 		switch((char)ch) {
45*30157Sbostic 			case 'a':
46*30157Sbostic 				do_ack = NO;
4712375Sralph 				break;
48*30157Sbostic 			case 'r':
49*30157Sbostic 				do_redist = NO;
5012695Sralph 				break;
51*30157Sbostic 			case '?':
5212375Sralph 			default:
53*30157Sbostic 				error("usage: bugfiler [-ar] [maildir]",CHN);
5414809Ssam 		}
5512695Sralph 
56*30157Sbostic 	if (!(pwd = getpwnam(BUGS_ID)))
57*30157Sbostic 		error("bugs person %s is unknown",BUGS_ID);
5812375Sralph 
59*30157Sbostic 	argv += optind;
60*30157Sbostic 	if (*argv) {		/* change to argument directory */
61*30157Sbostic 		if (chdir(*argv))
62*30157Sbostic 			error("can't move to %s.",*argv);
63*30157Sbostic 	}			/* change to bugs home directory */
64*30157Sbostic 	else if (chdir(pwd->pw_dir))
65*30157Sbostic 		error("can't move to %s.",pwd->pw_dir);
6612695Sralph 
67*30157Sbostic 	if (setreuid(0,pwd->pw_uid))
68*30157Sbostic 		error("can't set id to %s.",BUGS_ID);
6912695Sralph 
70*30157Sbostic 	umask(2);		/* everything is 664 */
71*30157Sbostic 	seterr();
72*30157Sbostic 	logit();
73*30157Sbostic 	make_copy();
7412375Sralph 
75*30157Sbostic 	if (access(LOCK_FILE,R_OK) || (lfd = open(LOCK_FILE,O_RDONLY,0)) < 0)
76*30157Sbostic 		error("can't read lock file %s.",LOCK_FILE);
7712375Sralph 
78*30157Sbostic 	gethead();
79*30157Sbostic 	process();
8012375Sralph 
81*30157Sbostic 	if (setuid(0,0))
82*30157Sbostic 		error("can't set id to root.",CHN);
8312375Sralph 
84*30157Sbostic 	if (do_ack)
85*30157Sbostic 		reply();
86*30157Sbostic 	if (do_redist)
87*30157Sbostic 		redist();
8812695Sralph 
89*30157Sbostic 	unlink(tmpname);
90*30157Sbostic 	exit(OK);
9112375Sralph }
9212375Sralph 
9312375Sralph /*
94*30157Sbostic  * make_copy --
95*30157Sbostic  *	make a copy of the bug report
9612695Sralph  */
97*30157Sbostic static
98*30157Sbostic make_copy()
9912695Sralph {
100*30157Sbostic 	register int	cnt,			/* read return value */
101*30157Sbostic 			tfd;			/* temp file descriptor */
102*30157Sbostic 	char	*mktemp(), *strcpy();
10312695Sralph 
104*30157Sbostic 	/* use O_EXCL, since may not be able to get a lock file */
105*30157Sbostic 	for (cnt = 0;cnt < 20;++cnt)
106*30157Sbostic 		if ((tfd = open(mktemp(strcpy(tmpname,TMP_BUG)),O_WRONLY | O_CREAT | O_EXCL,0664)) >= 0) {
107*30157Sbostic 			while ((cnt = read(fileno(stdin),bfr,sizeof(bfr))) != ERR && cnt)
108*30157Sbostic 				write(tfd,bfr,cnt);
109*30157Sbostic 			close(tfd);
11028322Skarels 			return;
11128322Skarels 		}
112*30157Sbostic 	error("unable to make copy using %s.\n",tmpname);
11312375Sralph }
11412375Sralph 
11512375Sralph /*
116*30157Sbostic  * logit --
117*30157Sbostic  *	log this run of the bugfiler
11814809Ssam  */
119*30157Sbostic static
120*30157Sbostic logit()
12114809Ssam {
122*30157Sbostic 	struct timeval	tp;
123*30157Sbostic 	struct timezone	tzp;
124*30157Sbostic 	char	*ctime();
12514809Ssam 
126*30157Sbostic 	if (gettimeofday(&tp,&tzp))
127*30157Sbostic 		error("unable to get time of day.",CHN);
128*30157Sbostic 	fprintf(stderr,"\n>>> BUGFILER <<<\n\t%s",ctime(&tp.tv_sec));
12914809Ssam }
130