1 /* $OpenBSD: lockspool.c,v 1.16 2009/10/27 23:59:31 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 #include <signal.h> 30 #include <pwd.h> 31 #include <syslog.h> 32 #include <unistd.h> 33 #include <errno.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <poll.h> 37 #include "mail.local.h" 38 39 void unhold(int); 40 void usage(void); 41 42 extern char *__progname; 43 44 int 45 main(int argc, char *argv[]) 46 { 47 struct passwd *pw; 48 struct pollfd pfd; 49 ssize_t nread; 50 char *from, c; 51 int holdfd; 52 53 openlog(__progname, LOG_PERROR, LOG_MAIL); 54 55 if (argc != 1 && argc != 2) 56 usage(); 57 if (argc == 2 && getuid() != 0) 58 merr(FATAL, "you must be root to lock someone else's spool"); 59 60 signal(SIGTERM, unhold); 61 signal(SIGINT, unhold); 62 signal(SIGHUP, unhold); 63 signal(SIGPIPE, unhold); 64 65 if (argc == 2) 66 pw = getpwnam(argv[1]); 67 else 68 pw = getpwuid(getuid()); 69 if (pw == NULL) 70 exit (1); 71 from = pw->pw_name; 72 73 holdfd = getlock(from, pw); 74 if (holdfd == -1) { 75 write(STDOUT_FILENO, "0\n", 2); 76 exit (1); 77 } 78 write(STDOUT_FILENO, "1\n", 2); 79 80 /* wait for the other end of the pipe to close, then release the lock */ 81 pfd.fd = STDIN_FILENO; 82 pfd.events = POLLIN; 83 do { 84 if (poll(&pfd, 1, INFTIM) == -1) { 85 if (errno != EINTR) 86 break; 87 } 88 do { 89 nread = read(STDIN_FILENO, &c, 1); 90 } while (nread == 1 || (nread == -1 && errno == EINTR)); 91 } while (nread == -1 && errno == EAGAIN); 92 rellock(); 93 exit (0); 94 } 95 96 /*ARGSUSED*/ 97 void 98 unhold(int signo) 99 { 100 101 rellock(); 102 _exit(0); 103 } 104 105 void 106 usage(void) 107 { 108 109 merr(FATAL, "usage: %s [username]", __progname); 110 } 111