3 * $Id: userdb.c,v 1.8 1998/06/08 11:21:22 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.8 1998/06/08 11:21:22 mdw
33 * Fixed bug in password and group file reading: strtok doesn't handle
34 * double colons nicely.
36 * Revision 1.7 1998/04/23 13:27:46 mdw
37 * Switch to using the ypstuff interface to YP server.
39 * Revision 1.6 1998/01/12 16:46:33 mdw
42 * Revision 1.5 1997/09/17 10:24:08 mdw
43 * Use `uid_t' instead of `int' for uids and gids. Not quite sure why I
44 * didn't do this before.
46 * Revision 1.4 1997/08/20 16:24:58 mdw
47 * Patch memory leak. Rename `userdb_reinit' to `userdb_end' for more
50 * Revision 1.3 1997/08/07 09:44:29 mdw
51 * Read NIS-based passwords from the YP server directly, rather than using
52 * `popen(ypcat)', which is probably both slower and less secure.
54 * Revision 1.2 1997/08/04 10:24:26 mdw
55 * Sources placed under CVS control.
57 * Revision 1.1 1997/07/21 13:47:43 mdw
62 /*----- Header files ------------------------------------------------------*/
64 /* --- ANSI headers --- */
72 /* --- Unix headers --- */
76 #include <sys/types.h>
82 /* --- Local headers --- */
90 /*----- Type definitions --------------------------------------------------*/
92 /* --- A map link --- */
94 typedef struct userdb__node
{
95 struct userdb__node
*next
;
99 /* --- A reference to a real record --- */
101 typedef struct userdb__sym
{
106 /* --- A name- and number-mapping --- */
108 typedef struct userdb__map
{
114 /*----- Static variables --------------------------------------------------*/
116 static userdb__map userdb__users
; /* Map of user info blocks */
117 static sym_iter userdb__useri
; /* Iterator for users */
118 static userdb__map userdb__groups
; /* Map of group info blocks */
119 static sym_iter userdb__groupi
; /* Iterator for groups */
121 /*----- Map management functions ------------------------------------------*/
123 /* --- @userdb__createMap@ --- *
125 * Arguments: @userdb__map *m@ = pointer to a map block
129 * Use: Initialises a map table.
132 static void userdb__createMap(userdb__map
*m
)
134 sym_createTable(&m
->nmap
);
135 sym_createTable(&m
->idmap
);
139 /* --- @userdb__addToMap@ --- *
141 * Arguments: @userdb__map *m@ = pointer to the map block
142 * @const char *name@ = pointer to the item's name
143 * @uid_t id@ = the item's id number
144 * @void *rec@ = pointer to the actual record
148 * Use: Adds an item to the given map.
151 static void userdb__addToMap(userdb__map
*m
,
159 s
= sym_find(&m
->nmap
, name
, -1, sizeof(*s
), &f
);
163 s
= sym_find(&m
->idmap
, (char *)&id
, sizeof(id
), sizeof(*s
), &f
);
167 n
= xmalloc(sizeof(*n
));
173 /* --- @userdb__byName@ --- *
175 * Arguments: @userdb__map *m@ = pointer to a map block
176 * @const char *name@ = name to look up
178 * Returns: A pointer to the appropriate block, or zero if not found.
180 * Use: Looks up a name in a mapping and returns the result.
183 static void *userdb__byName(userdb__map
*m
, const char *name
)
185 userdb__sym
*s
= sym_find(&m
->nmap
, name
, -1, 0, 0);
186 return (s ? s
->rec
: 0);
189 /* --- @userdb__byId@ --- *
191 * Arguments: @userdb__map *m@ = pointer to a map block
192 * @uid_t id@ = id number to find
194 * Returns: A pointer to the appropriate block, or zero if not found.
196 * Use: Looks up an ID in a mapping, and returns the result.
199 static void *userdb__byId(userdb__map
*m
, uid_t id
)
201 userdb__sym
*s
= sym_find(&m
->idmap
, (char *)&id
, sizeof(id
), 0, 0);
202 return (s ? s
->rec
: 0);
205 /* --- @userdb__clearMap@ --- *
207 * Arguments: @userdb__map *m@ = pointer to a map block
208 * @void (*freerec)(void *rec)@ = pointer to a free-record proc
212 * Use: Clears a map, emptying it and releasing the memory it
216 static void userdb__clearMap(userdb__map
*m
, void (*freerec
)(void *rec
))
220 sym_destroyTable(&m
->nmap
);
221 sym_destroyTable(&m
->idmap
);
223 for (n
= m
->list
; n
; n
= t
) {
230 /*----- User and group block management -----------------------------------*/
232 /* --- @userdb__dumpUser@ --- *
234 * Arguments: @const struct passwd *pw@ = pointer to a user block
238 * Use: Writes a user's informationt to a stream.
243 static void userdb__dumpUser(const struct passwd
*pw
)
246 "debug: name `%s' passwd `%s' uid %i gid %i",
247 pw
->pw_name
, pw
->pw_passwd
, (int)pw
->pw_uid
, (int)pw
->pw_gid
);
249 "debug: ... gecos `%s' home `%s' shell `%s'",
250 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
255 /* --- @userdb__split@ --- *
257 * Arguments: @char *p@ = pointer to string
258 * @char **v@ = pointer to vector to fill in
259 * @int sz@ = maximum number of fields to split
261 * Returns: Number of fields extracted.
263 * Use: Splits a string into fields at colon characters.
266 static int userdb__split(char *p
, char **v
, int sz
)
270 *v
++ = p
; sz
--; count
++;
276 *v
++ = p
; sz
--; count
++;
288 /* --- @userdb_copyUser@ --- *
290 * Arguments: @struct passwd *pw@ = pointer to block to copy
292 * Returns: Pointer to the copy.
294 * Use: Copies a user block. The copy is `deep' so all the strings
295 * are copied too. Free the copy with @userdb_freeUser@ when
296 * you don't want it any more.
299 struct passwd
*userdb_copyUser(struct passwd
*pw
)
306 npw
= xmalloc(sizeof(*npw
));
308 npw
->pw_name
= xstrdup(pw
->pw_name
);
309 npw
->pw_passwd
= xstrdup(pw
->pw_passwd
);
310 npw
->pw_uid
= pw
->pw_uid
;
311 npw
->pw_gid
= pw
->pw_gid
;
312 npw
->pw_gecos
= xstrdup(pw
->pw_gecos
);
313 npw
->pw_dir
= xstrdup(pw
->pw_dir
);
314 npw
->pw_shell
= xstrdup(pw
->pw_shell
);
319 /* --- @userdb__buildUser@ --- *
321 * Arguments: @char *s@ = pointer to user string
323 * Returns: Pointer to a user block.
325 * Use: Converts a line from a user file into a password entry.
326 * Note that the string is corrupted by @strtok@ while it gets
330 static struct passwd
*userdb__buildUser(char *s
)
332 struct passwd
*pw
= xmalloc(sizeof(*pw
));
335 if (userdb__split(s
, v
, 7) < 7) {
340 pw
->pw_name
= xstrdup(v
[0]);
341 pw
->pw_passwd
= xstrdup(v
[1]);
342 pw
->pw_uid
= (uid_t
)atol(v
[2]);
343 pw
->pw_gid
= (gid_t
)atol(v
[3]);
344 pw
->pw_gecos
= xstrdup(v
[4]);
345 pw
->pw_dir
= xstrdup(v
[5]);
346 pw
->pw_shell
= xstrdup(v
[6]);
350 /* --- @userdb_freeUser@ --- *
352 * Arguments: @void *rec@ = pointer to a user record
356 * Use: Frees a user record.
359 void userdb_freeUser(void *rec
)
375 /* --- @userdb__dumpGroup@ --- *
377 * Arguments: @const struct group *gr@ = pointer to a group block
378 * @FILE *fp@ = pointer to stream to write on
382 * Use: Writes a group's information to a stream.
387 static void userdb__dumpGroup(const struct group
*gr
)
392 "debug: name `%s' passwd `%s' gid %i",
393 gr
->gr_name
, gr
->gr_passwd
, (int)gr
->gr_gid
);
394 for (p
= gr
->gr_mem
; *p
; p
++)
395 trace(TRACE_DEBUG
,"debug: ... `%s'", *p
);
400 /* --- @userdb_copyGroup@ --- *
402 * Arguments: @struct group *gr@ = pointer to group block
404 * Returns: Pointer to copied block
406 * Use: Copies a group block. The copy is `deep' so all the strings
407 * are copied too. Free the copy with @userdb_freeGroup@ when
408 * you don't want it any more.
411 struct group
*userdb_copyGroup(struct group
*gr
)
419 ngr
= xmalloc(sizeof(*ngr
));
421 ngr
->gr_name
= xstrdup(gr
->gr_name
);
422 ngr
->gr_passwd
= xstrdup(gr
->gr_passwd
);
423 ngr
->gr_gid
= gr
->gr_gid
;
425 for (max
= 0; gr
->gr_mem
[max
]; max
++)
427 ngr
->gr_mem
= xmalloc((max
+ 1) * sizeof(char *));
428 for (i
= 0; i
< max
; i
++)
429 ngr
->gr_mem
[i
] = xstrdup(gr
->gr_mem
[i
]);
430 ngr
->gr_mem
[max
] = 0;
435 /* --- @userdb__buildGroup@ --- *
437 * Arguments: @char *s@ = pointer to group line string
439 * Returns: Pointer to a group block
441 * Use: Parses an entry in the groups file. The string is garbled
442 * by @strtok@ as we go.
445 static struct group
*userdb__buildGroup(char *s
)
447 struct group
*gr
= xmalloc(sizeof(*gr
));
451 /* --- Do the easy bits --- */
453 if (userdb__split(s
, v
, 4) < 3) {
457 gr
->gr_name
= xstrdup(v
[0]);
458 gr
->gr_passwd
= xstrdup(v
[1]);
459 gr
->gr_gid
= (gid_t
)atol(v
[2]);
461 /* --- Count the number of members --- */
468 if ((s
= strpbrk(s
, ",")) == 0)
474 /* --- Allocate the block and fill it --- */
476 gr
->gr_mem
= xmalloc((i
+ 1) * sizeof(char *));
479 s
= strtok(v
[3], ",");
481 gr
->gr_mem
[i
++] = xstrdup(s
);
490 /* --- @userdb_freeGroup@ --- *
492 * Arguments: @void *rec@ = pointer to a group record
496 * Use: Frees a group record.
499 void userdb_freeGroup(void *rec
)
510 for (p
= gr
->gr_mem
; *p
; p
++)
516 /*----- Answering queries -------------------------------------------------*/
518 /* --- @userdb_userByName@, @userdb_userById@ --- *
520 * Arguments: @const char *name@ = pointer to user's name
521 * @uid_t id@ = user id to find
523 * Returns: Pointer to user block, or zero if not found.
525 * Use: Looks up a user by name or id.
528 struct passwd
*userdb_userByName(const char *name
)
529 { return (userdb__byName(&userdb__users
, name
)); }
531 struct passwd
*userdb_userById(uid_t id
)
532 { return (userdb__byId(&userdb__users
, id
)); }
534 /* --- @userdb_iterateUsers@, @userdb_iterateUsers_r@ --- *
536 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator object
540 * Use: Initialises an iteration for the user database.
543 void userdb_iterateUsers(void)
544 { userdb_iterateUsers_r(&userdb__useri
); }
546 void userdb_iterateUsers_r(userdb_iter
*i
)
547 { sym_createIter(i
, &userdb__users
.nmap
); }
549 /* --- @userdb_nextUser@, @userdb_nextUser_r@ --- *
551 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator oject
553 * Returns: Pointer to the next user block, or null.
555 * Use: Returns another user block.
558 struct passwd
*userdb_nextUser(void)
559 { return (userdb_nextUser_r(&userdb__useri
)); }
561 struct passwd
*userdb_nextUser_r(userdb_iter
*i
)
563 userdb__sym
*s
= sym_next(i
);
564 return (s ? s
->rec
: 0);
567 /* --- @userdb_groupByName@, @userdb_groupById@ --- *
569 * Arguments: @const char *name@ = pointer to group's name
570 * @gid_t id@ = group id to find
572 * Returns: Pointer to group block, or zero if not found.
574 * Use: Looks up a group by name or id.
577 struct group
*userdb_groupByName(const char *name
)
578 { return (userdb__byName(&userdb__groups
, name
)); }
580 struct group
*userdb_groupById(gid_t id
)
581 { return (userdb__byId(&userdb__groups
, id
)); }
583 /* --- @userdb_iterateGroups@, @userdb_iterateGroups_r@ --- *
585 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator object
589 * Use: Initialises an iteration for the group database.
592 void userdb_iterateGroups(void)
593 { userdb_iterateGroups_r(&userdb__groupi
); }
595 void userdb_iterateGroups_r(userdb_iter
*i
)
596 { sym_createIter(i
, &userdb__groups
.nmap
); }
598 /* --- @userdb_nextGroup@, @userdb_nextGroup_r@ --- *
600 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator oject
602 * Returns: Pointer to the next group block, or null.
604 * Use: Returns another group block.
607 struct group
*userdb_nextGroup(void)
608 { return (userdb_nextGroup_r(&userdb__groupi
)); }
610 struct group
*userdb_nextGroup_r(userdb_iter
*i
)
612 userdb__sym
*s
= sym_next(i
);
613 return (s ? s
->rec
: 0);
616 /*----- Yellow pages support ----------------------------------------------*/
620 /* --- @userdb__foreachUser@ --- *
622 * Arguments: @int st@ = YP protocol-level status code
623 * @char *k@ = address of the key for this record
624 * @int ksz@ = size of the key
625 * @char *v@ = address of the value for this record
626 * @int vsz@ = size of the value
627 * @char *data@ = pointer to some data passed to me
629 * Returns: Zero to be called again, nonzero to end the enumeration.
631 * Use: Handles an incoming user record.
634 static int userdb__foreachUser(int st
, char *k
, int ksz
,
635 char *v
, int vsz
, char *data
)
642 cv
= xmalloc(vsz
+ 1);
645 T( trace(TRACE_DEBUG
, "debug: nis string: `%s'", cv
); )
646 pw
= userdb__buildUser(cv
);
647 if (pw
&& !userdb__byName(&userdb__users
, pw
->pw_name
)) {
648 IF_TRACING(TRACE_DEBUG
, userdb__dumpUser(pw
); )
649 userdb__addToMap(&userdb__users
, pw
->pw_name
, pw
->pw_uid
, pw
);
656 /* --- @userdb__foreachGroup@ --- *
658 * Arguments: @int st@ = YP protocol-level status code
659 * @char *k@ = address of the key for this record
660 * @int ksz@ = size of the key
661 * @char *v@ = address of the value for this record
662 * @int vsz@ = size of the value
663 * @char *data@ = pointer to some data passed to me
665 * Returns: Zero to be called again, nonzero to end the enumeration.
667 * Use: Handles an incoming user record.
670 static int userdb__foreachGroup(int st
, char *k
, int ksz
,
671 char *v
, int vsz
, char *data
)
678 cv
= xmalloc(vsz
+ 1);
681 T( trace(TRACE_DEBUG
, "debug: nis string: `%s'", cv
); )
682 gr
= userdb__buildGroup(cv
);
683 if (gr
&& !userdb__byName(&userdb__groups
, gr
->gr_name
)) {
684 IF_TRACING(TRACE_DEBUG
, userdb__dumpGroup(gr
); )
685 userdb__addToMap(&userdb__groups
, gr
->gr_name
, gr
->gr_gid
, gr
);
687 userdb_freeGroup(gr
);
692 /* --- @userdb_yp@ --- *
698 * Use: Fetches the YP database of users.
703 /* --- Bind to a server --- */
709 T( trace(TRACE_DEBUG
, "debug: adding NIS users"); )
711 /* --- Fetch the users map --- */
714 static struct ypall_callback ucb
= { userdb__foreachUser
, 0 };
715 yp_all(yp_domain
, "passwd.byuid", &ucb
);
718 /* --- Fetch the groups map --- */
721 static struct ypall_callback gcb
= { userdb__foreachGroup
, 0 };
722 yp_all(yp_domain
, "group.bygid", &gcb
);
728 void userdb_yp(void) { ; }
732 /*----- Building the databases --------------------------------------------*/
734 /* --- @userdb_local@ --- *
740 * Use: Reads the local list of users into the maps.
743 void userdb_local(void)
745 T( trace(TRACE_DEBUG
, "debug: adding local users"); )
747 /* --- Fetch users first --- */
753 while ((pw
= getpwent()) != 0) {
754 IF_TRACING(TRACE_DEBUG
, userdb__dumpUser(pw
); )
755 if (!userdb__byName(&userdb__users
, pw
->pw_name
))
756 userdb__addToMap(&userdb__users
, pw
->pw_name
, pw
->pw_uid
,
757 userdb_copyUser(pw
));
762 /* --- Then fetch groups --- */
768 while ((gr
= getgrent()) != 0) {
769 IF_TRACING(TRACE_DEBUG
, userdb__dumpGroup(gr
); )
770 if (!userdb__byName(&userdb__groups
, gr
->gr_name
))
771 userdb__addToMap(&userdb__groups
, gr
->gr_name
, gr
->gr_gid
,
772 userdb_copyGroup(gr
));
778 /* --- @userdb_init@ --- *
784 * Use: Initialises the user database.
787 void userdb_init(void)
789 userdb__createMap(&userdb__users
);
790 userdb__createMap(&userdb__groups
);
793 /* --- @userdb_end@ --- *
799 * Use: Closes down the user database.
802 void userdb_end(void)
804 userdb__clearMap(&userdb__users
, userdb_freeUser
);
805 userdb__clearMap(&userdb__groups
, userdb_freeGroup
);
808 /*----- Test rig ----------------------------------------------------------*/
812 void dumpit(const char *msg
)
814 trace(TRACE_DEBUG
, "debug: %s", msg
);
818 for (userdb_iterateUsers(); (pw
= userdb_nextUser()) != 0; )
819 userdb__dumpUser(pw
);
824 for (userdb_iterateGroups(); (gr
= userdb_nextGroup()) != 0; )
825 userdb__dumpGroup(gr
);
832 traceon(stdout
, TRACE_ALL
);
837 /* printf("loaded (%lu)\n", track_memused()); */
841 /* printf("cleared (%lu)\n", track_memused()); */
842 /* track_memlist(); */
846 /* printf("reloaded (%lu)\n", track_memused()); */
854 /*----- That's all, folks -------------------------------------------------*/