masqmail

annotate src/permissions.c @ 186:3dff59a4e764

added two setup guides they surely need improvements please help to improve them
author meillo@marmaro.de
date Wed, 14 Jul 2010 23:26:20 +0200
parents ffeff2c33799
children 996b53a50f55
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 <pwd.h>
meillo@0 20 #include <grp.h>
meillo@0 21
meillo@15 22 #include "masqmail.h"
meillo@15 23
meillo@0 24 /* is there really no function in libc for this? */
meillo@10 25 gboolean
meillo@10 26 is_ingroup(uid_t uid, gid_t gid)
meillo@0 27 {
meillo@10 28 struct group *grent = getgrgid(gid);
meillo@84 29 struct passwd *pwent = getpwuid(uid);
meillo@84 30 char *entry;
meillo@84 31 int i = 0;
meillo@0 32
meillo@84 33 if (!grent) {
meillo@84 34 return FALSE;
meillo@84 35 }
meillo@84 36 if (!pwent) {
meillo@84 37 return FALSE;
meillo@84 38 }
meillo@84 39 /* check primary group */
meillo@84 40 if (pwent->pw_gid == gid) {
meillo@84 41 return TRUE;
meillo@84 42 }
meillo@84 43 /* check secondary groups */
meillo@84 44 while ((entry = grent->gr_mem[i++])) {
meillo@84 45 if (strcmp(pwent->pw_name, entry) == 0)
meillo@84 46 return TRUE;
meillo@10 47 }
meillo@10 48 return FALSE;
meillo@0 49 }
meillo@0 50
meillo@10 51 gboolean
meillo@10 52 is_privileged_user(uid_t uid)
meillo@0 53 {
meillo@87 54 /* uncomment these lines if you need the `uucp' group to be trusted too
meillo@87 55 struct group* grent = getgrnam("uucp");
meillo@87 56
meillo@87 57 if (is_ingroup(uid, grent->gr_gid)) {
meillo@87 58 return TRUE;
meillo@87 59 }
meillo@87 60 */
meillo@87 61
meillo@10 62 return (uid == 0) || (uid == conf.mail_uid) || (is_ingroup(uid, conf.mail_gid));
meillo@0 63 }
meillo@0 64
meillo@10 65 void
meillo@10 66 set_euidgid(gint uid, gint gid, uid_t * old_uid, gid_t * old_gid)
meillo@0 67 {
meillo@10 68 if (old_uid)
meillo@10 69 *old_uid = geteuid();
meillo@10 70 if (old_gid)
meillo@10 71 *old_gid = getegid();
meillo@0 72
meillo@10 73 seteuid(0);
meillo@0 74
meillo@10 75 if (setegid(gid) != 0) {
meillo@10 76 logwrite(LOG_ALERT, "could not change gid to %d: %s\n", gid, strerror(errno));
meillo@10 77 exit(EXIT_FAILURE);
meillo@10 78 }
meillo@10 79 if (seteuid(uid) != 0) {
meillo@10 80 logwrite(LOG_ALERT, "could not change uid to %d: %s\n", uid, strerror(errno));
meillo@10 81 exit(EXIT_FAILURE);
meillo@10 82 }
meillo@0 83 }
meillo@0 84
meillo@10 85 void
meillo@10 86 set_identity(uid_t old_uid, gchar * task_name)
meillo@0 87 {
meillo@10 88 if (!conf.run_as_user) {
meillo@10 89 if (!is_privileged_user(old_uid)) {
meillo@10 90 fprintf(stderr, "must be root, %s or in group %s for %s.\n", DEF_MAIL_USER, DEF_MAIL_GROUP, task_name);
meillo@10 91 exit(EXIT_FAILURE);
meillo@10 92 }
meillo@0 93
meillo@10 94 set_euidgid(conf.mail_uid, conf.mail_gid, NULL, NULL);
meillo@10 95 }
meillo@0 96 }