kernel/ath_hal: Add missing header to fix build with -Wundef.
[dragonfly.git] / libexec / dma / dma-mbox-create.c
blob66e702e7d7b62f5e09364ccc032d34908f94adb3
1 /*
2 * Copyright (c) 2010-2014, Simon Schubert <2@0x2c.org>.
3 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Simon Schubert <2@0x2c.org>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
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.
37 * This binary is setuid root. Use extreme caution when touching
38 * user-supplied information. Keep the root window as small as possible.
41 #include <sys/param.h>
42 #include <sys/stat.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <grp.h>
47 #include <paths.h>
48 #include <pwd.h>
49 #include <stdio.h>
50 #include <syslog.h>
51 #include <unistd.h>
53 #include "dma.h"
56 static void
57 logfail(const char *fmt, ...)
59 int oerrno = errno;
60 va_list ap;
61 char outs[1024];
63 outs[0] = 0;
64 if (fmt != NULL) {
65 va_start(ap, fmt);
66 vsnprintf(outs, sizeof(outs), fmt, ap);
67 va_end(ap);
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");
76 exit(1);
80 * Create a mbox in /var/mail for a given user, or make sure
81 * the permissions are correct for dma.
84 int
85 main(int argc, char **argv)
87 const char *user;
88 struct passwd *pw;
89 struct group *gr;
90 uid_t user_uid;
91 gid_t mail_gid;
92 int error;
93 char fn[PATH_MAX+1];
94 int f;
96 openlog("dma-mbox-create", 0, LOG_MAIL);
98 errno = 0;
99 gr = getgrnam(DMA_GROUP);
100 if (!gr)
101 logfail("cannot find dma group `%s'", DMA_GROUP);
103 mail_gid = gr->gr_gid;
105 if (setgid(mail_gid) != 0)
106 logfail("cannot set gid to %d (%s)", mail_gid, DMA_GROUP);
107 if (getegid() != mail_gid)
108 logfail("cannot set gid to %d (%s), still at %d", mail_gid, DMA_GROUP, getegid());
111 * We take exactly one argument: the username.
113 if (argc != 2) {
114 errno = 0;
115 logfail("no arguments");
117 user = argv[1];
119 syslog(LOG_NOTICE, "creating mbox for `%s'", user);
121 /* the username may not contain a pathname separator */
122 if (strchr(user, '/')) {
123 errno = 0;
124 logfail("path separator in username `%s'", user);
125 exit(1);
128 /* verify the user exists */
129 errno = 0;
130 pw = getpwnam(user);
131 if (!pw)
132 logfail("cannot find user `%s'", user);
134 user_uid = pw->pw_uid;
136 error = snprintf(fn, sizeof(fn), "%s/%s", _PATH_MAILDIR, user);
137 if (error < 0 || (size_t)error >= sizeof(fn)) {
138 if (error >= 0) {
139 errno = 0;
140 logfail("mbox path too long");
142 logfail("cannot build mbox path for `%s/%s'", _PATH_MAILDIR, user);
145 f = open(fn, O_RDONLY|O_CREAT|O_NOFOLLOW, 0600);
146 if (f < 0)
147 logfail("cannot open mbox `%s'", fn);
149 if (fchown(f, user_uid, mail_gid))
150 logfail("cannot change owner of mbox `%s'", fn);
152 if (fchmod(f, 0620))
153 logfail("cannot change permissions of mbox `%s'", fn);
155 /* file should be present with the right owner and permissions */
157 syslog(LOG_NOTICE, "successfully created mbox for `%s'", user);
159 return (0);