]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/idcache.c
Implement state file and save/restore last_jobs list
[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)
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       V(mutex);
48       return tail->name;
49     }
50   }
51
52   pwent = getpwuid(uid);
53   tail = (struct userid *)malloc(sizeof (struct userid));
54   tail->id.u = uid;
55 #ifndef HAVE_WIN32
56   if (pwent == 0 || strcmp(pwent->pw_name, "????????") == 0) {
57       sprintf(usernum_string, "%u", (uint32_t)uid);
58       tail->name = bstrdup(usernum_string);
59   } else {
60       tail->name = bstrdup(pwent->pw_name);
61   }
62 #else
63       sprintf(usernum_string, "%u", (uint32_t)uid);
64       tail->name = bstrdup(usernum_string);
65 #endif
66
67   /* Add to the head of the list, so most recently used is first.  */
68   tail->next = user_alist;
69   user_alist = tail;
70   V(mutex);
71   return tail->name;
72 }
73
74 void free_getuser_cache()
75 {
76   register struct userid *tail;
77
78   P(mutex);
79   for (tail = user_alist; tail; ) {
80      struct userid *otail = tail;
81      free(tail->name);
82      tail = tail->next;
83      free(otail);
84   }
85   user_alist = NULL;
86   V(mutex);
87 }
88
89
90
91 /* Translate GID to a group name or a stringified number,
92    with cache.  */
93 char *getgroup(gid_t gid)
94 {
95   register struct userid *tail;
96   struct group *grent;
97   char groupnum_string[20];
98
99   P(mutex);
100   for (tail = group_alist; tail; tail = tail->next) {
101     if (tail->id.g == gid) {
102       V(mutex);
103       return tail->name;
104     }
105   }
106
107   grent = getgrgid(gid);
108   tail = (struct userid *)malloc(sizeof (struct userid));
109   tail->id.g = gid;
110 #ifndef HAVE_WIN32
111   if (grent == 0 || strcmp(grent->gr_name, "????????") == 0) {
112       sprintf (groupnum_string, "%u", (uint32_t)gid);
113       tail->name = bstrdup(groupnum_string);
114   } else {
115       tail->name = bstrdup(grent->gr_name);
116   }
117 #else
118       sprintf (groupnum_string, "%u", (uint32_t)gid);
119       tail->name = bstrdup(groupnum_string);
120 #endif
121   /* Add to the head of the list, so most recently used is first.  */
122   tail->next = group_alist;
123   group_alist = tail;
124   V(mutex);
125   return tail->name;
126 }
127
128 void free_getgroup_cache()
129 {
130   register struct userid *tail;
131
132   P(mutex);
133   for (tail = group_alist; tail; ) {
134      struct userid *otail = tail;
135      free(tail->name);
136      tail = tail->next;
137      free(otail);
138   }
139   group_alist = NULL;
140   V(mutex);
141 }