summaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2007-05-21 14:48:18 +0200
committerDavid Härdeman <david@hardeman.nu>2007-05-21 14:48:18 +0200
commit36d91ba2fe1ecc8120a252c12c19729519bdb078 (patch)
tree3437dfc9dd5056c84215b44d766086c9087a66fa /utils.c
parent201f1e4dfd09f41cce99182294824af0274898ae (diff)
Add caching uid/gid lookup functions
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c119
1 files changed, 119 insertions, 0 deletions
diff --git a/utils.c b/utils.c
index 7b8e784..aa6df96 100644
--- a/utils.c
+++ b/utils.c
@@ -26,6 +26,9 @@
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
+#include <sys/types.h>
+#include <grp.h>
+#include <pwd.h>
#include "utils.h"
@@ -182,3 +185,119 @@ read_string(char **from, const char *max)
return read_binary_string(from, strlen(*from) + 1, max);
}
+/* For group caching */
+static struct group *gtable = NULL;
+
+/* Initial setup of the gid table */
+static void
+create_group_table()
+{
+ struct group *tmp;
+ int count, index;
+
+ for (count = 0; getgrent(); count++) /* Do nothing */;
+
+ gtable = xmalloc(sizeof(struct group) * (count + 1));
+ memset(gtable, 0, sizeof(struct group) * (count + 1));
+ setgrent();
+
+ for (index = 0; (tmp = getgrent()) && index < count; index++) {
+ gtable[index].gr_gid = tmp->gr_gid;
+ gtable[index].gr_name = xstrdup(tmp->gr_name);
+ }
+
+ endgrent();
+}
+
+/* Caching version of getgrnam */
+struct group *
+xgetgrnam(const char *name)
+{
+ int i;
+
+ if (!gtable)
+ create_group_table();
+
+ for (i = 0; gtable[i].gr_name; i++) {
+ if (!strcmp(name, gtable[i].gr_name))
+ return &(gtable[i]);
+ }
+
+ return NULL;
+}
+
+/* Caching version of getgrgid */
+struct group *
+xgetgrgid(gid_t gid)
+{
+ int i;
+
+ if (!gtable)
+ create_group_table();
+
+ for (i = 0; gtable[i].gr_name; i++) {
+ if (gtable[i].gr_gid == gid)
+ return &(gtable[i]);
+ }
+
+ return NULL;
+}
+
+/* For user caching */
+static struct passwd *ptable = NULL;
+
+/* Initial setup of the passwd table */
+static void
+create_passwd_table()
+{
+ struct passwd *tmp;
+ int count, index;
+
+ for (count = 0; getpwent(); count++) /* Do nothing */;
+
+ ptable = xmalloc(sizeof(struct passwd) * (count + 1));
+ memset(ptable, 0, sizeof(struct passwd) * (count + 1));
+ setpwent();
+
+ for (index = 0; (tmp = getpwent()) && index < count; index++) {
+ ptable[index].pw_uid = tmp->pw_uid;
+ ptable[index].pw_name = xstrdup(tmp->pw_name);
+ }
+
+ endpwent();
+}
+
+/* Caching version of getpwnam */
+struct passwd *
+xgetpwnam(const char *name)
+{
+ int i;
+
+ if (!ptable)
+ create_passwd_table();
+
+ for (i = 0; ptable[i].pw_name; i++) {
+ if (!strcmp(name, ptable[i].pw_name))
+ return &(ptable[i]);
+ }
+
+ return NULL;
+}
+
+/* Caching version of getpwuid */
+struct passwd *
+xgetpwuid(uid_t uid)
+{
+ int i;
+
+ if (!ptable)
+ create_passwd_table();
+
+ for (i = 0; ptable[i].pw_name; i++) {
+ if (ptable[i].pw_uid == uid)
+ return &(ptable[i]);
+ }
+
+ return NULL;
+}
+