Back to index

courier  0.68.2
Functions
changeuidgid.c File Reference
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <grp.h>
#include <pwd.h>
#include <errno.h>
#include "numlib.h"

Go to the source code of this file.

Functions

void libmail_changegroup (gid_t gid)
void libmail_changeuidgid (uid_t uid, gid_t gid)
void libmail_changeusername (const char *uname, const gid_t *forcegrp)

Function Documentation

void libmail_changegroup ( gid_t  gid)

Definition at line 23 of file changeuidgid.c.

{
       if ( setgid(gid))
       {
              perror("setgid");
              exit(1);
       }

#if HAVE_SETGROUPS
       if ( getuid() == 0 && setgroups(1, &gid) )
       {
              perror("setgroups");
              exit(1);
       }
#endif
}

Here is the caller graph for this function:

void libmail_changeuidgid ( uid_t  uid,
gid_t  gid 
)

Definition at line 40 of file changeuidgid.c.

{
       libmail_changegroup(gid);
       if ( setuid(uid))
       {
              perror("setuid");
              exit(1);
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void libmail_changeusername ( const char *  uname,
const gid_t *  forcegrp 
)

Definition at line 50 of file changeuidgid.c.

{
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);
       }
}

Here is the caller graph for this function: