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