]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/idcache.c
- Add a kludge to detect bad date/times, which cause a seg fault in
[bacula/bacula] / bacula / src / lib / idcache.c
1 /* idcache.c -- map user and group IDs, cached for speed
2    Copyright (C) 1985, 1988, 1989, 1990 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 #include "bacula.h"
19
20 struct userid {
21   union {
22       uid_t u;
23       gid_t g;
24   } id;
25   char *name;
26   struct userid *next;
27 };
28
29 static struct userid *user_alist = NULL;
30 /* Use the same struct as for userids.  */
31 static struct userid *group_alist = NULL;
32
33 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
34
35 /* Translate UID to a login name or a stringified number,
36    with cache.  */
37
38 char *getuser(uid_t uid, char *name, int len)
39 {
40   register struct userid *tail;
41   struct passwd *pwent;
42   char usernum_string[20];
43
44   P(mutex);
45   for (tail = user_alist; tail; tail = tail->next) {
46     if (tail->id.u == uid) {
47       goto uid_done;
48     }
49   }
50
51   pwent = getpwuid(uid);
52   tail = (struct userid *)malloc(sizeof (struct userid));
53   tail->id.u = uid;
54 #ifndef HAVE_WIN32
55   if (pwent == NULL || strcmp(pwent->pw_name, "????????") == 0) {
56       sprintf(usernum_string, "%u", (uint32_t)uid);
57       tail->name = bstrdup(usernum_string);
58   } else {
59       tail->name = bstrdup(pwent->pw_name);
60   }
61 #else
62       sprintf(usernum_string, "%u", (uint32_t)uid);
63       tail->name = bstrdup(usernum_string);
64 #endif
65
66   /* Add to the head of the list, so most recently used is first.  */
67   tail->next = user_alist;
68   user_alist = tail;
69
70 uid_done:
71   bstrncpy(name, tail->name, len);
72   V(mutex);
73   return name;
74 }
75
76 void free_getuser_cache()
77 {
78   register struct userid *tail;
79
80   P(mutex);
81   for (tail = user_alist; tail; ) {
82      struct userid *otail = tail;
83      free(tail->name);
84      tail = tail->next;
85      free(otail);
86   }
87   user_alist = NULL;
88   V(mutex);
89 }
90
91
92
93 /* Translate GID to a group name or a stringified number,
94    with cache.  */
95 char *getgroup(gid_t gid, char *name, int len)
96 {
97   register struct userid *tail;
98   struct group *grent;
99   char groupnum_string[20];
100
101   P(mutex);
102   for (tail = group_alist; tail; tail = tail->next) {
103     if (tail->id.g == gid) {
104       goto gid_done;
105     }
106   }
107
108   grent = getgrgid(gid);
109   tail = (struct userid *)malloc(sizeof (struct userid));
110   tail->id.g = gid;
111 #ifndef HAVE_WIN32
112   if (grent == NULL || strcmp(grent->gr_name, "????????") == 0) {
113       sprintf (groupnum_string, "%u", (uint32_t)gid);
114       tail->name = bstrdup(groupnum_string);
115   } else {
116       tail->name = bstrdup(grent->gr_name);
117   }
118 #else
119       sprintf (groupnum_string, "%u", (uint32_t)gid);
120       tail->name = bstrdup(groupnum_string);
121 #endif
122   /* Add to the head of the list, so most recently used is first.  */
123   tail->next = group_alist;
124   group_alist = tail;
125
126 gid_done:
127   bstrncpy(name, tail->name, len);
128   V(mutex);
129   return name;
130 }
131
132 void free_getgroup_cache()
133 {
134   register struct userid *tail;
135
136   P(mutex);
137   for (tail = group_alist; tail; ) {
138      struct userid *otail = tail;
139      free(tail->name);
140      tail = tail->next;
141      free(otail);
142   }
143   group_alist = NULL;
144   V(mutex);
145 }