home *** CD-ROM | disk | FTP | other *** search
- /* $Header: context.c,v 1.3 88/09/14 19:41:40 network Exp $
- *
- * User context manager.
- * This module exists for efficiency reasons; I could just call getpwnam()
- * every time I need context info.
- *
- * $Log: context.c,v $
- * Revision 1.3 88/09/14 19:41:40 network
- * Portability to System V and BSD.
- * General fixup.
- *
- * Revision 1.2 88/08/30 16:12:28 network
- * Use savestr() instead of strdup().
- *
- * Revision 1.1 88/06/06 09:38:05 chip
- * Initial revision
- *
- */
-
- #include "deliver.h"
- #include <pwd.h>
- #include <grp.h>
-
- extern struct passwd *getpwnam();
- extern struct passwd *getpwuid();
- extern struct group *getgrnam();
- extern struct group *getgrgid();
-
- /*
- * Local functions.
- */
-
- static CONTEXT *new_context();
-
- /*
- * Local data.
- */
-
- static CONTEXT *ctlist; /* Chain of CONTEXT structures. */
-
- /*----------------------------------------------------------------------
- * Look up a context by user name.
- */
-
- CONTEXT *
- name_context(name)
- char *name;
- {
- struct passwd *pw;
- CONTEXT *ct;
-
- for (ct = ctlist; ct; ct = ct->next)
- {
- if (strcmp(ct->name, name) == 0)
- return ct;
- }
-
- if ((pw = getpwnam(name)) == NULL)
- return NULL;
-
- return new_context(pw);
- }
-
- /*----------------------------------------------------------------------
- * Look up a context by user ID.
- */
-
- CONTEXT *
- uid_context(uid)
- int uid;
- {
- struct passwd *pw;
- CONTEXT *ct;
-
- for (ct = ctlist; ct; ct = ct->next)
- {
- if (ct->uid == uid)
- return ct;
- }
-
- if ((pw = getpwuid(uid)) == NULL)
- return NULL;
-
- return new_context(pw);
- }
-
- /*----------------------------------------------------------------------
- * Local function -- create a new context structure and return
- * its address.
- */
-
- static CONTEXT *
- new_context(pw)
- struct passwd *pw;
- {
- CONTEXT *ct;
-
- ct = (CONTEXT *) zalloc(sizeof(CONTEXT));
- ct->uid = pw->pw_uid;
- ct->gid = pw->pw_gid;
- ct->name = copystr(pw->pw_name);
- ct->home = copystr(pw->pw_dir);
-
- ct->next = ctlist;
- ctlist = ct;
-
- return ct;
- }
-
- /*----------------------------------------------------------------------
- * Report whether is is possible or not to enter the given context.
- */
-
- int
- ok_context(ct)
- CONTEXT *ct;
- {
- if (! ct)
- return FALSE;
-
- if (eff_uid == 0
- || ((real_uid == ct->uid) && (real_gid == ct->gid)))
- return TRUE;
- else
- return FALSE;
- }
-
- /*----------------------------------------------------------------------
- * Look up a group ID by name.
- */
-
- #ifdef MAILBOX_GROUP
-
- int
- group_id(name)
- char *name;
- {
- struct group *grp;
-
- if ((grp = getgrnam(name)) == NULL)
- return -1;
-
- return grp->gr_gid;
- }
-
- #endif /* MAILBOX_GROUP */
-