masqmail

annotate src/permissions.c @ 10:26e34ae9a3e3

changed indention and line wrapping to a more consistent style
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:10 +0100
parents 08114f7dcc23
children f671821d8222
rev   line source
meillo@0 1 /* MasqMail
meillo@0 2 Copyright (C) 2000 Oliver Kurth
meillo@0 3
meillo@0 4 This program is free software; you can redistribute it and/or modify
meillo@0 5 it under the terms of the GNU General Public License as published by
meillo@0 6 the Free Software Foundation; either version 2 of the License, or
meillo@0 7 (at your option) any later version.
meillo@0 8
meillo@0 9 This program is distributed in the hope that it will be useful,
meillo@0 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@0 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@0 12 GNU General Public License for more details.
meillo@0 13
meillo@0 14 You should have received a copy of the GNU General Public License
meillo@0 15 along with this program; if not, write to the Free Software
meillo@0 16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 17 */
meillo@0 18
meillo@0 19 #include "masqmail.h"
meillo@0 20 #include <pwd.h>
meillo@0 21 #include <grp.h>
meillo@0 22
meillo@0 23 /* is there really no function in libc for this? */
meillo@10 24 gboolean
meillo@10 25 is_ingroup(uid_t uid, gid_t gid)
meillo@0 26 {
meillo@10 27 struct group *grent = getgrgid(gid);
meillo@0 28
meillo@10 29 if (grent) {
meillo@10 30 struct passwd *pwent = getpwuid(uid);
meillo@10 31 if (pwent) {
meillo@10 32 char *entry;
meillo@10 33 int i = 0;
meillo@10 34 while ((entry = grent->gr_mem[i++])) {
meillo@10 35 if (strcmp(pwent->pw_name, entry) == 0)
meillo@10 36 return TRUE;
meillo@10 37 }
meillo@10 38 }
meillo@10 39 }
meillo@10 40 return FALSE;
meillo@0 41 }
meillo@0 42
meillo@10 43 gboolean
meillo@10 44 is_privileged_user(uid_t uid)
meillo@0 45 {
meillo@10 46 return (uid == 0) || (uid == conf.mail_uid) || (is_ingroup(uid, conf.mail_gid));
meillo@0 47 }
meillo@0 48
meillo@10 49 void
meillo@10 50 set_euidgid(gint uid, gint gid, uid_t * old_uid, gid_t * old_gid)
meillo@0 51 {
meillo@10 52 if (old_uid)
meillo@10 53 *old_uid = geteuid();
meillo@10 54 if (old_gid)
meillo@10 55 *old_gid = getegid();
meillo@0 56
meillo@10 57 seteuid(0);
meillo@0 58
meillo@10 59 if (setegid(gid) != 0) {
meillo@10 60 logwrite(LOG_ALERT, "could not change gid to %d: %s\n", gid, strerror(errno));
meillo@10 61 exit(EXIT_FAILURE);
meillo@10 62 }
meillo@10 63 if (seteuid(uid) != 0) {
meillo@10 64 logwrite(LOG_ALERT, "could not change uid to %d: %s\n", uid, strerror(errno));
meillo@10 65 exit(EXIT_FAILURE);
meillo@10 66 }
meillo@0 67 }
meillo@0 68
meillo@10 69 void
meillo@10 70 set_identity(uid_t old_uid, gchar * task_name)
meillo@0 71 {
meillo@10 72 if (!conf.run_as_user) {
meillo@10 73 if (!is_privileged_user(old_uid)) {
meillo@10 74 fprintf(stderr, "must be root, %s or in group %s for %s.\n", DEF_MAIL_USER, DEF_MAIL_GROUP, task_name);
meillo@10 75 exit(EXIT_FAILURE);
meillo@10 76 }
meillo@0 77
meillo@10 78 set_euidgid(conf.mail_uid, conf.mail_gid, NULL, NULL);
meillo@10 79 }
meillo@0 80 }