diff options
author | Thomas Schwinge <thomas@schwinge.name> | 2010-12-29 12:00:30 +0100 |
---|---|---|
committer | Ali Polatel <alip@exherbo.org> | 2011-11-05 01:12:35 +0200 |
commit | 38b245b46a51be82a5a5f798255cbbbbc0186f78 (patch) | |
tree | d4210e76396f3d45614f329f8288e10762fad407 /contrib/notmuch-deliver/maildrop/numlib/changeuidgid.c | |
parent | 5f53ce3e2b9704f4f2a4be92c91f82c697c15b9d (diff) |
Move files copied from maildrop to a separate hierarchy.
Signed-off-by: Thomas Schwinge <thomas@schwinge.name>
Diffstat (limited to 'contrib/notmuch-deliver/maildrop/numlib/changeuidgid.c')
-rw-r--r-- | contrib/notmuch-deliver/maildrop/numlib/changeuidgid.c | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/contrib/notmuch-deliver/maildrop/numlib/changeuidgid.c b/contrib/notmuch-deliver/maildrop/numlib/changeuidgid.c new file mode 100644 index 0000000..5679392 --- /dev/null +++ b/contrib/notmuch-deliver/maildrop/numlib/changeuidgid.c @@ -0,0 +1,111 @@ +/* +** Copyright 1998 - 2002 Double Precision, Inc. See COPYING for +** distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include <sys/types.h> +#if HAVE_UNISTD_H +#include <unistd.h> +#endif +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <grp.h> +#include <pwd.h> +#include <errno.h> + +#include "numlib.h" + +static const char rcsid[]="$Id: changeuidgid.c,v 1.2 2003/01/05 04:01:17 mrsam Exp $"; + +void libmail_changegroup(gid_t gid) +{ + if ( setgid(gid)) + { + perror("setgid"); + exit(1); + } + +#if HAVE_SETGROUPS + if ( getuid() == 0 && setgroups(1, &gid) ) + { + perror("setgroups"); + exit(1); + } +#endif +} + +void libmail_changeuidgid(uid_t uid, gid_t gid) +{ + libmail_changegroup(gid); + if ( setuid(uid)) + { + perror("setuid"); + exit(1); + } +} + +void libmail_changeusername(const char *uname, const gid_t *forcegrp) +{ +struct passwd *pw; +uid_t changeuid; +gid_t changegid; + +/* uname might be a pointer returned from a previous called to getpw(), +** and libc has a problem getting it back. +*/ +char *p=malloc(strlen(uname)+1); + + if (!p) + { + perror("malloc"); + exit(1); + } + strcpy(p, uname); + + errno=ENOENT; + if ((pw=getpwnam(p)) == 0) + { + free(p); + perror("getpwnam"); + exit(1); + } + free(p); + + changeuid=pw->pw_uid; + + if ( !forcegrp ) forcegrp= &pw->pw_gid; + + changegid= *forcegrp; + + if ( setgid( changegid )) + { + perror("setgid"); + exit(1); + } + +#if HAVE_INITGROUPS + if ( getuid() == 0 && initgroups(pw->pw_name, changegid) ) + { + perror("initgroups"); + exit(1); + } +#else +#if HAVE_SETGROUPS + if ( getuid() == 0 && setgroups(1, &changegid) ) + { + perror("setgroups"); + exit(1); + } +#endif +#endif + + if (setuid(changeuid)) + { + perror("setuid"); + exit(1); + } +} |