]>
Commit | Line | Data |
---|---|---|
c1ebccb4 | 1 | /* |
e458e5f0 | 2 | * Copyright (c) 2010-2014, Simon Schubert <2@0x2c.org>. |
c1ebccb4 SS |
3 | * Copyright (c) 2008 The DragonFly Project. All rights reserved. |
4 | * | |
5 | * This code is derived from software contributed to The DragonFly Project | |
e458e5f0 | 6 | * by Simon Schubert <2@0x2c.org>. |
c1ebccb4 SS |
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 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in | |
16 | * the documentation and/or other materials provided with the | |
17 | * distribution. | |
18 | * 3. Neither the name of The DragonFly Project nor the names of its | |
19 | * contributors may be used to endorse or promote products derived | |
20 | * from this software without specific, prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
23 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
25 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
26 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
27 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
29 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
30 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
31 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
32 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
33 | * SUCH DAMAGE. | |
34 | */ | |
35 | ||
36 | /* | |
37 | * This binary is setuid root. Use extreme caution when touching | |
38 | * user-supplied information. Keep the root window as small as possible. | |
39 | */ | |
40 | ||
41 | #include <sys/param.h> | |
42 | #include <sys/stat.h> | |
43 | ||
9ffd73f0 | 44 | #include <errno.h> |
c1ebccb4 SS |
45 | #include <fcntl.h> |
46 | #include <grp.h> | |
47 | #include <paths.h> | |
48 | #include <pwd.h> | |
49 | #include <stdio.h> | |
9ffd73f0 | 50 | #include <syslog.h> |
c1ebccb4 SS |
51 | #include <unistd.h> |
52 | ||
53 | #include "dma.h" | |
54 | ||
9ffd73f0 SS |
55 | |
56 | static void | |
de196e33 | 57 | logfail(int exitcode, const char *fmt, ...) |
9ffd73f0 SS |
58 | { |
59 | int oerrno = errno; | |
60 | va_list ap; | |
61 | char outs[1024]; | |
62 | ||
63 | outs[0] = 0; | |
64 | if (fmt != NULL) { | |
65 | va_start(ap, fmt); | |
66 | vsnprintf(outs, sizeof(outs), fmt, ap); | |
67 | va_end(ap); | |
68 | } | |
69 | ||
70 | errno = oerrno; | |
71 | if (*outs != 0) | |
72 | syslog(LOG_ERR, errno ? "%s: %m" : "%s", outs); | |
73 | else | |
74 | syslog(LOG_ERR, errno ? "%m" : "unknown error"); | |
75 | ||
de196e33 | 76 | exit(exitcode); |
9ffd73f0 SS |
77 | } |
78 | ||
c1ebccb4 SS |
79 | /* |
80 | * Create a mbox in /var/mail for a given user, or make sure | |
81 | * the permissions are correct for dma. | |
82 | */ | |
83 | ||
84 | int | |
85 | main(int argc, char **argv) | |
86 | { | |
87 | const char *user; | |
88 | struct passwd *pw; | |
89 | struct group *gr; | |
90 | uid_t user_uid; | |
91 | gid_t mail_gid; | |
6748bdd4 | 92 | int f, maildirfd; |
c1ebccb4 | 93 | |
9ffd73f0 SS |
94 | openlog("dma-mbox-create", 0, LOG_MAIL); |
95 | ||
96 | errno = 0; | |
97 | gr = getgrnam(DMA_GROUP); | |
98 | if (!gr) | |
de196e33 | 99 | logfail(EX_CONFIG, "cannot find dma group `%s'", DMA_GROUP); |
9ffd73f0 SS |
100 | |
101 | mail_gid = gr->gr_gid; | |
102 | ||
103 | if (setgid(mail_gid) != 0) | |
de196e33 | 104 | logfail(EX_NOPERM, "cannot set gid to %d (%s)", mail_gid, DMA_GROUP); |
9ffd73f0 | 105 | if (getegid() != mail_gid) |
de196e33 | 106 | logfail(EX_NOPERM, "cannot set gid to %d (%s), still at %d", mail_gid, DMA_GROUP, getegid()); |
9ffd73f0 | 107 | |
c1ebccb4 SS |
108 | /* |
109 | * We take exactly one argument: the username. | |
110 | */ | |
9ffd73f0 SS |
111 | if (argc != 2) { |
112 | errno = 0; | |
de196e33 | 113 | logfail(EX_USAGE, "no arguments"); |
9ffd73f0 | 114 | } |
c1ebccb4 SS |
115 | user = argv[1]; |
116 | ||
9ffd73f0 SS |
117 | syslog(LOG_NOTICE, "creating mbox for `%s'", user); |
118 | ||
c1ebccb4 | 119 | /* the username may not contain a pathname separator */ |
9ffd73f0 SS |
120 | if (strchr(user, '/')) { |
121 | errno = 0; | |
de196e33 | 122 | logfail(EX_DATAERR, "path separator in username `%s'", user); |
9ffd73f0 SS |
123 | exit(1); |
124 | } | |
c1ebccb4 SS |
125 | |
126 | /* verify the user exists */ | |
9ffd73f0 | 127 | errno = 0; |
c1ebccb4 SS |
128 | pw = getpwnam(user); |
129 | if (!pw) | |
de196e33 | 130 | logfail(EX_NOUSER, "cannot find user `%s'", user); |
c1ebccb4 | 131 | |
6748bdd4 EM |
132 | maildirfd = open(_PATH_MAILDIR, O_RDONLY); |
133 | if (maildirfd < 0) | |
134 | logfail(EX_NOINPUT, "cannot open maildir %s", _PATH_MAILDIR); | |
c1ebccb4 | 135 | |
6748bdd4 | 136 | user_uid = pw->pw_uid; |
c1ebccb4 | 137 | |
6748bdd4 | 138 | f = openat(maildirfd, user, O_RDONLY|O_CREAT|O_NOFOLLOW, 0600); |
c1ebccb4 | 139 | if (f < 0) |
6748bdd4 | 140 | logfail(EX_NOINPUT, "cannot open mbox `%s'", user); |
c1ebccb4 SS |
141 | |
142 | if (fchown(f, user_uid, mail_gid)) | |
6748bdd4 | 143 | logfail(EX_OSERR, "cannot change owner of mbox `%s'", user); |
c1ebccb4 | 144 | |
b7f06fda | 145 | if (fchmod(f, 0620)) |
6748bdd4 EM |
146 | logfail(EX_OSERR, "cannot change permissions of mbox `%s'", |
147 | user); | |
c1ebccb4 SS |
148 | |
149 | /* file should be present with the right owner and permissions */ | |
150 | ||
9ffd73f0 SS |
151 | syslog(LOG_NOTICE, "successfully created mbox for `%s'", user); |
152 | ||
c1ebccb4 SS |
153 | return (0); |
154 | } |