2014-02-20 22:39:55 +00:00
|
|
|
/*
|
2015-10-10 23:22:05 +00:00
|
|
|
* Copyright (c) 2010-2014, Simon Schubert <2@0x2c.org>.
|
2014-02-20 22:39:55 +00:00
|
|
|
* Copyright (c) 2008 The DragonFly Project. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The DragonFly Project
|
2015-10-10 23:22:05 +00:00
|
|
|
* by Simon Schubert <2@0x2c.org>.
|
2014-02-20 22:39:55 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name of The DragonFly Project nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific, prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This binary is setuid root. Use extreme caution when touching
|
|
|
|
* user-supplied information. Keep the root window as small as possible.
|
|
|
|
*/
|
|
|
|
|
2016-12-16 01:48:55 +00:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#define USE_CAPSICUM 1
|
|
|
|
#endif
|
|
|
|
|
2014-02-20 22:39:55 +00:00
|
|
|
#include <sys/param.h>
|
2016-12-16 01:48:55 +00:00
|
|
|
#if USE_CAPSICUM
|
|
|
|
#include <sys/capsicum.h>
|
|
|
|
#endif
|
2014-02-20 22:39:55 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2016-12-16 01:48:55 +00:00
|
|
|
#include <capsicum_helpers.h>
|
|
|
|
#include <err.h>
|
2014-02-20 22:39:55 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "dma.h"
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2015-10-10 23:22:05 +00:00
|
|
|
logfail(int exitcode, const char *fmt, ...)
|
2014-02-20 22:39:55 +00:00
|
|
|
{
|
|
|
|
int oerrno = errno;
|
|
|
|
va_list ap;
|
|
|
|
char outs[1024];
|
|
|
|
|
|
|
|
outs[0] = 0;
|
|
|
|
if (fmt != NULL) {
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(outs, sizeof(outs), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = oerrno;
|
|
|
|
if (*outs != 0)
|
|
|
|
syslog(LOG_ERR, errno ? "%s: %m" : "%s", outs);
|
|
|
|
else
|
|
|
|
syslog(LOG_ERR, errno ? "%m" : "unknown error");
|
|
|
|
|
2015-10-10 23:22:05 +00:00
|
|
|
exit(exitcode);
|
2014-02-20 22:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a mbox in /var/mail for a given user, or make sure
|
|
|
|
* the permissions are correct for dma.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2016-12-16 01:48:55 +00:00
|
|
|
#if USE_CAPSICUM
|
|
|
|
cap_rights_t rights;
|
|
|
|
#endif
|
2014-02-20 22:39:55 +00:00
|
|
|
const char *user;
|
|
|
|
struct passwd *pw;
|
|
|
|
struct group *gr;
|
|
|
|
uid_t user_uid;
|
|
|
|
gid_t mail_gid;
|
2016-09-30 23:35:47 +00:00
|
|
|
int f, maildirfd;
|
2014-02-20 22:39:55 +00:00
|
|
|
|
2016-12-16 01:48:55 +00:00
|
|
|
/*
|
|
|
|
* Open log fd now for capability sandbox.
|
|
|
|
*/
|
|
|
|
openlog("dma-mbox-create", LOG_NDELAY, LOG_MAIL);
|
2014-02-20 22:39:55 +00:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
gr = getgrnam(DMA_GROUP);
|
|
|
|
if (!gr)
|
2015-10-10 23:22:05 +00:00
|
|
|
logfail(EX_CONFIG, "cannot find dma group `%s'", DMA_GROUP);
|
2014-02-20 22:39:55 +00:00
|
|
|
|
|
|
|
mail_gid = gr->gr_gid;
|
|
|
|
|
|
|
|
if (setgid(mail_gid) != 0)
|
2015-10-10 23:22:05 +00:00
|
|
|
logfail(EX_NOPERM, "cannot set gid to %d (%s)", mail_gid, DMA_GROUP);
|
2014-02-20 22:39:55 +00:00
|
|
|
if (getegid() != mail_gid)
|
2015-10-10 23:22:05 +00:00
|
|
|
logfail(EX_NOPERM, "cannot set gid to %d (%s), still at %d", mail_gid, DMA_GROUP, getegid());
|
2014-02-20 22:39:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We take exactly one argument: the username.
|
|
|
|
*/
|
|
|
|
if (argc != 2) {
|
|
|
|
errno = 0;
|
2015-10-10 23:22:05 +00:00
|
|
|
logfail(EX_USAGE, "no arguments");
|
2014-02-20 22:39:55 +00:00
|
|
|
}
|
|
|
|
user = argv[1];
|
|
|
|
|
|
|
|
syslog(LOG_NOTICE, "creating mbox for `%s'", user);
|
|
|
|
|
|
|
|
/* the username may not contain a pathname separator */
|
|
|
|
if (strchr(user, '/')) {
|
|
|
|
errno = 0;
|
2015-10-10 23:22:05 +00:00
|
|
|
logfail(EX_DATAERR, "path separator in username `%s'", user);
|
2014-02-20 22:39:55 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* verify the user exists */
|
|
|
|
errno = 0;
|
|
|
|
pw = getpwnam(user);
|
|
|
|
if (!pw)
|
2015-10-10 23:22:05 +00:00
|
|
|
logfail(EX_NOUSER, "cannot find user `%s'", user);
|
2014-02-20 22:39:55 +00:00
|
|
|
|
2016-09-30 23:35:47 +00:00
|
|
|
maildirfd = open(_PATH_MAILDIR, O_RDONLY);
|
|
|
|
if (maildirfd < 0)
|
|
|
|
logfail(EX_NOINPUT, "cannot open maildir %s", _PATH_MAILDIR);
|
2014-02-20 22:39:55 +00:00
|
|
|
|
2016-12-16 01:48:55 +00:00
|
|
|
/*
|
|
|
|
* Cache NLS data, for strerror, for err(3), before entering capability
|
|
|
|
* mode.
|
|
|
|
*/
|
|
|
|
caph_cache_catpages();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cache local time before entering Capsicum capability sandbox.
|
|
|
|
*/
|
|
|
|
caph_cache_tzdata();
|
|
|
|
|
|
|
|
#if USE_CAPSICUM
|
|
|
|
cap_rights_init(&rights, CAP_CREATE, CAP_FCHMOD, CAP_FCHOWN,
|
|
|
|
CAP_LOOKUP, CAP_READ);
|
|
|
|
if (cap_rights_limit(maildirfd, &rights) < 0 && errno != ENOSYS)
|
|
|
|
err(EX_OSERR, "can't limit maildirfd rights");
|
|
|
|
|
|
|
|
/* Enter Capsicum capability sandbox */
|
|
|
|
if (cap_enter() < 0 && errno != ENOSYS)
|
|
|
|
err(EX_OSERR, "cap_enter");
|
|
|
|
#endif
|
|
|
|
|
2016-09-30 23:35:47 +00:00
|
|
|
user_uid = pw->pw_uid;
|
2014-02-20 22:39:55 +00:00
|
|
|
|
2016-09-30 23:35:47 +00:00
|
|
|
f = openat(maildirfd, user, O_RDONLY|O_CREAT|O_NOFOLLOW, 0600);
|
2014-02-20 22:39:55 +00:00
|
|
|
if (f < 0)
|
2016-09-30 23:35:47 +00:00
|
|
|
logfail(EX_NOINPUT, "cannot open mbox `%s'", user);
|
2014-02-20 22:39:55 +00:00
|
|
|
|
|
|
|
if (fchown(f, user_uid, mail_gid))
|
2016-09-30 23:35:47 +00:00
|
|
|
logfail(EX_OSERR, "cannot change owner of mbox `%s'", user);
|
2014-02-20 22:39:55 +00:00
|
|
|
|
|
|
|
if (fchmod(f, 0620))
|
2016-09-30 23:35:47 +00:00
|
|
|
logfail(EX_OSERR, "cannot change permissions of mbox `%s'",
|
|
|
|
user);
|
2014-02-20 22:39:55 +00:00
|
|
|
|
|
|
|
/* file should be present with the right owner and permissions */
|
|
|
|
|
|
|
|
syslog(LOG_NOTICE, "successfully created mbox for `%s'", user);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|