lockspool.c revision 1.6
1/*	$OpenBSD: lockspool.c,v 1.6 2001/09/05 18:53:26 millert 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
32static const char rcsid[] = "$OpenBSD: lockspool.c,v 1.6 2001/09/05 18:53:26 millert 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
43void unhold __P((int));
44void usage __P((void));
45
46extern char *__progname;
47
48int
49main(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		merr(FATAL, "you must be root to lock someone else's spool");
63
64	signal(SIGTERM, unhold);
65	signal(SIGINT, unhold);
66	signal(SIGHUP, unhold);
67	signal(SIGPIPE, unhold);
68
69	if (argc == 2)
70		from = argv[1];
71	else
72		from = getlogin();
73
74	if (from) {
75		pw = getpwnam(from);
76		if (pw == NULL)
77			exit (1);
78	} else {
79		pw = getpwuid(getuid());
80		if (pw)
81			from = pw->pw_name;
82		else
83			exit (1);
84	}
85
86	holdfd = getlock(from, pw);
87	if (holdfd == -1) {
88		write(STDOUT_FILENO, "0\n", 2);
89		exit (1);
90	}
91	write(STDOUT_FILENO, "1\n", 2);
92
93	while (read(0, &c, 1) == -1 && errno == EINTR)
94		;
95	rellock();
96	exit (0);
97}
98
99void
100unhold(sig)
101	int sig;
102{
103
104	rellock();
105	_exit(0);
106}
107
108void
109usage()
110{
111
112	merr(FATAL, "usage: %s [username]", __progname);
113}
114