3 * $Id: userdb.c,v 1.3 1997/08/07 09:44:29 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.3 1997/08/07 09:44:29 mdw
33 * Read NIS-based passwords from the YP server directly, rather than using
34 * `popen(ypcat)', which is probably both slower and less secure.
36 * Revision 1.2 1997/08/04 10:24:26 mdw
37 * Sources placed under CVS control.
39 * Revision 1.1 1997/07/21 13:47:43 mdw
44 /*----- Header files ------------------------------------------------------*/
46 /* --- ANSI headers --- */
54 /* --- Unix headers --- */
58 #include <sys/types.h>
62 # include <rpcsvc/ypclnt.h>
63 # include <rpcsvc/yp_prot.h>
70 /* --- Local headers --- */
77 /*----- Type definitions --------------------------------------------------*/
79 /* --- A map link --- */
81 typedef struct userdb__node
{
82 struct userdb__node
*next
;
86 /* --- A reference to a real record --- */
88 typedef struct userdb__sym
{
93 /* --- A name- and number-mapping --- */
95 typedef struct userdb__map
{
101 /*----- Static variables --------------------------------------------------*/
103 static userdb__map userdb__users
; /* Map of user info blocks */
104 static sym_iter userdb__useri
; /* Iterator for users */
105 static userdb__map userdb__groups
; /* Map of group info blocks */
106 static sym_iter userdb__groupi
; /* Iterator for groups */
108 /*----- Map management functions ------------------------------------------*/
110 /* --- @userdb__createMap@ --- *
112 * Arguments: @userdb__map *m@ = pointer to a map block
116 * Use: Initialises a map table.
119 static void userdb__createMap(userdb__map
*m
)
121 sym_createTable(&m
->nmap
);
122 sym_createTable(&m
->idmap
);
126 /* --- @userdb__addToMap@ --- *
128 * Arguments: @userdb__map *m@ = pointer to the map block
129 * @const char *name@ = pointer to the item's name
130 * @int id@ = the item's id number
131 * @void *rec@ = pointer to the actual record
135 * Use: Adds an item to the given map.
138 static void userdb__addToMap(userdb__map
*m
,
146 s
= sym_find(&m
->nmap
, name
, -1, sizeof(*s
), &f
);
150 s
= sym_find(&m
->idmap
, (char *)&id
, sizeof(id
), sizeof(*s
), &f
);
154 n
= xmalloc(sizeof(*n
));
160 /* --- @userdb__byName@ --- *
162 * Arguments: @userdb__map *m@ = pointer to a map block
163 * @const char *name@ = name to look up
165 * Returns: A pointer to the appropriate block, or zero if not found.
167 * Use: Looks up a name in a mapping and returns the result.
170 static void *userdb__byName(userdb__map
*m
, const char *name
)
172 userdb__sym
*s
= sym_find(&m
->nmap
, name
, -1, 0, 0);
173 return (s ? s
->rec
: 0);
176 /* --- @userdb__byId@ --- *
178 * Arguments: @userdb__map *m@ = pointer to a map block
179 * @int id@ = id number to find
181 * Returns: A pointer to the appropriate block, or zero if not found.
183 * Use: Looks up an ID in a mapping, and returns the result.
186 static void *userdb__byId(userdb__map
*m
, int id
)
188 userdb__sym
*s
= sym_find(&m
->idmap
, (char *)&id
, sizeof(id
), 0, 0);
189 return (s ? s
->rec
: 0);
192 /* --- @userdb__clearMap@ --- *
194 * Arguments: @userdb__map *m@ = pointer to a map block
195 * @void (*freerec)(void *rec)@ = pointer to a free-record proc
199 * Use: Clears a map, emptying it and releasing the memory it
203 static void userdb__clearMap(userdb__map
*m
, void (*freerec
)(void *rec
))
207 sym_destroyTable(&m
->nmap
);
208 sym_destroyTable(&m
->idmap
);
210 for (n
= m
->list
; n
; n
= t
) {
217 /*----- User and group block management -----------------------------------*/
219 /* --- @userdb__dumpUser@ --- *
221 * Arguments: @const struct passwd *pw@ = pointer to a user block
225 * Use: Writes a user's informationt to a stream.
230 static void userdb__dumpUser(const struct passwd
*pw
)
233 "debug: name `%s' passwd `%s' uid %i gid %i",
234 pw
->pw_name
, pw
->pw_passwd
, (int)pw
->pw_uid
, (int)pw
->pw_gid
);
236 "debug: ... gecos `%s' home `%s' shell `%s'",
237 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
242 /* --- @userdb_copyUser@ --- *
244 * Arguments: @struct passwd *pw@ = pointer to block to copy
246 * Returns: Pointer to the copy.
248 * Use: Copies a user block. The copy is `deep' so all the strings
249 * are copied too. Free the copy with @userdb_freeUser@ when
250 * you don't want it any more.
253 struct passwd
*userdb_copyUser(struct passwd
*pw
)
260 npw
= xmalloc(sizeof(*npw
));
262 npw
->pw_name
= xstrdup(pw
->pw_name
);
263 npw
->pw_passwd
= xstrdup(pw
->pw_passwd
);
264 npw
->pw_uid
= pw
->pw_uid
;
265 npw
->pw_gid
= pw
->pw_gid
;
266 npw
->pw_gecos
= xstrdup(pw
->pw_gecos
);
267 npw
->pw_dir
= xstrdup(pw
->pw_dir
);
268 npw
->pw_shell
= xstrdup(pw
->pw_shell
);
273 /* --- @userdb__buildUser@ --- *
275 * Arguments: @char *s@ = pointer to user string
277 * Returns: Pointer to a user block.
279 * Use: Converts a line from a user file into a password entry.
280 * Note that the string is corrupted by @strtok@ while it gets
284 static struct passwd
*userdb__buildUser(char *s
)
286 struct passwd
*pw
= xmalloc(sizeof(*pw
));
288 s
= strtok(s
, ":"); if (!s
) goto tidy_0
; pw
->pw_name
= xstrdup(s
);
289 s
= strtok(0, ":"); if (!s
) goto tidy_1
; pw
->pw_passwd
= xstrdup(s
);
290 s
= strtok(0, ":"); if (!s
) goto tidy_2
; pw
->pw_uid
= atoi(s
);
291 s
= strtok(0, ":"); if (!s
) goto tidy_2
; pw
->pw_gid
= atoi(s
);
292 s
= strtok(0, ":"); if (!s
) goto tidy_2
; pw
->pw_gecos
= xstrdup(s
);
293 s
= strtok(0, ":"); if (!s
) goto tidy_3
; pw
->pw_dir
= xstrdup(s
);
294 s
= strtok(0, ":"); if (!s
) goto tidy_4
; pw
->pw_shell
= xstrdup(s
);
297 /* --- Error handling --- */
313 /* --- @userdb_freeUser@ --- *
315 * Arguments: @void *rec@ = pointer to a user record
319 * Use: Frees a user record.
322 void userdb_freeUser(void *rec
)
338 /* --- @userdb__dumpGroup@ --- *
340 * Arguments: @const struct group *gr@ = pointer to a group block
341 * @FILE *fp@ = pointer to stream to write on
345 * Use: Writes a group's information to a stream.
350 static void userdb__dumpGroup(const struct group
*gr
)
355 "debug: name `%s' passwd `%s' gid %i",
356 gr
->gr_name
, gr
->gr_passwd
, (int)gr
->gr_gid
);
357 for (p
= gr
->gr_mem
; *p
; p
++)
358 trace(TRACE_DEBUG
,"debug: ... `%s'", *p
);
363 /* --- @userdb_copyGroup@ --- *
365 * Arguments: @struct group *gr@ = pointer to group block
367 * Returns: Pointer to copied block
369 * Use: Copies a group block. The copy is `deep' so all the strings
370 * are copied too. Free the copy with @userdb_freeGroup@ when
371 * you don't want it any more.
374 struct group
*userdb_copyGroup(struct group
*gr
)
382 ngr
= xmalloc(sizeof(*ngr
));
384 ngr
->gr_name
= xstrdup(gr
->gr_name
);
385 ngr
->gr_passwd
= xstrdup(gr
->gr_passwd
);
386 ngr
->gr_gid
= gr
->gr_gid
;
388 for (max
= 0; gr
->gr_mem
[max
]; max
++)
390 ngr
->gr_mem
= xmalloc((max
+ 1) * sizeof(char *));
391 for (i
= 0; i
< max
; i
++)
392 ngr
->gr_mem
[i
] = xstrdup(gr
->gr_mem
[i
]);
393 ngr
->gr_mem
[max
] = 0;
398 /* --- @userdb__buildGroup@ --- *
400 * Arguments: @char *s@ = pointer to group line string
402 * Returns: Pointer to a group block
404 * Use: Parses an entry in the groups file. The string is garbled
405 * by @strtok@ as we go.
408 static struct group
*userdb__buildGroup(char *s
)
410 struct group
*gr
= xmalloc(sizeof(*gr
));
414 /* --- Do the easy bits --- */
416 s
= strtok(s
, ":"); if (!s
) goto tidy_0
; gr
->gr_name
= xstrdup(s
);
417 s
= strtok(0, ":"); if (!s
) goto tidy_1
; gr
->gr_passwd
= xstrdup(s
);
418 s
= strtok(0, ":"); if (!s
) goto tidy_2
; gr
->gr_gid
= atoi(s
);
420 /* --- Find the start of the member list --- */
426 /* --- Count the number of members --- */
432 if ((p
= strpbrk(p
, ",")) == 0)
437 /* --- Allocate the block and fill it --- */
439 gr
->gr_mem
= xmalloc((i
+ 1) * sizeof(char *));
443 gr
->gr_mem
[i
++] = xstrdup(s
);
450 /* --- Various tidying-up things --- */
462 /* --- @userdb_freeGroup@ --- *
464 * Arguments: @void *rec@ = pointer to a group record
468 * Use: Frees a group record.
471 void userdb_freeGroup(void *rec
)
482 for (p
= gr
->gr_mem
; *p
; p
++)
488 /*----- Answering queries -------------------------------------------------*/
490 /* --- @userdb_userByName@, @userdb_userById@ --- *
492 * Arguments: @const char *name@ = pointer to user's name
493 * @uid_t id@ = user id to find
495 * Returns: Pointer to user block, or zero if not found.
497 * Use: Looks up a user by name or id.
500 struct passwd
*userdb_userByName(const char *name
)
501 { return (userdb__byName(&userdb__users
, name
)); }
503 struct passwd
*userdb_userById(uid_t id
)
504 { return (userdb__byId(&userdb__users
, id
)); }
506 /* --- @userdb_iterateUsers@, @userdb_iterateUsers_r@ --- *
508 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator object
512 * Use: Initialises an iteration for the user database.
515 void userdb_iterateUsers(void)
516 { userdb_iterateUsers_r(&userdb__useri
); }
518 void userdb_iterateUsers_r(userdb_iter
*i
)
519 { sym_createIter(i
, &userdb__users
.nmap
); }
521 /* --- @userdb_nextUser@, @userdb_nextUser_r@ --- *
523 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator oject
525 * Returns: Pointer to the next user block, or null.
527 * Use: Returns another user block.
530 struct passwd
*userdb_nextUser(void)
531 { return (userdb_nextUser_r(&userdb__useri
)); }
533 struct passwd
*userdb_nextUser_r(userdb_iter
*i
)
535 userdb__sym
*s
= sym_next(i
);
536 return (s ? s
->rec
: 0);
539 /* --- @userdb_groupByName@, @userdb_groupById@ --- *
541 * Arguments: @const char *name@ = pointer to group's name
542 * @gid_t id@ = group id to find
544 * Returns: Pointer to group block, or zero if not found.
546 * Use: Looks up a group by name or id.
549 struct group
*userdb_groupByName(const char *name
)
550 { return (userdb__byName(&userdb__groups
, name
)); }
552 struct group
*userdb_groupById(gid_t id
)
553 { return (userdb__byId(&userdb__groups
, id
)); }
555 /* --- @userdb_iterateGroups@, @userdb_iterateGroups_r@ --- *
557 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator object
561 * Use: Initialises an iteration for the group database.
564 void userdb_iterateGroups(void)
565 { userdb_iterateGroups_r(&userdb__groupi
); }
567 void userdb_iterateGroups_r(userdb_iter
*i
)
568 { sym_createIter(i
, &userdb__groups
.nmap
); }
570 /* --- @userdb_nextGroup@, @userdb_nextGroup_r@ --- *
572 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator oject
574 * Returns: Pointer to the next group block, or null.
576 * Use: Returns another group block.
579 struct group
*userdb_nextGroup(void)
580 { return (userdb_nextGroup_r(&userdb__groupi
)); }
582 struct group
*userdb_nextGroup_r(userdb_iter
*i
)
584 userdb__sym
*s
= sym_next(i
);
585 return (s ? s
->rec
: 0);
588 /*----- Yellow pages support ----------------------------------------------*/
592 /* --- @userdb__foreachUser@ --- *
594 * Arguments: @int st@ = YP protocol-level status code
595 * @char *k@ = address of the key for this record
596 * @int ksz@ = size of the key
597 * @char *v@ = address of the value for this record
598 * @int vsz@ = size of the value
599 * @char *data@ = pointer to some data passed to me
601 * Returns: Zero to be called again, nonzero to end the enumeration.
603 * Use: Handles an incoming user record.
606 static int userdb__foreachUser(int st
, char *k
, int ksz
,
607 char *v
, int vsz
, char *data
)
614 cv
= xmalloc(vsz
+ 1);
617 T( trace(TRACE_DEBUG
, "debug: nis string: `%s'", cv
); )
618 pw
= userdb__buildUser(cv
);
619 if (pw
&& !userdb__byName(&userdb__users
, pw
->pw_name
)) {
620 IF_TRACING(TRACE_DEBUG
, userdb__dumpUser(pw
); )
621 userdb__addToMap(&userdb__users
, pw
->pw_name
, pw
->pw_uid
, pw
);
627 /* --- @userdb__foreachGroup@ --- *
629 * Arguments: @int st@ = YP protocol-level status code
630 * @char *k@ = address of the key for this record
631 * @int ksz@ = size of the key
632 * @char *v@ = address of the value for this record
633 * @int vsz@ = size of the value
634 * @char *data@ = pointer to some data passed to me
636 * Returns: Zero to be called again, nonzero to end the enumeration.
638 * Use: Handles an incoming user record.
641 static int userdb__foreachGroup(int st
, char *k
, int ksz
,
642 char *v
, int vsz
, char *data
)
649 cv
= xmalloc(vsz
+ 1);
652 T( trace(TRACE_DEBUG
, "debug: nis string: `%s'", cv
); )
653 gr
= userdb__buildGroup(cv
);
654 if (gr
&& !userdb__byName(&userdb__groups
, gr
->gr_name
)) {
655 IF_TRACING(TRACE_DEBUG
, userdb__dumpGroup(gr
); )
656 userdb__addToMap(&userdb__groups
, gr
->gr_name
, gr
->gr_gid
, gr
);
662 /* --- @userdb_yp@ --- *
668 * Use: Fetches the YP database of users.
675 /* --- Bind to a server --- */
677 if (yp_get_default_domain(&ypdom
) ||
681 T( trace(TRACE_DEBUG
, "debug: adding NIS users"); )
683 /* --- Fetch the users map --- */
686 static struct ypall_callback ucb
= { userdb__foreachUser
, 0 };
687 yp_all(ypdom
, "passwd.byuid", &ucb
);
690 /* --- Fetch the groups map --- */
693 static struct ypall_callback gcb
= { userdb__foreachGroup
, 0 };
694 yp_all(ypdom
, "group.bygid", &gcb
);
702 void userdb_yp(void) { ; }
706 /*----- Building the databases --------------------------------------------*/
708 /* --- @userdb_local@ --- *
714 * Use: Reads the local list of users into the maps.
717 void userdb_local(void)
719 T( trace(TRACE_DEBUG
, "debug: adding local users"); )
721 /* --- Fetch users first --- */
727 while ((pw
= getpwent()) != 0) {
728 IF_TRACING(TRACE_DEBUG
, userdb__dumpUser(pw
); )
729 if (!userdb__byName(&userdb__users
, pw
->pw_name
))
730 userdb__addToMap(&userdb__users
, pw
->pw_name
, pw
->pw_uid
,
731 userdb_copyUser(pw
));
736 /* --- Then fetch groups --- */
742 while ((gr
= getgrent()) != 0) {
743 IF_TRACING(TRACE_DEBUG
, userdb__dumpGroup(gr
); )
744 if (!userdb__byName(&userdb__groups
, gr
->gr_name
))
745 userdb__addToMap(&userdb__groups
, gr
->gr_name
, gr
->gr_gid
,
746 userdb_copyGroup(gr
));
752 /* --- @userdb_init@ --- *
758 * Use: Initialises the user database.
761 void userdb_init(void)
763 userdb__createMap(&userdb__users
);
764 userdb__createMap(&userdb__groups
);
767 /* --- @userdb_reinit@ --- *
773 * Use: Reinitialises the user database.
776 void userdb_reinit(void)
778 userdb__clearMap(&userdb__users
, userdb_freeUser
);
779 userdb__clearMap(&userdb__groups
, userdb_freeGroup
);
783 /*----- Test rig ----------------------------------------------------------*/
787 void dumpit(const char *msg
)
789 trace(TRACE_DEBUG
, "debug: %s", msg
);
793 for (userdb_iterateUsers(); (pw
= userdb_nextUser()) != 0; )
794 userdb__dumpUser(pw
);
799 for (userdb_iterateGroups(); (gr
= userdb_nextGroup()) != 0; )
800 userdb__dumpGroup(gr
);
807 traceon(stderr
, TRACE_ALL
);
819 /*----- That's all, folks -------------------------------------------------*/