xref: /original-bsd/libexec/bugfiler/bugfiler.c (revision 562686c3)
1 /*
2  * Copyright (c) 1983, 1986, 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1983, 1986, 1987 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)bugfiler.c	5.18 (Berkeley) 05/17/93";
16 #endif /* not lint */
17 
18 /*
19  * Bug report processing program, designed to be invoked
20  * through aliases(5).
21  */
22 #include <sys/param.h>
23 #include <sys/time.h>
24 #include <sys/stat.h>
25 
26 #include <dirent.h>
27 #include <pwd.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 
33 #include "bug.h"
34 #include "extern.h"
35 
36 char	bfr[MAXBSIZE],			/* general I/O buffer */
37 	tmpname[sizeof(TMP_BUG) + 5];	/* temp bug file */
38 
39 static void logit __P((void));
40 static void make_copy __P((void));
41 
42 int
43 main(argc, argv)
44 	int	argc;
45 	char	*argv[];
46 {
47 	extern char	*optarg;	/* getopt arguments */
48 	register struct passwd	*pwd;	/* bugs password entry */
49 	register int	ch;		/* getopts char */
50 	int	do_ack,			/* acknowledge bug report */
51 		do_redist;		/* redistribut BR */
52 	char	*argversion;		/* folder name provided */
53 
54 	do_ack = do_redist = YES;
55 	argversion = NULL;
56 	while ((ch = getopt(argc, argv, "av:r")) != EOF)
57 		switch(ch) {
58 		case 'a':
59 			do_ack = NO;
60 			break;
61 		case 'v':
62 			argversion = optarg;
63 			break;
64 		case 'r':
65 			do_redist = NO;
66 			break;
67 		case '?':
68 		default:
69 			fputs("usage: bugfiler [-ar] [-v version]\n", stderr);
70 			error("usage: bugfiler [-ar] [-v version]", CHN);
71 		}
72 
73 	if (!(pwd = getpwnam(BUGS_ID)))
74 		error("can't find bugs login.", BUGS_ID);
75 
76 	if (chdir(pwd->pw_dir))		/* change to bugs home directory */
77 		error("can't chdir to %s.", pwd->pw_dir);
78 
79 	if (seteuid(pwd->pw_uid))
80 		error("can't set id to %s.", BUGS_ID);
81 
82 	(void)umask(02);		/* everything is 664 */
83 	seterr();			/* redirect to log file */
84 	logit();			/* log report arrival */
85 	make_copy();			/* save copy in case */
86 	gethead(do_redist);
87 
88 	if (argversion)			/* specific folder requested */
89 		(void)strcpy(dir, argversion);
90 
91 	process();
92 
93 	if (seteuid(0))
94 		error("can't set id to root.", CHN);
95 	if (do_ack)
96 		reply();
97 	if (do_redist)
98 		redist();
99 	(void)unlink(tmpname);
100 	exit(OK);
101 }
102 
103 /*
104  * make_copy --
105  *	make a copy of bug report in error folder
106  */
107 static void
108 make_copy()
109 {
110 	register int	cnt,			/* read return value */
111 			tfd;			/* temp file descriptor */
112 
113 	if (access(TMP_DIR, F_OK))
114 		(void)mkdir(TMP_DIR, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH);
115 	(void)strcpy(tmpname, TMP_BUG);
116 	if (tfd = mkstemp(tmpname)) {
117 		while ((cnt = read(fileno(stdin),
118 		    bfr, sizeof(bfr))) != ERR && cnt)
119 			write(tfd, bfr, cnt);
120 		(void)close(tfd);
121 		return;
122 	}
123 	error("can't make copy using %s.", tmpname);
124 }
125 
126 /*
127  * logit --
128  *	log this run of the bugfiler
129  */
130 static void
131 logit()
132 {
133 	struct timeval	tp;
134 	char	*C1, *C2;
135 
136 	if (gettimeofday(&tp, (struct timezone *)NULL))
137 		error("can't get time of day.", CHN);
138 	for (C1 = C2 = ctime(&tp.tv_sec); *C1 && *C1 != '\n'; ++C1);
139 	*C1 = EOS;
140 	fputs(C2, stderr);
141 }
142