Deleted Added
full compact
dma-mbox-create.c (262282) dma-mbox-create.c (289123)
1/*
1/*
2 * Copyright (c) 2010 Simon Schubert <2@0x2c.org>
2 * Copyright (c) 2010-2014, Simon Schubert <2@0x2c.org>.
3 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 *
5 * This code is derived from software contributed to The DragonFly Project
3 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 *
5 * This code is derived from software contributed to The DragonFly Project
6 * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
6 * by Simon Schubert <2@0x2c.org>.
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

--- 34 unchanged lines hidden (view full) ---

49#include <stdio.h>
50#include <syslog.h>
51#include <unistd.h>
52
53#include "dma.h"
54
55
56static void
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

--- 34 unchanged lines hidden (view full) ---

49#include <stdio.h>
50#include <syslog.h>
51#include <unistd.h>
52
53#include "dma.h"
54
55
56static void
57logfail(const char *fmt, ...)
57logfail(int exitcode, const char *fmt, ...)
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
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
76 exit(1);
76 exit(exitcode);
77}
78
79/*
80 * Create a mbox in /var/mail for a given user, or make sure
81 * the permissions are correct for dma.
82 */
83
84int

--- 8 unchanged lines hidden (view full) ---

93 char fn[PATH_MAX+1];
94 int f;
95
96 openlog("dma-mbox-create", 0, LOG_MAIL);
97
98 errno = 0;
99 gr = getgrnam(DMA_GROUP);
100 if (!gr)
77}
78
79/*
80 * Create a mbox in /var/mail for a given user, or make sure
81 * the permissions are correct for dma.
82 */
83
84int

--- 8 unchanged lines hidden (view full) ---

93 char fn[PATH_MAX+1];
94 int f;
95
96 openlog("dma-mbox-create", 0, LOG_MAIL);
97
98 errno = 0;
99 gr = getgrnam(DMA_GROUP);
100 if (!gr)
101 logfail("cannot find dma group `%s'", DMA_GROUP);
101 logfail(EX_CONFIG, "cannot find dma group `%s'", DMA_GROUP);
102
103 mail_gid = gr->gr_gid;
104
105 if (setgid(mail_gid) != 0)
102
103 mail_gid = gr->gr_gid;
104
105 if (setgid(mail_gid) != 0)
106 logfail("cannot set gid to %d (%s)", mail_gid, DMA_GROUP);
106 logfail(EX_NOPERM, "cannot set gid to %d (%s)", mail_gid, DMA_GROUP);
107 if (getegid() != mail_gid)
107 if (getegid() != mail_gid)
108 logfail("cannot set gid to %d (%s), still at %d", mail_gid, DMA_GROUP, getegid());
108 logfail(EX_NOPERM, "cannot set gid to %d (%s), still at %d", mail_gid, DMA_GROUP, getegid());
109
110 /*
111 * We take exactly one argument: the username.
112 */
113 if (argc != 2) {
114 errno = 0;
109
110 /*
111 * We take exactly one argument: the username.
112 */
113 if (argc != 2) {
114 errno = 0;
115 logfail("no arguments");
115 logfail(EX_USAGE, "no arguments");
116 }
117 user = argv[1];
118
119 syslog(LOG_NOTICE, "creating mbox for `%s'", user);
120
121 /* the username may not contain a pathname separator */
122 if (strchr(user, '/')) {
123 errno = 0;
116 }
117 user = argv[1];
118
119 syslog(LOG_NOTICE, "creating mbox for `%s'", user);
120
121 /* the username may not contain a pathname separator */
122 if (strchr(user, '/')) {
123 errno = 0;
124 logfail("path separator in username `%s'", user);
124 logfail(EX_DATAERR, "path separator in username `%s'", user);
125 exit(1);
126 }
127
128 /* verify the user exists */
129 errno = 0;
130 pw = getpwnam(user);
131 if (!pw)
125 exit(1);
126 }
127
128 /* verify the user exists */
129 errno = 0;
130 pw = getpwnam(user);
131 if (!pw)
132 logfail("cannot find user `%s'", user);
132 logfail(EX_NOUSER, "cannot find user `%s'", user);
133
134 user_uid = pw->pw_uid;
135
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;
133
134 user_uid = pw->pw_uid;
135
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");
140 logfail(EX_USAGE, "mbox path too long");
141 }
141 }
142 logfail("cannot build mbox path for `%s/%s'", _PATH_MAILDIR, user);
142 logfail(EX_CANTCREAT, "cannot build mbox path for `%s/%s'", _PATH_MAILDIR, user);
143 }
144
145 f = open(fn, O_RDONLY|O_CREAT, 0600);
146 if (f < 0)
143 }
144
145 f = open(fn, O_RDONLY|O_CREAT, 0600);
146 if (f < 0)
147 logfail("cannot open mbox `%s'", fn);
147 logfail(EX_NOINPUT, "cannt open mbox `%s'", fn);
148
149 if (fchown(f, user_uid, mail_gid))
148
149 if (fchown(f, user_uid, mail_gid))
150 logfail("cannot change owner of mbox `%s'", fn);
150 logfail(EX_OSERR, "cannot change owner of mbox `%s'", fn);
151
152 if (fchmod(f, 0620))
151
152 if (fchmod(f, 0620))
153 logfail("cannot change permissions of mbox `%s'", fn);
153 logfail(EX_OSERR, "cannot change permissions of mbox `%s'", fn);
154
155 /* file should be present with the right owner and permissions */
156
157 syslog(LOG_NOTICE, "successfully created mbox for `%s'", user);
158
159 return (0);
160}
154
155 /* file should be present with the right owner and permissions */
156
157 syslog(LOG_NOTICE, "successfully created mbox for `%s'", user);
158
159 return (0);
160}