3 * $Id: userdb.c,v 1.7 1998/04/23 13:27:46 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.7 1998/04/23 13:27:46 mdw
33 * Switch to using the ypstuff interface to YP server.
35 * Revision 1.6 1998/01/12 16:46:33 mdw
38 * Revision 1.5 1997/09/17 10:24:08 mdw
39 * Use `uid_t' instead of `int' for uids and gids. Not quite sure why I
40 * didn't do this before.
42 * Revision 1.4 1997/08/20 16:24:58 mdw
43 * Patch memory leak. Rename `userdb_reinit' to `userdb_end' for more
46 * Revision 1.3 1997/08/07 09:44:29 mdw
47 * Read NIS-based passwords from the YP server directly, rather than using
48 * `popen(ypcat)', which is probably both slower and less secure.
50 * Revision 1.2 1997/08/04 10:24:26 mdw
51 * Sources placed under CVS control.
53 * Revision 1.1 1997/07/21 13:47:43 mdw
58 /*----- Header files ------------------------------------------------------*/
60 /* --- ANSI headers --- */
68 /* --- Unix headers --- */
72 #include <sys/types.h>
78 /* --- Local headers --- */
86 /*----- Type definitions --------------------------------------------------*/
88 /* --- A map link --- */
90 typedef struct userdb__node
{
91 struct userdb__node
*next
;
95 /* --- A reference to a real record --- */
97 typedef struct userdb__sym
{
102 /* --- A name- and number-mapping --- */
104 typedef struct userdb__map
{
110 /*----- Static variables --------------------------------------------------*/
112 static userdb__map userdb__users
; /* Map of user info blocks */
113 static sym_iter userdb__useri
; /* Iterator for users */
114 static userdb__map userdb__groups
; /* Map of group info blocks */
115 static sym_iter userdb__groupi
; /* Iterator for groups */
117 /*----- Map management functions ------------------------------------------*/
119 /* --- @userdb__createMap@ --- *
121 * Arguments: @userdb__map *m@ = pointer to a map block
125 * Use: Initialises a map table.
128 static void userdb__createMap(userdb__map
*m
)
130 sym_createTable(&m
->nmap
);
131 sym_createTable(&m
->idmap
);
135 /* --- @userdb__addToMap@ --- *
137 * Arguments: @userdb__map *m@ = pointer to the map block
138 * @const char *name@ = pointer to the item's name
139 * @uid_t id@ = the item's id number
140 * @void *rec@ = pointer to the actual record
144 * Use: Adds an item to the given map.
147 static void userdb__addToMap(userdb__map
*m
,
155 s
= sym_find(&m
->nmap
, name
, -1, sizeof(*s
), &f
);
159 s
= sym_find(&m
->idmap
, (char *)&id
, sizeof(id
), sizeof(*s
), &f
);
163 n
= xmalloc(sizeof(*n
));
169 /* --- @userdb__byName@ --- *
171 * Arguments: @userdb__map *m@ = pointer to a map block
172 * @const char *name@ = name to look up
174 * Returns: A pointer to the appropriate block, or zero if not found.
176 * Use: Looks up a name in a mapping and returns the result.
179 static void *userdb__byName(userdb__map
*m
, const char *name
)
181 userdb__sym
*s
= sym_find(&m
->nmap
, name
, -1, 0, 0);
182 return (s ? s
->rec
: 0);
185 /* --- @userdb__byId@ --- *
187 * Arguments: @userdb__map *m@ = pointer to a map block
188 * @uid_t id@ = id number to find
190 * Returns: A pointer to the appropriate block, or zero if not found.
192 * Use: Looks up an ID in a mapping, and returns the result.
195 static void *userdb__byId(userdb__map
*m
, uid_t id
)
197 userdb__sym
*s
= sym_find(&m
->idmap
, (char *)&id
, sizeof(id
), 0, 0);
198 return (s ? s
->rec
: 0);
201 /* --- @userdb__clearMap@ --- *
203 * Arguments: @userdb__map *m@ = pointer to a map block
204 * @void (*freerec)(void *rec)@ = pointer to a free-record proc
208 * Use: Clears a map, emptying it and releasing the memory it
212 static void userdb__clearMap(userdb__map
*m
, void (*freerec
)(void *rec
))
216 sym_destroyTable(&m
->nmap
);
217 sym_destroyTable(&m
->idmap
);
219 for (n
= m
->list
; n
; n
= t
) {
226 /*----- User and group block management -----------------------------------*/
228 /* --- @userdb__dumpUser@ --- *
230 * Arguments: @const struct passwd *pw@ = pointer to a user block
234 * Use: Writes a user's informationt to a stream.
239 static void userdb__dumpUser(const struct passwd
*pw
)
242 "debug: name `%s' passwd `%s' uid %i gid %i",
243 pw
->pw_name
, pw
->pw_passwd
, (int)pw
->pw_uid
, (int)pw
->pw_gid
);
245 "debug: ... gecos `%s' home `%s' shell `%s'",
246 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
251 /* --- @userdb_copyUser@ --- *
253 * Arguments: @struct passwd *pw@ = pointer to block to copy
255 * Returns: Pointer to the copy.
257 * Use: Copies a user block. The copy is `deep' so all the strings
258 * are copied too. Free the copy with @userdb_freeUser@ when
259 * you don't want it any more.
262 struct passwd
*userdb_copyUser(struct passwd
*pw
)
269 npw
= xmalloc(sizeof(*npw
));
271 npw
->pw_name
= xstrdup(pw
->pw_name
);
272 npw
->pw_passwd
= xstrdup(pw
->pw_passwd
);
273 npw
->pw_uid
= pw
->pw_uid
;
274 npw
->pw_gid
= pw
->pw_gid
;
275 npw
->pw_gecos
= xstrdup(pw
->pw_gecos
);
276 npw
->pw_dir
= xstrdup(pw
->pw_dir
);
277 npw
->pw_shell
= xstrdup(pw
->pw_shell
);
282 /* --- @userdb__buildUser@ --- *
284 * Arguments: @char *s@ = pointer to user string
286 * Returns: Pointer to a user block.
288 * Use: Converts a line from a user file into a password entry.
289 * Note that the string is corrupted by @strtok@ while it gets
293 static struct passwd
*userdb__buildUser(char *s
)
295 struct passwd
*pw
= xmalloc(sizeof(*pw
));
297 s
= strtok(s
, ":"); if (!s
) goto tidy_0
; pw
->pw_name
= xstrdup(s
);
298 s
= strtok(0, ":"); if (!s
) goto tidy_1
; pw
->pw_passwd
= xstrdup(s
);
299 s
= strtok(0, ":"); if (!s
) goto tidy_2
; pw
->pw_uid
= (uid_t
)atol(s
);
300 s
= strtok(0, ":"); if (!s
) goto tidy_2
; pw
->pw_gid
= (gid_t
)atol(s
);
301 s
= strtok(0, ":"); if (!s
) goto tidy_2
; pw
->pw_gecos
= xstrdup(s
);
302 s
= strtok(0, ":"); if (!s
) goto tidy_3
; pw
->pw_dir
= xstrdup(s
);
303 s
= strtok(0, ":"); if (!s
) goto tidy_4
; pw
->pw_shell
= xstrdup(s
);
306 /* --- Error handling --- */
322 /* --- @userdb_freeUser@ --- *
324 * Arguments: @void *rec@ = pointer to a user record
328 * Use: Frees a user record.
331 void userdb_freeUser(void *rec
)
347 /* --- @userdb__dumpGroup@ --- *
349 * Arguments: @const struct group *gr@ = pointer to a group block
350 * @FILE *fp@ = pointer to stream to write on
354 * Use: Writes a group's information to a stream.
359 static void userdb__dumpGroup(const struct group
*gr
)
364 "debug: name `%s' passwd `%s' gid %i",
365 gr
->gr_name
, gr
->gr_passwd
, (int)gr
->gr_gid
);
366 for (p
= gr
->gr_mem
; *p
; p
++)
367 trace(TRACE_DEBUG
,"debug: ... `%s'", *p
);
372 /* --- @userdb_copyGroup@ --- *
374 * Arguments: @struct group *gr@ = pointer to group block
376 * Returns: Pointer to copied block
378 * Use: Copies a group block. The copy is `deep' so all the strings
379 * are copied too. Free the copy with @userdb_freeGroup@ when
380 * you don't want it any more.
383 struct group
*userdb_copyGroup(struct group
*gr
)
391 ngr
= xmalloc(sizeof(*ngr
));
393 ngr
->gr_name
= xstrdup(gr
->gr_name
);
394 ngr
->gr_passwd
= xstrdup(gr
->gr_passwd
);
395 ngr
->gr_gid
= gr
->gr_gid
;
397 for (max
= 0; gr
->gr_mem
[max
]; max
++)
399 ngr
->gr_mem
= xmalloc((max
+ 1) * sizeof(char *));
400 for (i
= 0; i
< max
; i
++)
401 ngr
->gr_mem
[i
] = xstrdup(gr
->gr_mem
[i
]);
402 ngr
->gr_mem
[max
] = 0;
407 /* --- @userdb__buildGroup@ --- *
409 * Arguments: @char *s@ = pointer to group line string
411 * Returns: Pointer to a group block
413 * Use: Parses an entry in the groups file. The string is garbled
414 * by @strtok@ as we go.
417 static struct group
*userdb__buildGroup(char *s
)
419 struct group
*gr
= xmalloc(sizeof(*gr
));
423 /* --- Do the easy bits --- */
425 s
= strtok(s
, ":"); if (!s
) goto tidy_0
; gr
->gr_name
= xstrdup(s
);
426 s
= strtok(0, ":"); if (!s
) goto tidy_1
; gr
->gr_passwd
= xstrdup(s
);
427 s
= strtok(0, ":"); if (!s
) goto tidy_2
; gr
->gr_gid
= (gid_t
)atol(s
);
429 /* --- Find the start of the member list --- */
435 /* --- Count the number of members --- */
441 if ((p
= strpbrk(p
, ",")) == 0)
446 /* --- Allocate the block and fill it --- */
448 gr
->gr_mem
= xmalloc((i
+ 1) * sizeof(char *));
452 gr
->gr_mem
[i
++] = xstrdup(s
);
459 /* --- Various tidying-up things --- */
471 /* --- @userdb_freeGroup@ --- *
473 * Arguments: @void *rec@ = pointer to a group record
477 * Use: Frees a group record.
480 void userdb_freeGroup(void *rec
)
491 for (p
= gr
->gr_mem
; *p
; p
++)
497 /*----- Answering queries -------------------------------------------------*/
499 /* --- @userdb_userByName@, @userdb_userById@ --- *
501 * Arguments: @const char *name@ = pointer to user's name
502 * @uid_t id@ = user id to find
504 * Returns: Pointer to user block, or zero if not found.
506 * Use: Looks up a user by name or id.
509 struct passwd
*userdb_userByName(const char *name
)
510 { return (userdb__byName(&userdb__users
, name
)); }
512 struct passwd
*userdb_userById(uid_t id
)
513 { return (userdb__byId(&userdb__users
, id
)); }
515 /* --- @userdb_iterateUsers@, @userdb_iterateUsers_r@ --- *
517 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator object
521 * Use: Initialises an iteration for the user database.
524 void userdb_iterateUsers(void)
525 { userdb_iterateUsers_r(&userdb__useri
); }
527 void userdb_iterateUsers_r(userdb_iter
*i
)
528 { sym_createIter(i
, &userdb__users
.nmap
); }
530 /* --- @userdb_nextUser@, @userdb_nextUser_r@ --- *
532 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator oject
534 * Returns: Pointer to the next user block, or null.
536 * Use: Returns another user block.
539 struct passwd
*userdb_nextUser(void)
540 { return (userdb_nextUser_r(&userdb__useri
)); }
542 struct passwd
*userdb_nextUser_r(userdb_iter
*i
)
544 userdb__sym
*s
= sym_next(i
);
545 return (s ? s
->rec
: 0);
548 /* --- @userdb_groupByName@, @userdb_groupById@ --- *
550 * Arguments: @const char *name@ = pointer to group's name
551 * @gid_t id@ = group id to find
553 * Returns: Pointer to group block, or zero if not found.
555 * Use: Looks up a group by name or id.
558 struct group
*userdb_groupByName(const char *name
)
559 { return (userdb__byName(&userdb__groups
, name
)); }
561 struct group
*userdb_groupById(gid_t id
)
562 { return (userdb__byId(&userdb__groups
, id
)); }
564 /* --- @userdb_iterateGroups@, @userdb_iterateGroups_r@ --- *
566 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator object
570 * Use: Initialises an iteration for the group database.
573 void userdb_iterateGroups(void)
574 { userdb_iterateGroups_r(&userdb__groupi
); }
576 void userdb_iterateGroups_r(userdb_iter
*i
)
577 { sym_createIter(i
, &userdb__groups
.nmap
); }
579 /* --- @userdb_nextGroup@, @userdb_nextGroup_r@ --- *
581 * Arguments: @userdb_iter *i@ = pointer to a symbol table iterator oject
583 * Returns: Pointer to the next group block, or null.
585 * Use: Returns another group block.
588 struct group
*userdb_nextGroup(void)
589 { return (userdb_nextGroup_r(&userdb__groupi
)); }
591 struct group
*userdb_nextGroup_r(userdb_iter
*i
)
593 userdb__sym
*s
= sym_next(i
);
594 return (s ? s
->rec
: 0);
597 /*----- Yellow pages support ----------------------------------------------*/
601 /* --- @userdb__foreachUser@ --- *
603 * Arguments: @int st@ = YP protocol-level status code
604 * @char *k@ = address of the key for this record
605 * @int ksz@ = size of the key
606 * @char *v@ = address of the value for this record
607 * @int vsz@ = size of the value
608 * @char *data@ = pointer to some data passed to me
610 * Returns: Zero to be called again, nonzero to end the enumeration.
612 * Use: Handles an incoming user record.
615 static int userdb__foreachUser(int st
, char *k
, int ksz
,
616 char *v
, int vsz
, char *data
)
623 cv
= xmalloc(vsz
+ 1);
626 T( trace(TRACE_DEBUG
, "debug: nis string: `%s'", cv
); )
627 pw
= userdb__buildUser(cv
);
628 if (pw
&& !userdb__byName(&userdb__users
, pw
->pw_name
)) {
629 IF_TRACING(TRACE_DEBUG
, userdb__dumpUser(pw
); )
630 userdb__addToMap(&userdb__users
, pw
->pw_name
, pw
->pw_uid
, pw
);
637 /* --- @userdb__foreachGroup@ --- *
639 * Arguments: @int st@ = YP protocol-level status code
640 * @char *k@ = address of the key for this record
641 * @int ksz@ = size of the key
642 * @char *v@ = address of the value for this record
643 * @int vsz@ = size of the value
644 * @char *data@ = pointer to some data passed to me
646 * Returns: Zero to be called again, nonzero to end the enumeration.
648 * Use: Handles an incoming user record.
651 static int userdb__foreachGroup(int st
, char *k
, int ksz
,
652 char *v
, int vsz
, char *data
)
659 cv
= xmalloc(vsz
+ 1);
662 T( trace(TRACE_DEBUG
, "debug: nis string: `%s'", cv
); )
663 gr
= userdb__buildGroup(cv
);
664 if (gr
&& !userdb__byName(&userdb__groups
, gr
->gr_name
)) {
665 IF_TRACING(TRACE_DEBUG
, userdb__dumpGroup(gr
); )
666 userdb__addToMap(&userdb__groups
, gr
->gr_name
, gr
->gr_gid
, gr
);
668 userdb_freeGroup(gr
);
673 /* --- @userdb_yp@ --- *
679 * Use: Fetches the YP database of users.
684 /* --- Bind to a server --- */
690 T( trace(TRACE_DEBUG
, "debug: adding NIS users"); )
692 /* --- Fetch the users map --- */
695 static struct ypall_callback ucb
= { userdb__foreachUser
, 0 };
696 yp_all(yp_domain
, "passwd.byuid", &ucb
);
699 /* --- Fetch the groups map --- */
702 static struct ypall_callback gcb
= { userdb__foreachGroup
, 0 };
703 yp_all(yp_domain
, "group.bygid", &gcb
);
709 void userdb_yp(void) { ; }
713 /*----- Building the databases --------------------------------------------*/
715 /* --- @userdb_local@ --- *
721 * Use: Reads the local list of users into the maps.
724 void userdb_local(void)
726 T( trace(TRACE_DEBUG
, "debug: adding local users"); )
728 /* --- Fetch users first --- */
734 while ((pw
= getpwent()) != 0) {
735 IF_TRACING(TRACE_DEBUG
, userdb__dumpUser(pw
); )
736 if (!userdb__byName(&userdb__users
, pw
->pw_name
))
737 userdb__addToMap(&userdb__users
, pw
->pw_name
, pw
->pw_uid
,
738 userdb_copyUser(pw
));
743 /* --- Then fetch groups --- */
749 while ((gr
= getgrent()) != 0) {
750 IF_TRACING(TRACE_DEBUG
, userdb__dumpGroup(gr
); )
751 if (!userdb__byName(&userdb__groups
, gr
->gr_name
))
752 userdb__addToMap(&userdb__groups
, gr
->gr_name
, gr
->gr_gid
,
753 userdb_copyGroup(gr
));
759 /* --- @userdb_init@ --- *
765 * Use: Initialises the user database.
768 void userdb_init(void)
770 userdb__createMap(&userdb__users
);
771 userdb__createMap(&userdb__groups
);
774 /* --- @userdb_end@ --- *
780 * Use: Closes down the user database.
783 void userdb_end(void)
785 userdb__clearMap(&userdb__users
, userdb_freeUser
);
786 userdb__clearMap(&userdb__groups
, userdb_freeGroup
);
789 /*----- Test rig ----------------------------------------------------------*/
793 void dumpit(const char *msg
)
795 trace(TRACE_DEBUG
, "debug: %s", msg
);
799 for (userdb_iterateUsers(); (pw
= userdb_nextUser()) != 0; )
800 userdb__dumpUser(pw
);
805 for (userdb_iterateGroups(); (gr
= userdb_nextGroup()) != 0; )
806 userdb__dumpGroup(gr
);
813 traceon(stdout
, TRACE_ALL
);
818 /* printf("loaded (%lu)\n", track_memused()); */
822 /* printf("cleared (%lu)\n", track_memused()); */
823 /* track_memlist(); */
827 /* printf("reloaded (%lu)\n", track_memused()); */
835 /*----- That's all, folks -------------------------------------------------*/