3 * $Id: userdb.c,v 1.5 1997/09/17 10:24:08 mdw Exp $
5 * User database management
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of `become'
14 * `Become' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `Become' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.5 1997/09/17 10:24:08 mdw
33 * Use `uid_t' instead of `int' for uids and gids. Not quite sure why I
34 * didn't do this before.
36 * Revision 1.4 1997/08/20 16:24:58 mdw
37 * Patch memory leak. Rename `userdb_reinit' to `userdb_end' for more
40 * Revision 1.3 1997/08/07 09:44:29 mdw
41 * Read NIS-based passwords from the YP server directly, rather than using
42 * `popen(ypcat)', which is probably both slower and less secure.
44 * Revision 1.2 1997/08/04 10:24:26 mdw
45 * Sources placed under CVS control.
47 * Revision 1.1 1997/07/21 13:47:43 mdw
52 /*----- Header files ------------------------------------------------------*/
54 /* --- ANSI headers --- */
62 /* --- Unix headers --- */
66 #include <sys/types.h>
70 # include <rpcsvc/ypclnt.h>
71 # include <rpcsvc/yp_prot.h>
78 /* --- Local headers --- */
85 /*----- Type definitions --------------------------------------------------*/
87 /* --- A map link --- */
89 typedef struct userdb__node {
90 struct userdb__node *next;
94 /* --- A reference to a real record --- */
96 typedef struct userdb__sym {
101 /* --- A name- and number-mapping --- */
103 typedef struct userdb__map {
109 /*----- Static variables --------------------------------------------------*/
111 static userdb__map userdb__users; /* Map of user info blocks */
112 static sym_iter userdb__useri; /* Iterator for users */
113 static userdb__map userdb__groups; /* Map of group info blocks */
114 static sym_iter userdb__groupi; /* Iterator for groups */
116 /*----- Map management functions ------------------------------------------*/
118 /* --- @userdb__createMap@ --- *
120 * Arguments: @userdb__map *m@ = pointer to a map block
124 * Use: Initialises a map table.
127 static void userdb__createMap(userdb__map *m)
129 sym_createTable(&m->nmap);
130 sym_createTable(&m->idmap);
134 /* --- @userdb__addToMap@ --- *
136 * Arguments: @userdb__map *m@ = pointer to the map block
137 * @const char *name@ = pointer to the item's name
138 * @uid_t id@ = the item's id number
139 * @void *rec@ = pointer to the actual record
143 * Use: Adds an item to the given map.
146 static void userdb__addToMap(userdb__map *m,
154 s = sym_find(&m->nmap, name, -1, sizeof(*s), &f);
158 s = sym_find(&m->idmap, (char *)&id, sizeof(id), sizeof(*s), &f);
162 n = xmalloc(sizeof(*n));
168 /* --- @userdb__byName@ --- *
170 * Arguments: @userdb__map *m@ = pointer to a map block
171 * @const char *name@ = name to look up
173 * Returns: A pointer to the appropriate block, or zero if not found.
175 * Use: Looks up a name in a mapping and returns the result.
178 static void *userdb__byName(userdb__map *m, const char *name)
180 userdb__sym *s = sym_find(&m->nmap, name, -1, 0, 0);
181 return (s ? s->rec : 0);
184 /* --- @userdb__byId@ --- *
186 * Arguments: @userdb__map *m@ = pointer to a map block
187 * @uid_t id@ = id number to find
189 * Returns: A pointer to the appropriate block, or zero if not found.
191 * Use: Looks up an ID in a mapping, and returns the result.
194 static void *userdb__byId(userdb__map *m, uid_t id)
196 userdb__sym *s = sym_find(&m->idmap, (char *)&id, sizeof(id), 0, 0);
197 return (s ? s->rec : 0);
200 /* --- @userdb__clearMap@ --- *
202 * Arguments: @userdb__map *m@ = pointer to a map block
203 * @void (*freerec)(void *rec)@ = pointer to a free-record proc
207 * Use: Clears a map, emptying it and releasing the memory it
211 static void userdb__clearMap(userdb__map *m, void (*freerec)(void *rec))
215 sym_destroyTable(&m->nmap);
216 sym_destroyTable(&m->idmap);
218 for (n = m->list; n; n = t) {
225 /*----- User and group block management -----------------------------------*/
227 /* --- @userdb__dumpUser@ --- *
229 * Arguments: @const struct passwd *pw@ = pointer to a user block
233 * Use: Writes a user's informationt to a stream.
238 static void userdb__dumpUser(const struct passwd *pw)
241 "debug: name `%s' passwd `%s' uid %i gid %i",
242 pw->pw_name, pw->pw_passwd, (int)pw->pw_uid, (int)pw->pw_gid);
244 "debug: ... gecos `%s' home `%s' shell `%s'",
245 pw->pw_gecos, pw->pw_dir, pw->pw_shell);
250 /* --- @userdb_copyUser@ --- *
252 * Arguments: @struct passwd *pw@ = pointer to block to copy
254 * Returns: Pointer to the copy.
256 * Use: Copies a user block. The copy is `deep' so all the strings
257 * are copied too. Free the copy with @userdb_freeUser@ when
258 * you don't want it any more.
261 struct passwd *userdb_copyUser(struct passwd *pw)
268 npw = xmalloc(sizeof(*npw));
270 npw->pw_name = xstrdup(pw->pw_name);
271 npw->pw_passwd = xstrdup(pw->pw_passwd);
272 npw->pw_uid = pw->pw_uid;
273 npw->pw_gid = pw->pw_gid;
274 npw->pw_gecos = xstrdup(pw->pw_gecos);
275 npw->pw_dir = xstrdup(pw->pw_dir);
276 npw->pw_shell = xstrdup(pw->pw_shell);
281 /* --- @userdb__buildUser@ --- *
283 * Arguments: @char *s@ = pointer to user string
285 * Returns: Pointer to a user block.
287 * Use: Converts a line from a user file into a password entry.
288 * Note that the string is corrupted by @strtok@ while it gets
292 static struct passwd *userdb__buildUser(char *s)
294 struct passwd *pw = xmalloc(sizeof(*pw));
296 s = strtok(s, ":"); if (!s) goto tidy_0; pw->pw_name = xstrdup(s);
297 s = strtok(0, ":"); if (!s) goto tidy_1; pw->pw_passwd = xstrdup(s);
298 s = strtok(0, ":"); if (!s) goto tidy_2; pw->pw_uid = (uid_t)atol(s);
299 s = strtok(0, ":"); if (!s) goto tidy_2; pw->pw_gid = (gid_t)atol(s);
300 s = strtok(0, ":"); if (!s) goto tidy_2; pw->pw_gecos = xstrdup(s);
301 s = strtok(0, ":"); if (!s) goto tidy_3; pw->pw_dir = xstrdup(s);
302 s = strtok(0, ":"); if (!s) goto tidy_4; pw->pw_shell = xstrdup(s);
305 /* --- Error handling --- */
321 /* --- @userdb_freeUser@ --- *
323 * Arguments: @void *rec@ = pointer to a user record
327 * Use: Frees a user record.
330 void userdb_freeUser(void *rec)
346 /* --- @userdb__dumpGroup@ --- *
348 * Arguments: @const struct group *gr@ = pointer to a group block
349 * @FILE *fp@ = pointer to stream to write on
353 * Use: Writes a group's information to a stream.
358 static void userdb__dumpGroup(const struct group *gr)
363 "debug: name `%s' passwd `%s' gid %i",
364 gr->gr_name, gr->gr_passwd, (int)gr->gr_gid);
365 for (p = gr->gr_mem; *p; p++)
366 trace(TRACE_DEBUG,"debug: ... `%s'", *p);
371 /* --- @userdb_copyGroup@ --- *
373 * Arguments: @struct group *gr@ = pointer to group block
375 * Returns: Pointer to copied block
377 * Use: Copies a group block. The copy is `deep' so all the strings
378 * are copied too. Free the copy with @userdb_freeGroup@ when
379 * you don't want it any more.
382 struct group *userdb_copyGroup(struct group *gr)
390 ngr = xmalloc(sizeof(*ngr));
392 ngr->gr_name = xstrdup(gr->gr_name);
393 ngr->gr_passwd = xstrdup(gr->gr_passwd);
394 ngr->gr_gid = gr->gr_gid;
396 for (max = 0; gr->gr_mem[max]; max++)
398 ngr->gr_mem = xmalloc((max + 1) * sizeof(char *));
399 for (i = 0; i < max; i++)
400 ngr->gr_mem[i] = xstrdup(gr->gr_mem[i]);
401 ngr->gr_mem[max] = 0;
406 /* --- @userdb__buildGroup@ --- *
408 * Arguments: @char *s@ = pointer to group line string
410 * Returns: Pointer to a group block
412 * Use: Parses an entry in the groups file. The string is garbled
413 * by @strtok@ as we go.
416 static struct group *userdb__buildGroup(char *s)
418 struct group *gr = xmalloc(sizeof(*gr));
422 /* --- Do the easy bits --- */
424 s = strtok(s, ":"); if (!s) goto tidy_0; gr->gr_name = xstrdup(s);
425 s = strtok(0, ":"); if (!s) goto tidy_1; gr->gr_passwd = xstrdup(s);
426 s = strtok(0, ":"); if (!s) goto tidy_2; gr->gr_gid = (gid_t)atol(s);
428 /* --- Find the start of the member list --- */
434 /* --- Count the number of members --- */
440 if ((p = strpbrk(p, ",")) == 0)
445 /* --- Allocate the block and fill it --- */
447 gr->gr_mem = xmalloc((i + 1) * sizeof(char *));
451 gr->gr_mem[i++] = xstrdup(s);
458 /* --- Various tidying-up things --- */
470 /* --- @userdb_freeGroup@ --- *
472 * Arguments: @void *rec@ = pointer to a group record
476 * Use: Frees a group record.
479 void userdb_freeGroup(void *rec)
490 for (p = gr->gr_mem; *p; p++)
496 /*----- Answering queries -------------------------------------------------*/
498 /* --- @userdb_userByName@, @userdb_userById@ --- *
500 * Arguments: @const char *name@ = pointer to user's name
501 * @uid_t id@ = user id to find
503 * Returns: Pointer to user block, or zero if not found.
505 * Use: Looks up a user by name or id.
508 struct passwd *userdb_userByName(const char *name)
509 { return (userdb__byName(&userdb__users, name)); }
511 struct passwd *userdb_userById(uid_t id)
512 { return (userdb__byId(&userdb__users, id)); }
514 /* --- @userdb_iterateUsers@, @userdb_iterateUsers_r@ --- *
516 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator object
520 * Use: Initialises an iteration for the user database.
523 void userdb_iterateUsers(void)
524 { userdb_iterateUsers_r(&userdb__useri); }
526 void userdb_iterateUsers_r(userdb_iter *i)
527 { sym_createIter(i, &userdb__users.nmap); }
529 /* --- @userdb_nextUser@, @userdb_nextUser_r@ --- *
531 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator oject
533 * Returns: Pointer to the next user block, or null.
535 * Use: Returns another user block.
538 struct passwd *userdb_nextUser(void)
539 { return (userdb_nextUser_r(&userdb__useri)); }
541 struct passwd *userdb_nextUser_r(userdb_iter *i)
543 userdb__sym *s = sym_next(i);
544 return (s ? s->rec : 0);
547 /* --- @userdb_groupByName@, @userdb_groupById@ --- *
549 * Arguments: @const char *name@ = pointer to group's name
550 * @gid_t id@ = group id to find
552 * Returns: Pointer to group block, or zero if not found.
554 * Use: Looks up a group by name or id.
557 struct group *userdb_groupByName(const char *name)
558 { return (userdb__byName(&userdb__groups, name)); }
560 struct group *userdb_groupById(gid_t id)
561 { return (userdb__byId(&userdb__groups, id)); }
563 /* --- @userdb_iterateGroups@, @userdb_iterateGroups_r@ --- *
565 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator object
569 * Use: Initialises an iteration for the group database.
572 void userdb_iterateGroups(void)
573 { userdb_iterateGroups_r(&userdb__groupi); }
575 void userdb_iterateGroups_r(userdb_iter *i)
576 { sym_createIter(i, &userdb__groups.nmap); }
578 /* --- @userdb_nextGroup@, @userdb_nextGroup_r@ --- *
580 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator oject
582 * Returns: Pointer to the next group block, or null.
584 * Use: Returns another group block.
587 struct group *userdb_nextGroup(void)
588 { return (userdb_nextGroup_r(&userdb__groupi)); }
590 struct group *userdb_nextGroup_r(userdb_iter *i)
592 userdb__sym *s = sym_next(i);
593 return (s ? s->rec : 0);
596 /*----- Yellow pages support ----------------------------------------------*/
600 /* --- @userdb__foreachUser@ --- *
602 * Arguments: @int st@ = YP protocol-level status code
603 * @char *k@ = address of the key for this record
604 * @int ksz@ = size of the key
605 * @char *v@ = address of the value for this record
606 * @int vsz@ = size of the value
607 * @char *data@ = pointer to some data passed to me
609 * Returns: Zero to be called again, nonzero to end the enumeration.
611 * Use: Handles an incoming user record.
614 static int userdb__foreachUser(int st, char *k, int ksz,
615 char *v, int vsz, char *data)
622 cv = xmalloc(vsz + 1);
625 T( trace(TRACE_DEBUG, "debug: nis string: `%s'", cv); )
626 pw = userdb__buildUser(cv);
627 if (pw && !userdb__byName(&userdb__users, pw->pw_name)) {
628 IF_TRACING(TRACE_DEBUG, userdb__dumpUser(pw); )
629 userdb__addToMap(&userdb__users, pw->pw_name, pw->pw_uid, pw);
636 /* --- @userdb__foreachGroup@ --- *
638 * Arguments: @int st@ = YP protocol-level status code
639 * @char *k@ = address of the key for this record
640 * @int ksz@ = size of the key
641 * @char *v@ = address of the value for this record
642 * @int vsz@ = size of the value
643 * @char *data@ = pointer to some data passed to me
645 * Returns: Zero to be called again, nonzero to end the enumeration.
647 * Use: Handles an incoming user record.
650 static int userdb__foreachGroup(int st, char *k, int ksz,
651 char *v, int vsz, char *data)
658 cv = xmalloc(vsz + 1);
661 T( trace(TRACE_DEBUG, "debug: nis string: `%s'", cv); )
662 gr = userdb__buildGroup(cv);
663 if (gr && !userdb__byName(&userdb__groups, gr->gr_name)) {
664 IF_TRACING(TRACE_DEBUG, userdb__dumpGroup(gr); )
665 userdb__addToMap(&userdb__groups, gr->gr_name, gr->gr_gid, gr);
667 userdb_freeGroup(gr);
672 /* --- @userdb_yp@ --- *
678 * Use: Fetches the YP database of users.
685 /* --- Bind to a server --- */
687 if (yp_get_default_domain(&ypdom) ||
691 T( trace(TRACE_DEBUG, "debug: adding NIS users"); )
693 /* --- Fetch the users map --- */
696 static struct ypall_callback ucb = { userdb__foreachUser, 0 };
697 yp_all(ypdom, "passwd.byuid", &ucb);
700 /* --- Fetch the groups map --- */
703 static struct ypall_callback gcb = { userdb__foreachGroup, 0 };
704 yp_all(ypdom, "group.bygid", &gcb);
712 void userdb_yp(void) { ; }
716 /*----- Building the databases --------------------------------------------*/
718 /* --- @userdb_local@ --- *
724 * Use: Reads the local list of users into the maps.
727 void userdb_local(void)
729 T( trace(TRACE_DEBUG, "debug: adding local users"); )
731 /* --- Fetch users first --- */
737 while ((pw = getpwent()) != 0) {
738 IF_TRACING(TRACE_DEBUG, userdb__dumpUser(pw); )
739 if (!userdb__byName(&userdb__users, pw->pw_name))
740 userdb__addToMap(&userdb__users, pw->pw_name, pw->pw_uid,
741 userdb_copyUser(pw));
746 /* --- Then fetch groups --- */
752 while ((gr = getgrent()) != 0) {
753 IF_TRACING(TRACE_DEBUG, userdb__dumpGroup(gr); )
754 if (!userdb__byName(&userdb__groups, gr->gr_name))
755 userdb__addToMap(&userdb__groups, gr->gr_name, gr->gr_gid,
756 userdb_copyGroup(gr));
762 /* --- @userdb_init@ --- *
768 * Use: Initialises the user database.
771 void userdb_init(void)
773 userdb__createMap(&userdb__users);
774 userdb__createMap(&userdb__groups);
777 /* --- @userdb_end@ --- *
783 * Use: Closes down the user database.
786 void userdb_end(void)
788 userdb__clearMap(&userdb__users, userdb_freeUser);
789 userdb__clearMap(&userdb__groups, userdb_freeGroup);
792 /*----- Test rig ----------------------------------------------------------*/
796 void dumpit(const char *msg)
798 trace(TRACE_DEBUG, "debug: %s", msg);
802 for (userdb_iterateUsers(); (pw = userdb_nextUser()) != 0; )
803 userdb__dumpUser(pw);
808 for (userdb_iterateGroups(); (gr = userdb_nextGroup()) != 0; )
809 userdb__dumpGroup(gr);
816 /* traceon(stdout, TRACE_ALL); */
820 printf("loaded (%lu)\n", track_memused());
824 printf("cleared (%lu)\n", track_memused());
825 /* track_memlist(); */
829 printf("reloaded (%lu)\n", track_memused());
837 /*----- That's all, folks -------------------------------------------------*/