3 * $Id: userdb.c,v 1.10 2003/10/12 00:39:16 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.10 2003/10/12 00:39:16 mdw
33 * Light fixes for strange building.
35 * Revision 1.9 2003/10/12 00:14:55 mdw
36 * Major overhaul. Now uses DSA signatures rather than the bogus symmetric
37 * encrypt-and-hope thing. Integrated with mLib and Catacomb.
39 * Revision 1.8 1998/06/08 11:21:22 mdw
40 * Fixed bug in password and group file reading: strtok doesn't handle
41 * double colons nicely.
43 * Revision 1.7 1998/04/23 13:27:46 mdw
44 * Switch to using the ypstuff interface to YP server.
46 * Revision 1.6 1998/01/12 16:46:33 mdw
49 * Revision 1.5 1997/09/17 10:24:08 mdw
50 * Use `uid_t' instead of `int' for uids and gids. Not quite sure why I
51 * didn't do this before.
53 * Revision 1.4 1997/08/20 16:24:58 mdw
54 * Patch memory leak. Rename `userdb_reinit' to `userdb_end' for more
57 * Revision 1.3 1997/08/07 09:44:29 mdw
58 * Read NIS-based passwords from the YP server directly, rather than using
59 * `popen(ypcat)', which is probably both slower and less secure.
61 * Revision 1.2 1997/08/04 10:24:26 mdw
62 * Sources placed under CVS control.
64 * Revision 1.1 1997/07/21 13:47:43 mdw
69 /*----- Header files ------------------------------------------------------*/
71 /* --- ANSI headers --- */
79 /* --- Unix headers --- */
83 #include <sys/types.h>
89 /* --- mLib headers --- */
91 #include <mLib/alloc.h>
93 #include <mLib/trace.h>
95 /* --- Local headers --- */
101 /*----- Type definitions --------------------------------------------------*/
103 /* --- A map link --- */
105 typedef struct userdb__node
{
106 struct userdb__node
*next
;
110 /* --- A reference to a real record --- */
112 typedef struct userdb__sym
{
117 /* --- A name- and number-mapping --- */
119 typedef struct userdb__map
{
125 /*----- Static variables --------------------------------------------------*/
127 static userdb__map userdb__users
; /* Map of user info blocks */
128 static sym_iter userdb__useri
; /* Iterator for users */
129 static userdb__map userdb__groups
; /* Map of group info blocks */
130 static sym_iter userdb__groupi
; /* Iterator for groups */
132 /*----- Map management functions ------------------------------------------*/
134 /* --- @userdb__createMap@ --- *
136 * Arguments: @userdb__map *m@ = pointer to a map block
140 * Use: Initialises a map table.
143 static void userdb__createMap(userdb__map
*m
)
145 sym_create(&m
->nmap
);
146 sym_create(&m
->idmap
);
150 /* --- @userdb__addToMap@ --- *
152 * Arguments: @userdb__map *m@ = pointer to the map block
153 * @const char *name@ = pointer to the item's name
154 * @uid_t id@ = the item's id number
155 * @void *rec@ = pointer to the actual record
159 * Use: Adds an item to the given map.
162 static void userdb__addToMap(userdb__map
*m
,
170 s
= sym_find(&m
->nmap
, name
, -1, sizeof(*s
), &f
);
174 s
= sym_find(&m
->idmap
, (char *)&id
, sizeof(id
), sizeof(*s
), &f
);
178 n
= xmalloc(sizeof(*n
));
184 /* --- @userdb__byName@ --- *
186 * Arguments: @userdb__map *m@ = pointer to a map block
187 * @const char *name@ = name to look up
189 * Returns: A pointer to the appropriate block, or zero if not found.
191 * Use: Looks up a name in a mapping and returns the result.
194 static void *userdb__byName(userdb__map
*m
, const char *name
)
196 userdb__sym
*s
= sym_find(&m
->nmap
, name
, -1, 0, 0);
197 return (s ? s
->rec
: 0);
200 /* --- @userdb__byId@ --- *
202 * Arguments: @userdb__map *m@ = pointer to a map block
203 * @uid_t id@ = id number to find
205 * Returns: A pointer to the appropriate block, or zero if not found.
207 * Use: Looks up an ID in a mapping, and returns the result.
210 static void *userdb__byId(userdb__map
*m
, uid_t id
)
212 userdb__sym
*s
= sym_find(&m
->idmap
, (char *)&id
, sizeof(id
), 0, 0);
213 return (s ? s
->rec
: 0);
216 /* --- @userdb__clearMap@ --- *
218 * Arguments: @userdb__map *m@ = pointer to a map block
219 * @void (*freerec)(void *rec)@ = pointer to a free-record proc
223 * Use: Clears a map, emptying it and releasing the memory it
227 static void userdb__clearMap(userdb__map
*m
, void (*freerec
)(void *rec
))
231 sym_destroy(&m
->nmap
);
232 sym_destroy(&m
->idmap
);
234 for (n
= m
->list
; n
; n
= t
) {
241 /*----- User and group block management -----------------------------------*/
243 /* --- @userdb__dumpUser@ --- *
245 * Arguments: @const struct passwd *pw@ = pointer to a user block
249 * Use: Writes a user's informationt to a stream.
254 static void userdb__dumpUser(const struct passwd
*pw
)
257 "debug: name `%s' passwd `%s' uid %i gid %i",
258 pw
->pw_name
, pw
->pw_passwd
, (int)pw
->pw_uid
, (int)pw
->pw_gid
);
260 "debug: ... gecos `%s' home `%s' shell `%s'",
261 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
266 /* --- @userdb__split@ --- *
268 * Arguments: @char *p@ = pointer to string
269 * @char **v@ = pointer to vector to fill in
270 * @int sz@ = maximum number of fields to split
272 * Returns: Number of fields extracted.
274 * Use: Splits a string into fields at colon characters.
277 static int userdb__split(char *p
, char **v
, int sz
)
281 *v
++ = p
; sz
--; count
++;
287 *v
++ = p
; sz
--; count
++;
299 /* --- @userdb_copyUser@ --- *
301 * Arguments: @struct passwd *pw@ = pointer to block to copy
303 * Returns: Pointer to the copy.
305 * Use: Copies a user block. The copy is `deep' so all the strings
306 * are copied too. Free the copy with @userdb_freeUser@ when
307 * you don't want it any more.
310 struct passwd
*userdb_copyUser(struct passwd
*pw
)
317 npw
= xmalloc(sizeof(*npw
));
319 npw
->pw_name
= xstrdup(pw
->pw_name
);
320 npw
->pw_passwd
= xstrdup(pw
->pw_passwd
);
321 npw
->pw_uid
= pw
->pw_uid
;
322 npw
->pw_gid
= pw
->pw_gid
;
323 npw
->pw_gecos
= xstrdup(pw
->pw_gecos
);
324 npw
->pw_dir
= xstrdup(pw
->pw_dir
);
325 npw
->pw_shell
= xstrdup(pw
->pw_shell
);
330 /* --- @userdb__buildUser@ --- *
332 * Arguments: @char *s@ = pointer to user string
334 * Returns: Pointer to a user block.
336 * Use: Converts a line from a user file into a password entry.
337 * Note that the string is corrupted by @strtok@ while it gets
341 static struct passwd
*userdb__buildUser(char *s
)
343 struct passwd
*pw
= xmalloc(sizeof(*pw
));
346 if (userdb__split(s
, v
, 7) < 7) {
351 pw
->pw_name
= xstrdup(v
[0]);
352 pw
->pw_passwd
= xstrdup(v
[1]);
353 pw
->pw_uid
= (uid_t
)atol(v
[2]);
354 pw
->pw_gid
= (gid_t
)atol(v
[3]);
355 pw
->pw_gecos
= xstrdup(v
[4]);
356 pw
->pw_dir
= xstrdup(v
[5]);
357 pw
->pw_shell
= xstrdup(v
[6]);
361 /* --- @userdb_freeUser@ --- *
363 * Arguments: @void *rec@ = pointer to a user record
367 * Use: Frees a user record.
370 void userdb_freeUser(void *rec
)
386 /* --- @userdb__dumpGroup@ --- *
388 * Arguments: @const struct group *gr@ = pointer to a group block
389 * @FILE *fp@ = pointer to stream to write on
393 * Use: Writes a group's information to a stream.
398 static void userdb__dumpGroup(const struct group
*gr
)
403 "debug: name `%s' passwd `%s' gid %i",
404 gr
->gr_name
, gr
->gr_passwd
, (int)gr
->gr_gid
);
405 for (p
= gr
->gr_mem
; *p
; p
++)
406 trace(TRACE_DEBUG
,"debug: ... `%s'", *p
);
411 /* --- @userdb_copyGroup@ --- *
413 * Arguments: @struct group *gr@ = pointer to group block
415 * Returns: Pointer to copied block
417 * Use: Copies a group block. The copy is `deep' so all the strings
418 * are copied too. Free the copy with @userdb_freeGroup@ when
419 * you don't want it any more.
422 struct group
*userdb_copyGroup(struct group
*gr
)
430 ngr
= xmalloc(sizeof(*ngr
));
432 ngr
->gr_name
= xstrdup(gr
->gr_name
);
433 ngr
->gr_passwd
= xstrdup(gr
->gr_passwd
);
434 ngr
->gr_gid
= gr
->gr_gid
;
436 for (max
= 0; gr
->gr_mem
[max
]; max
++)
438 ngr
->gr_mem
= xmalloc((max
+ 1) * sizeof(char *));
439 for (i
= 0; i
< max
; i
++)
440 ngr
->gr_mem
[i
] = xstrdup(gr
->gr_mem
[i
]);
441 ngr
->gr_mem
[max
] = 0;
446 /* --- @userdb__buildGroup@ --- *
448 * Arguments: @char *s@ = pointer to group line string
450 * Returns: Pointer to a group block
452 * Use: Parses an entry in the groups file. The string is garbled
453 * by @strtok@ as we go.
456 static struct group
*userdb__buildGroup(char *s
)
458 struct group
*gr
= xmalloc(sizeof(*gr
));
462 /* --- Do the easy bits --- */
464 if (userdb__split(s
, v
, 4) < 3) {
468 gr
->gr_name
= xstrdup(v
[0]);
469 gr
->gr_passwd
= xstrdup(v
[1]);
470 gr
->gr_gid
= (gid_t
)atol(v
[2]);
472 /* --- Count the number of members --- */
479 if ((s
= strpbrk(s
, ",")) == 0)
485 /* --- Allocate the block and fill it --- */
487 gr
->gr_mem
= xmalloc((i
+ 1) * sizeof(char *));
490 s
= strtok(v
[3], ",");
492 gr
->gr_mem
[i
++] = xstrdup(s
);
501 /* --- @userdb_freeGroup@ --- *
503 * Arguments: @void *rec@ = pointer to a group record
507 * Use: Frees a group record.
510 void userdb_freeGroup(void *rec
)
521 for (p
= gr
->gr_mem
; *p
; p
++)
527 /*----- Answering queries -------------------------------------------------*/
529 /* --- @userdb_userByName@, @userdb_userById@ --- *
531 * Arguments: @const char *name@ = pointer to user's name
532 * @uid_t id@ = user id to find
534 * Returns: Pointer to user block, or zero if not found.
536 * Use: Looks up a user by name or id.
539 struct passwd
*userdb_userByName(const char *name
)
540 { return (userdb__byName(&userdb__users
, name
)); }
542 struct passwd
*userdb_userById(uid_t id
)
543 { return (userdb__byId(&userdb__users
, id
)); }
545 /* --- @userdb_iterateUsers@, @userdb_iterateUsers_r@ --- *
547 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator object
551 * Use: Initialises an iteration for the user database.
554 void userdb_iterateUsers(void)
555 { userdb_iterateUsers_r(&userdb__useri
); }
557 void userdb_iterateUsers_r(userdb_iter
*i
)
558 { sym_mkiter(i
, &userdb__users
.nmap
); }
560 /* --- @userdb_nextUser@, @userdb_nextUser_r@ --- *
562 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator oject
564 * Returns: Pointer to the next user block, or null.
566 * Use: Returns another user block.
569 struct passwd
*userdb_nextUser(void)
570 { return (userdb_nextUser_r(&userdb__useri
)); }
572 struct passwd
*userdb_nextUser_r(userdb_iter
*i
)
574 userdb__sym
*s
= sym_next(i
);
575 return (s ? s
->rec
: 0);
578 /* --- @userdb_groupByName@, @userdb_groupById@ --- *
580 * Arguments: @const char *name@ = pointer to group's name
581 * @gid_t id@ = group id to find
583 * Returns: Pointer to group block, or zero if not found.
585 * Use: Looks up a group by name or id.
588 struct group
*userdb_groupByName(const char *name
)
589 { return (userdb__byName(&userdb__groups
, name
)); }
591 struct group
*userdb_groupById(gid_t id
)
592 { return (userdb__byId(&userdb__groups
, id
)); }
594 /* --- @userdb_iterateGroups@, @userdb_iterateGroups_r@ --- *
596 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator object
600 * Use: Initialises an iteration for the group database.
603 void userdb_iterateGroups(void)
604 { userdb_iterateGroups_r(&userdb__groupi
); }
606 void userdb_iterateGroups_r(userdb_iter
*i
)
607 { sym_mkiter(i
, &userdb__groups
.nmap
); }
609 /* --- @userdb_nextGroup@, @userdb_nextGroup_r@ --- *
611 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator oject
613 * Returns: Pointer to the next group block, or null.
615 * Use: Returns another group block.
618 struct group
*userdb_nextGroup(void)
619 { return (userdb_nextGroup_r(&userdb__groupi
)); }
621 struct group
*userdb_nextGroup_r(userdb_iter
*i
)
623 userdb__sym
*s
= sym_next(i
);
624 return (s ? s
->rec
: 0);
627 /*----- Yellow pages support ----------------------------------------------*/
631 /* --- @userdb__foreachUser@ --- *
633 * Arguments: @int st@ = YP protocol-level status code
634 * @char *k@ = address of the key for this record
635 * @int ksz@ = size of the key
636 * @char *v@ = address of the value for this record
637 * @int vsz@ = size of the value
638 * @char *data@ = pointer to some data passed to me
640 * Returns: Zero to be called again, nonzero to end the enumeration.
642 * Use: Handles an incoming user record.
645 static int userdb__foreachUser(int st
, char *k
, int ksz
,
646 char *v
, int vsz
, char *data
)
653 cv
= xmalloc(vsz
+ 1);
656 T( trace(TRACE_DEBUG
, "debug: nis string: `%s'", cv
); )
657 pw
= userdb__buildUser(cv
);
658 if (pw
&& !userdb__byName(&userdb__users
, pw
->pw_name
)) {
659 IF_TRACING(TRACE_DEBUG
, userdb__dumpUser(pw
); )
660 userdb__addToMap(&userdb__users
, pw
->pw_name
, pw
->pw_uid
, pw
);
667 /* --- @userdb__foreachGroup@ --- *
669 * Arguments: @int st@ = YP protocol-level status code
670 * @char *k@ = address of the key for this record
671 * @int ksz@ = size of the key
672 * @char *v@ = address of the value for this record
673 * @int vsz@ = size of the value
674 * @char *data@ = pointer to some data passed to me
676 * Returns: Zero to be called again, nonzero to end the enumeration.
678 * Use: Handles an incoming user record.
681 static int userdb__foreachGroup(int st
, char *k
, int ksz
,
682 char *v
, int vsz
, char *data
)
689 cv
= xmalloc(vsz
+ 1);
692 T( trace(TRACE_DEBUG
, "debug: nis string: `%s'", cv
); )
693 gr
= userdb__buildGroup(cv
);
694 if (gr
&& !userdb__byName(&userdb__groups
, gr
->gr_name
)) {
695 IF_TRACING(TRACE_DEBUG
, userdb__dumpGroup(gr
); )
696 userdb__addToMap(&userdb__groups
, gr
->gr_name
, gr
->gr_gid
, gr
);
698 userdb_freeGroup(gr
);
703 /* --- @userdb_yp@ --- *
709 * Use: Fetches the YP database of users.
714 /* --- Bind to a server --- */
720 T( trace(TRACE_DEBUG
, "debug: adding NIS users"); )
722 /* --- Fetch the users map --- */
725 static struct ypall_callback ucb
= { userdb__foreachUser
, 0 };
726 yp_all(yp_domain
, "passwd.byuid", &ucb
);
729 /* --- Fetch the groups map --- */
732 static struct ypall_callback gcb
= { userdb__foreachGroup
, 0 };
733 yp_all(yp_domain
, "group.bygid", &gcb
);
739 void userdb_yp(void) { ; }
743 /*----- Building the databases --------------------------------------------*/
745 /* --- @userdb_local@ --- *
751 * Use: Reads the local list of users into the maps.
754 void userdb_local(void)
756 T( trace(TRACE_DEBUG
, "debug: adding local users"); )
758 /* --- Fetch users first --- */
764 while ((pw
= getpwent()) != 0) {
765 IF_TRACING(TRACE_DEBUG
, userdb__dumpUser(pw
); )
766 if (!userdb__byName(&userdb__users
, pw
->pw_name
))
767 userdb__addToMap(&userdb__users
, pw
->pw_name
, pw
->pw_uid
,
768 userdb_copyUser(pw
));
773 /* --- Then fetch groups --- */
779 while ((gr
= getgrent()) != 0) {
780 IF_TRACING(TRACE_DEBUG
, userdb__dumpGroup(gr
); )
781 if (!userdb__byName(&userdb__groups
, gr
->gr_name
))
782 userdb__addToMap(&userdb__groups
, gr
->gr_name
, gr
->gr_gid
,
783 userdb_copyGroup(gr
));
789 /* --- @userdb_init@ --- *
795 * Use: Initialises the user database.
798 void userdb_init(void)
800 userdb__createMap(&userdb__users
);
801 userdb__createMap(&userdb__groups
);
804 /* --- @userdb_end@ --- *
810 * Use: Closes down the user database.
813 void userdb_end(void)
815 userdb__clearMap(&userdb__users
, userdb_freeUser
);
816 userdb__clearMap(&userdb__groups
, userdb_freeGroup
);
819 /*----- Test rig ----------------------------------------------------------*/
823 void dumpit(const char *msg
)
825 trace(TRACE_DEBUG
, "debug: %s", msg
);
829 for (userdb_iterateUsers(); (pw
= userdb_nextUser()) != 0; )
830 userdb__dumpUser(pw
);
835 for (userdb_iterateGroups(); (gr
= userdb_nextGroup()) != 0; )
836 userdb__dumpGroup(gr
);
843 trace_on(stdout
, TRACE_ALL
);
848 /* printf("loaded (%lu)\n", track_memused()); */
852 /* printf("cleared (%lu)\n", track_memused()); */
853 /* track_memlist(); */
857 /* printf("reloaded (%lu)\n", track_memused()); */
865 /*----- That's all, folks -------------------------------------------------*/