Use `uid_t' instead of `int' for uids and gids. Not quite sure why I
[become] / src / userdb.c
index 77e1a3d..eb963d0 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: userdb.c,v 1.2 1997/08/04 10:24:26 mdw Exp $
+ * $Id: userdb.c,v 1.5 1997/09/17 10:24:08 mdw Exp $
  *
  * User database management
  *
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: userdb.c,v $
+ * Revision 1.5  1997/09/17 10:24:08  mdw
+ * Use `uid_t' instead of `int' for uids and gids.  Not quite sure why I
+ * didn't do this before.
+ *
+ * Revision 1.4  1997/08/20  16:24:58  mdw
+ * Patch memory leak.  Rename `userdb_reinit' to `userdb_end' for more
+ * sensible restart.
+ *
+ * Revision 1.3  1997/08/07 09:44:29  mdw
+ * Read NIS-based passwords from the YP server directly, rather than using
+ * `popen(ypcat)', which is probably both slower and less secure.
+ *
  * Revision 1.2  1997/08/04 10:24:26  mdw
  * Sources placed under CVS control.
  *
@@ -54,6 +66,7 @@
 #include <sys/types.h>
 
 #ifdef HAVE_YP
+#  include <rpc/rpc.h>
 #  include <rpcsvc/ypclnt.h>
 #  include <rpcsvc/yp_prot.h>
 #endif
@@ -64,6 +77,7 @@
 
 /* --- Local headers --- */
 
+#include "become.h"
 #include "sym.h"
 #include "userdb.h"
 #include "utils.h"
@@ -121,7 +135,7 @@ static void userdb__createMap(userdb__map *m)
  *
  * Arguments:  @userdb__map *m@ = pointer to the map block
  *             @const char *name@ = pointer to the item's name
- *             @int id@ = the item's id number
+ *             @uid_t id@ = the item's id number
  *             @void *rec@ = pointer to the actual record
  *
  * Returns:    ---
@@ -131,7 +145,7 @@ static void userdb__createMap(userdb__map *m)
 
 static void userdb__addToMap(userdb__map *m,
                             const char *name,
-                            int id, void *rec)
+                            uid_t id, void *rec)
 {
   unsigned f;
   userdb__sym *s;
@@ -170,14 +184,14 @@ static void *userdb__byName(userdb__map *m, const char *name)
 /* --- @userdb__byId@ --- *
  *
  * Arguments:  @userdb__map *m@ = pointer to a map block
- *             @int id@ = id number to find
+ *             @uid_t id@ = id number to find
  *
  * Returns:    A pointer to the appropriate block, or zero if not found.
  *
  * Use:                Looks up an ID in a mapping, and returns the result.
  */
 
-static void *userdb__byId(userdb__map *m, int id)
+static void *userdb__byId(userdb__map *m, uid_t id)
 {
   userdb__sym *s = sym_find(&m->idmap, (char *)&id, sizeof(id), 0, 0);
   return (s ? s->rec : 0);
@@ -213,33 +227,24 @@ static void userdb__clearMap(userdb__map *m, void (*freerec)(void *rec))
 /* --- @userdb__dumpUser@ --- *
  *
  * Arguments:  @const struct passwd *pw@ = pointer to a user block
- *             @FILE *fp@ = pointer to stream to write on
  *
  * Returns:    ---
  *
  * Use:                Writes a user's informationt to a stream.
  */
 
-#ifndef NDEBUG
+#ifdef TRACING
 
-static void userdb__dumpUser(const struct passwd *pw, FILE *fp)
+static void userdb__dumpUser(const struct passwd *pw)
 {
-  printf("\n"
-        "***   name == %s\n"
-        "*** passwd == %s\n"
-        "***    uid == %i\n"
-        "***    gid == %i\n"
-        "***  gecos == %s\n"
-        "***   home == %s\n"
-        "***  shell == %s\n",
-        pw->pw_name, pw->pw_passwd, (int)pw->pw_uid, (int)pw->pw_gid,
-        pw->pw_gecos, pw->pw_dir, pw->pw_shell);
+  trace(TRACE_DEBUG,
+       "debug: name `%s' passwd `%s' uid %i gid %i",
+       pw->pw_name, pw->pw_passwd, (int)pw->pw_uid, (int)pw->pw_gid);
+  trace(TRACE_DEBUG,
+       "debug: ... gecos `%s' home `%s' shell `%s'",
+       pw->pw_gecos, pw->pw_dir, pw->pw_shell);
 }
 
-#else
-
-#define userdb__dumpUser(pw, fp) ((void)0)
-
 #endif
 
 /* --- @userdb_copyUser@ --- *
@@ -290,8 +295,8 @@ static struct passwd *userdb__buildUser(char *s)
 
   s = strtok(s, ":"); if (!s) goto tidy_0; pw->pw_name = xstrdup(s);
   s = strtok(0, ":"); if (!s) goto tidy_1; pw->pw_passwd = xstrdup(s);
-  s = strtok(0, ":"); if (!s) goto tidy_2; pw->pw_uid = atoi(s);
-  s = strtok(0, ":"); if (!s) goto tidy_2; pw->pw_gid = atoi(s);
+  s = strtok(0, ":"); if (!s) goto tidy_2; pw->pw_uid = (uid_t)atol(s);
+  s = strtok(0, ":"); if (!s) goto tidy_2; pw->pw_gid = (gid_t)atol(s);
   s = strtok(0, ":"); if (!s) goto tidy_2; pw->pw_gecos = xstrdup(s);
   s = strtok(0, ":"); if (!s) goto tidy_3; pw->pw_dir = xstrdup(s);
   s = strtok(0, ":"); if (!s) goto tidy_4; pw->pw_shell = xstrdup(s);
@@ -348,26 +353,19 @@ void userdb_freeUser(void *rec)
  * Use:                Writes a group's information to a stream.
  */
 
-#ifndef NDEBUG
+#ifdef TRACING
 
-static void userdb__dumpGroup(const struct group *gr, FILE *fp)
+static void userdb__dumpGroup(const struct group *gr)
 {
   char *const *p;
 
-  printf("\n"
-        "***   name == %s\n"
-        "*** passwd == %s\n"
-        "***    gid == %i\n"
-        "*** members...\n",
+  trace(TRACE_DEBUG,
+        "debug: name `%s' passwd `%s' gid %i",
         gr->gr_name, gr->gr_passwd, (int)gr->gr_gid);
   for (p = gr->gr_mem; *p; p++)
-    printf("***   %s\n", *p);
+    trace(TRACE_DEBUG,"debug: ... `%s'", *p);
 }
 
-#else
-
-#define userdb__dumpUser(pw, fp) ((void)0)
-
 #endif
 
 /* --- @userdb_copyGroup@ --- *
@@ -425,7 +423,7 @@ static struct group *userdb__buildGroup(char *s)
 
   s = strtok(s, ":"); if (!s) goto tidy_0; gr->gr_name = xstrdup(s);
   s = strtok(0, ":"); if (!s) goto tidy_1; gr->gr_passwd = xstrdup(s);
-  s = strtok(0, ":"); if (!s) goto tidy_2; gr->gr_gid = atoi(s);
+  s = strtok(0, ":"); if (!s) goto tidy_2; gr->gr_gid = (gid_t)atol(s);
 
   /* --- Find the start of the member list --- */
 
@@ -624,9 +622,13 @@ static int userdb__foreachUser(int st, char *k, int ksz,
   cv = xmalloc(vsz + 1);
   memcpy(cv, v, vsz);
   cv[vsz] = 0;
+  T( trace(TRACE_DEBUG, "debug: nis string: `%s'", cv); )
   pw = userdb__buildUser(cv);
-  if (pw && !userdb__byName(&userdb__users, pw->pw_name))
+  if (pw && !userdb__byName(&userdb__users, pw->pw_name)) {
+    IF_TRACING(TRACE_DEBUG, userdb__dumpUser(pw); )
     userdb__addToMap(&userdb__users, pw->pw_name, pw->pw_uid, pw);
+  } else
+    userdb_freeUser(pw);
   free(cv);
   return (0);
 }
@@ -656,9 +658,13 @@ static int userdb__foreachGroup(int st, char *k, int ksz,
   cv = xmalloc(vsz + 1);
   memcpy(cv, v, vsz);
   cv[vsz] = 0;
+  T( trace(TRACE_DEBUG, "debug: nis string: `%s'", cv); )
   gr = userdb__buildGroup(cv);
-  if (gr && !userdb__byName(&userdb__groups, gr->gr_name))
+  if (gr && !userdb__byName(&userdb__groups, gr->gr_name)) {
+    IF_TRACING(TRACE_DEBUG, userdb__dumpGroup(gr); )
     userdb__addToMap(&userdb__groups, gr->gr_name, gr->gr_gid, gr);
+  } else
+    userdb_freeGroup(gr);
   free(cv);
   return (0);
 }
@@ -682,6 +688,8 @@ void userdb_yp(void)
       yp_bind(ypdom))
     return;
 
+  T( trace(TRACE_DEBUG, "debug: adding NIS users"); )
+
   /* --- Fetch the users map --- */
 
   {
@@ -697,7 +705,6 @@ void userdb_yp(void)
   }
 
   yp_unbind(ypdom);
-  free(ypdom);
 }
 
 #else
@@ -719,7 +726,7 @@ void userdb_yp(void) { ; }
 
 void userdb_local(void)
 {
-  D( printf("adding local users...\n"); )
+  T( trace(TRACE_DEBUG, "debug: adding local users"); )
 
   /* --- Fetch users first --- */
 
@@ -728,7 +735,7 @@ void userdb_local(void)
 
     setpwent();
     while ((pw = getpwent()) != 0) {
-      D( userdb__dumpUser(pw, stdout); )
+      IF_TRACING(TRACE_DEBUG, userdb__dumpUser(pw); )
       if (!userdb__byName(&userdb__users, pw->pw_name))
        userdb__addToMap(&userdb__users, pw->pw_name, pw->pw_uid,
                         userdb_copyUser(pw));
@@ -743,7 +750,7 @@ void userdb_local(void)
 
     setgrent();
     while ((gr = getgrent()) != 0) {
-      D( userdb__dumpGroup(gr, stdout); )
+      IF_TRACING(TRACE_DEBUG, userdb__dumpGroup(gr); )
       if (!userdb__byName(&userdb__groups, gr->gr_name))
        userdb__addToMap(&userdb__groups, gr->gr_name, gr->gr_gid,
                         userdb_copyGroup(gr));
@@ -767,20 +774,19 @@ void userdb_init(void)
   userdb__createMap(&userdb__groups);
 }
 
-/* --- @userdb_reinit@ --- *
+/* --- @userdb_end@ --- *
  *
  * Arguments:  ---
  *
  * Returns:    ---
  *
- * Use:                Reinitialises the user database.
+ * Use:                Closes down the user database.
  */
 
-void userdb_reinit(void)
+void userdb_end(void)
 {
   userdb__clearMap(&userdb__users, userdb_freeUser);
   userdb__clearMap(&userdb__groups, userdb_freeGroup);
-  userdb_init();
 }
 
 /*----- Test rig ----------------------------------------------------------*/
@@ -789,29 +795,40 @@ void userdb_reinit(void)
 
 void dumpit(const char *msg)
 {
-  printf("\n\n$$$ %s\n", msg);
+  trace(TRACE_DEBUG, "debug: %s", msg);
 
   {
     struct passwd *pw;
     for (userdb_iterateUsers(); (pw = userdb_nextUser()) != 0; )
-      userdb__dumpUser(pw, stdout);
+      userdb__dumpUser(pw);
   }
 
   {
     struct group *gr;
     for (userdb_iterateGroups(); (gr = userdb_nextGroup()) != 0; )
-      userdb__dumpGroup(gr, stdout);
+      userdb__dumpGroup(gr);
   }
 }
 
 int main(void)
 {
+  ego("userdb-test");
+/*   traceon(stdout, TRACE_ALL); */
   userdb_init();
-  dumpit("cleared");
   userdb_local();
-  dumpit("local");
   userdb_yp();
-  dumpit("yp");
+  printf("loaded (%lu)\n", track_memused());
+  getchar();
+  for (;;) {
+    userdb_end();
+    printf("cleared (%lu)\n", track_memused());
+/*    track_memlist(); */
+    userdb_init();
+    userdb_local();
+    userdb_yp();
+    printf("reloaded (%lu)\n", track_memused());
+    getchar();
+  }
   return (0);
 }