lockspool.c revision 1.13
1/*	$OpenBSD: lockspool.c,v 1.13 2006/03/31 00:37:26 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
30static const char rcsid[] = "$OpenBSD: lockspool.c,v 1.13 2006/03/31 00:37:26 deraadt Exp $";
31#endif /* not lint */
32
33#include <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
42void unhold(int);
43void usage(void);
44
45extern char *__progname;
46
47int
48main(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/*ARGSUSED*/
97void
98unhold(int signo)
99{
100
101	rellock();
102	_exit(0);
103}
104
105void
106usage(void)
107{
108
109	merr(FATAL, "usage: %s [username]", __progname);
110}
111