1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
/*
** 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"
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);
}
}
|