1 /*	$OpenBSD: lockspool.c,v 1.11 2005/04/13 02:33:09 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  *
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
20  * THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef lint
30 static const char rcsid[] = "$OpenBSD: lockspool.c,v 1.11 2005/04/13 02:33:09 deraadt Exp $";
31 #endif /* not lint */
32 
33 #include <sys/signal.h>
34 #include <pwd.h>
35 #include <syslog.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include "mail.local.h"
41 
42 void unhold(int);
43 void usage(void);
44 
45 extern char *__progname;
46 
47 int
main(int argc,char * argv[])48 main(int argc, char *argv[])
49 {
50 	struct passwd *pw;
51 	char *from, c;
52 	int holdfd;
53 
54 	openlog(__progname, LOG_PERROR, LOG_MAIL);
55 
56 	if (argc != 1 && argc != 2)
57 		usage();
58 	if (argc == 2 && getuid() != 0)
59 		merr(FATAL, "you must be root to lock someone else's spool");
60 
61 	signal(SIGTERM, unhold);
62 	signal(SIGINT, unhold);
63 	signal(SIGHUP, unhold);
64 	signal(SIGPIPE, unhold);
65 
66 	if (argc == 2)
67 		from = argv[1];
68 	else
69 		from = getlogin();
70 
71 	if (from) {
72 		pw = getpwnam(from);
73 		if (pw == NULL)
74 			exit (1);
75 	} else {
76 		pw = getpwuid(getuid());
77 		if (pw)
78 			from = pw->pw_name;
79 		else
80 			exit (1);
81 	}
82 
83 	holdfd = getlock(from, pw);
84 	if (holdfd == -1) {
85 		write(STDOUT_FILENO, "0\n", 2);
86 		exit (1);
87 	}
88 	write(STDOUT_FILENO, "1\n", 2);
89 
90 	while (read(STDIN_FILENO, &c, 1) == -1 && errno == EINTR)
91 		;
92 	rellock();
93 	exit (0);
94 }
95 
96 void
unhold(int signo)97 unhold(int signo)
98 {
99 
100 	rellock();
101 	_exit(0);
102 }
103 
104 void
usage(void)105 usage(void)
106 {
107 
108 	merr(FATAL, "usage: %s [username]", __progname);
109 }
110