]> git.ipfire.org Git - people/ms/dma.git/blame - dma-mbox-create.c
make ppa: force lower version number
[people/ms/dma.git] / dma-mbox-create.c
CommitLineData
c1ebccb4
SS
1/*
2 * Copyright (c) 2010 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
6 * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
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
44#include <fcntl.h>
45#include <grp.h>
46#include <paths.h>
47#include <pwd.h>
48#include <stdio.h>
49#include <unistd.h>
50
51#include "dma.h"
52
53/*
54 * Create a mbox in /var/mail for a given user, or make sure
55 * the permissions are correct for dma.
56 */
57
58int
59main(int argc, char **argv)
60{
61 const char *user;
62 struct passwd *pw;
63 struct group *gr;
64 uid_t user_uid;
65 gid_t mail_gid;
66 int error;
67 char fn[PATH_MAX+1];
68 int f;
69
70 /*
71 * We take exactly one argument: the username.
72 */
73 if (argc != 2)
74 return (1);
75 user = argv[1];
76
77 /* the username may not contain a pathname separator */
78 if (strchr(user, '/'))
79 return (1);
80
81 /* verify the user exists */
82 pw = getpwnam(user);
83 if (!pw)
84 return (1);
85
86 user_uid = pw->pw_uid;
87
88 gr = getgrnam(DMA_GROUP);
89 if (!gr)
90 return (1);
91
92 mail_gid = gr->gr_gid;
93
94 if (setgid(mail_gid) != 0)
95 return (1);
96 if (getegid() != mail_gid)
97 return (1);
98
99 error = snprintf(fn, sizeof(fn), "%s/%s", _PATH_MAILDIR, user);
100 if (error < 0 || (size_t)error >= sizeof(fn))
101 return (1);
102
103 f = open(fn, O_RDONLY|O_CREAT, 0600);
104 if (f < 0)
105 return (1);
106
107 if (fchown(f, user_uid, mail_gid))
108 return (1);
109
110 if (fchmod(f, 0660))
111 return (1);
112
113 /* file should be present with the right owner and permissions */
114
115 return (0);
116}