xref: /openbsd/libexec/lockspool/lockspool.c (revision 133306f0)
1 /*	$OpenBSD: lockspool.c,v 1.3 2001/01/17 19:22:20 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 Theo de Raadt <deraadt@theos.com>
5  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the authors may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
22  * THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef lint
32 static char rcsid[] = "$OpenBSD: lockspool.c,v 1.3 2001/01/17 19:22:20 deraadt Exp $";
33 #endif /* not lint */
34 
35 #include <sys/signal.h>
36 #include <pwd.h>
37 #include <syslog.h>
38 #include <unistd.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include "mail.local.h"
42 
43 void unhold __P((int));
44 void usage __P((void));
45 
46 extern char *__progname;
47 
48 int
49 main(argc, argv)
50 	int argc;
51 	char **argv;
52 {
53 	struct passwd *pw;
54 	char *from, c;
55 	int holdfd;
56 
57 	openlog(__progname, LOG_PERROR, LOG_MAIL);
58 
59 	if (argc != 1 && argc != 2)
60 		usage();
61 	if (argc == 2 && getuid() != 0)
62 		err(1, "you must be root to lock someone else's spool");
63 
64 	signal(SIGTERM, unhold);
65 	signal(SIGINT, unhold);
66 	signal(SIGHUP, unhold);
67 
68 	if (argc == 2)
69 		from = argv[1];
70 	else
71 		from = getlogin();
72 
73 	if (from) {
74 		pw = getpwnam(from);
75 		if (pw == NULL)
76 			exit (1);
77 	} else {
78 		pw = getpwuid(getuid());
79 		if (pw)
80 			from = pw->pw_name;
81 		else
82 			exit (1);
83 	}
84 
85 	holdfd = getlock(from, pw);
86 	if (holdfd == -1) {
87 		write(STDOUT_FILENO, "0\n", 2);
88 		exit (1);
89 	}
90 	write(STDOUT_FILENO, "1\n", 2);
91 
92 	while (read(0, &c, 1) == -1 && errno == EINTR)
93 		;
94 	rellock();
95 	exit (0);
96 }
97 
98 void
99 unhold(sig)
100 	int sig;
101 {
102 
103 	rellock();
104 	_exit(0);
105 }
106 
107 void
108 usage()
109 {
110 
111 	err(FATAL, "usage: %s [username]", __progname);
112 }
113