X-Git-Url: https://git.distorted.org.uk/~mdw/become/blobdiff_plain/c4f2d992e4a0fc068281376d89ec38de56dc2f58..03f996bd8a0d6391518979cdab3dbe38cba0bf83:/src/userdb.c diff --git a/src/userdb.c b/src/userdb.c index bcefc85..77e1a3d 100644 --- a/src/userdb.c +++ b/src/userdb.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: userdb.c,v 1.1 1997/07/21 13:47:43 mdw Exp $ + * $Id: userdb.c,v 1.2 1997/08/04 10:24:26 mdw Exp $ * * User database management * * (c) 1997 EBI */ -/*----- Licencing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of `become' * @@ -22,14 +22,17 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with `become'; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * along with `become'; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /*----- Revision history --------------------------------------------------* * * $Log: userdb.c,v $ - * Revision 1.1 1997/07/21 13:47:43 mdw + * Revision 1.2 1997/08/04 10:24:26 mdw + * Sources placed under CVS control. + * + * Revision 1.1 1997/07/21 13:47:43 mdw * Initial revision * */ @@ -46,15 +49,21 @@ /* --- Unix headers --- */ +#include "config.h" + #include +#ifdef HAVE_YP +# include +# include +#endif + #include #include #include /* --- Local headers --- */ -#include "config.h" #include "sym.h" #include "userdb.h" #include "utils.h" @@ -486,137 +495,7 @@ void userdb_freeGroup(void *rec) free(gr); } -/*----- Higher-level functions --------------------------------------------*/ - -/* --- @userdb_local@ --- * - * - * Arguments: --- - * - * Returns: --- - * - * Use: Reads the local list of users into the maps. - */ - -void userdb_local(void) -{ - D( printf("adding local users...\n"); ) - - /* --- Fetch users first --- */ - - { - struct passwd *pw; - - setpwent(); - while ((pw = getpwent()) != 0) { - D( userdb__dumpUser(pw, stdout); ) - if (!userdb__byName(&userdb__users, pw->pw_name)) - userdb__addToMap(&userdb__users, pw->pw_name, pw->pw_uid, - userdb_copyUser(pw)); - } - endpwent(); - } - - /* --- Then fetch groups --- */ - - { - struct group *gr; - - setgrent(); - while ((gr = getgrent()) != 0) { - D( userdb__dumpGroup(gr, stdout); ) - if (!userdb__byName(&userdb__groups, gr->gr_name)) - userdb__addToMap(&userdb__groups, gr->gr_name, gr->gr_gid, - userdb_copyGroup(gr)); - } - endgrent(); - } -} - -/* --- @userdb__getLine@ --- * - * - * Arguments: @char *buff@ = pointer to buffer to read into - * @size_t sz@ = size of the buffer - * @FILE *fp@ = pointer to stream to read on - * - * Returns: Zero if something didn't work. - * - * Use: Reads a line into the buffer. If the line didn't fit, bits - * of it are thrown away. The newline character is not - * included. - */ - -static char *userdb__getLine(char *buff, size_t sz, FILE *fp) -{ - if ((buff = fgets(buff, sz, fp)) == 0) - return (0); - sz = strlen(buff) - 1; - if (buff[sz] == '\n') - buff[sz] = 0; - else for (;;) { - int ch = getc(fp); - if (ch == '\n' || ch == EOF) - break; - } - return (buff); -} - -/* --- @userdb_yp@ --- * - * - * Arguments: --- - * - * Returns: --- - * - * Use: Fetches the YP database of users. - */ - -void userdb_yp(void) -{ - -#ifdef HAVE_YP - - char line[1024]; - FILE *fp; - - D( printf("adding nis users\n"); ) - - /* --- First, users --- */ - - if ((fp = popen("ypcat passwd", "r")) != 0) { - while (userdb__getLine(line, sizeof(line), fp)) { - struct passwd *pw; - - if ((pw = userdb__buildUser(line)) != 0) { - D( userdb__dumpUser(pw, stdout); ) - if (userdb__byName(&userdb__users, pw->pw_name)) - userdb_freeUser(pw); - else - userdb__addToMap(&userdb__users, pw->pw_name, pw->pw_uid, pw); - } - } - pclose(fp); - } - - /* --- Next, groups --- */ - - - if ((fp = popen("ypcat group", "r")) != 0) { - while (userdb__getLine(line, sizeof(line), fp)) { - struct group *gr; - - if ((gr = userdb__buildGroup(line)) != 0) { - D( userdb__dumpGroup(gr, stdout); ) - if (userdb__byName(&userdb__groups, gr->gr_name)) - userdb_freeGroup(gr); - else - userdb__addToMap(&userdb__groups, gr->gr_name, gr->gr_gid, gr); - } - } - pclose(fp); - } - -#endif - -} +/*----- Answering queries -------------------------------------------------*/ /* --- @userdb_userByName@, @userdb_userById@ --- * * @@ -716,6 +595,163 @@ struct group *userdb_nextGroup_r(userdb_iter *i) return (s ? s->rec : 0); } +/*----- Yellow pages support ----------------------------------------------*/ + +#ifdef HAVE_YP + +/* --- @userdb__foreachUser@ --- * + * + * Arguments: @int st@ = YP protocol-level status code + * @char *k@ = address of the key for this record + * @int ksz@ = size of the key + * @char *v@ = address of the value for this record + * @int vsz@ = size of the value + * @char *data@ = pointer to some data passed to me + * + * Returns: Zero to be called again, nonzero to end the enumeration. + * + * Use: Handles an incoming user record. + */ + +static int userdb__foreachUser(int st, char *k, int ksz, + char *v, int vsz, char *data) +{ + char *cv; + struct passwd *pw; + + if (st != YP_TRUE) + return (-1); + cv = xmalloc(vsz + 1); + memcpy(cv, v, vsz); + cv[vsz] = 0; + pw = userdb__buildUser(cv); + if (pw && !userdb__byName(&userdb__users, pw->pw_name)) + userdb__addToMap(&userdb__users, pw->pw_name, pw->pw_uid, pw); + free(cv); + return (0); +} + +/* --- @userdb__foreachGroup@ --- * + * + * Arguments: @int st@ = YP protocol-level status code + * @char *k@ = address of the key for this record + * @int ksz@ = size of the key + * @char *v@ = address of the value for this record + * @int vsz@ = size of the value + * @char *data@ = pointer to some data passed to me + * + * Returns: Zero to be called again, nonzero to end the enumeration. + * + * Use: Handles an incoming user record. + */ + +static int userdb__foreachGroup(int st, char *k, int ksz, + char *v, int vsz, char *data) +{ + char *cv; + struct group *gr; + + if (st != YP_TRUE) + return (-1); + cv = xmalloc(vsz + 1); + memcpy(cv, v, vsz); + cv[vsz] = 0; + gr = userdb__buildGroup(cv); + if (gr && !userdb__byName(&userdb__groups, gr->gr_name)) + userdb__addToMap(&userdb__groups, gr->gr_name, gr->gr_gid, gr); + free(cv); + return (0); +} + +/* --- @userdb_yp@ --- * + * + * Arguments: --- + * + * Returns: --- + * + * Use: Fetches the YP database of users. + */ + +void userdb_yp(void) +{ + char *ypdom; + + /* --- Bind to a server --- */ + + if (yp_get_default_domain(&ypdom) || + yp_bind(ypdom)) + return; + + /* --- Fetch the users map --- */ + + { + static struct ypall_callback ucb = { userdb__foreachUser, 0 }; + yp_all(ypdom, "passwd.byuid", &ucb); + } + + /* --- Fetch the groups map --- */ + + { + static struct ypall_callback gcb = { userdb__foreachGroup, 0 }; + yp_all(ypdom, "group.bygid", &gcb); + } + + yp_unbind(ypdom); + free(ypdom); +} + +#else + +void userdb_yp(void) { ; } + +#endif + +/*----- Building the databases --------------------------------------------*/ + +/* --- @userdb_local@ --- * + * + * Arguments: --- + * + * Returns: --- + * + * Use: Reads the local list of users into the maps. + */ + +void userdb_local(void) +{ + D( printf("adding local users...\n"); ) + + /* --- Fetch users first --- */ + + { + struct passwd *pw; + + setpwent(); + while ((pw = getpwent()) != 0) { + D( userdb__dumpUser(pw, stdout); ) + if (!userdb__byName(&userdb__users, pw->pw_name)) + userdb__addToMap(&userdb__users, pw->pw_name, pw->pw_uid, + userdb_copyUser(pw)); + } + endpwent(); + } + + /* --- Then fetch groups --- */ + + { + struct group *gr; + + setgrent(); + while ((gr = getgrent()) != 0) { + D( userdb__dumpGroup(gr, stdout); ) + if (!userdb__byName(&userdb__groups, gr->gr_name)) + userdb__addToMap(&userdb__groups, gr->gr_name, gr->gr_gid, + userdb_copyGroup(gr)); + } + endgrent(); + } +} + /* --- @userdb_init@ --- * * * Arguments: ---