-
Notifications
You must be signed in to change notification settings - Fork 256
/
guid_to_name.cc
177 lines (156 loc) · 4.18 KB
/
guid_to_name.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2007-2011 Kern Sibbald
Copyright (C) 2013-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Written by Kern Sibbald, July 2007 to replace idcache.c
*
* Program to convert uid and gid into names, and cache the results
* for preformance reasons.
*/
#include "include/bareos.h"
#include "lib/edit.h"
#include "lib/dlist.h"
#ifndef WIN32
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
struct guitem {
dlink<guitem> link;
char* name;
union {
uid_t uid;
gid_t gid;
};
};
guid_list* new_guid_list()
{
guid_list* list;
list = (guid_list*)malloc(sizeof(guid_list));
list->uid_list = new dlist<guitem>();
list->gid_list = new dlist<guitem>();
return list;
}
void FreeGuidList(guid_list* list)
{
guitem* item;
foreach_dlist (item, list->uid_list) {
free(item->name);
}
foreach_dlist (item, list->gid_list) {
free(item->name);
}
delete list->uid_list;
delete list->gid_list;
free(list);
}
static int UidCompare(guitem* item1, guitem* item2)
{
guitem* i1 = item1;
guitem* i2 = item2;
if (i1->uid < i2->uid) {
return -1;
} else if (i1->uid > i2->uid) {
return 1;
} else {
return 0;
}
}
static int GidCompare(guitem* item1, guitem* item2)
{
guitem* i1 = item1;
guitem* i2 = item2;
if (i1->gid < i2->gid) {
return -1;
} else if (i1->gid > i2->gid) {
return 1;
} else {
return 0;
}
}
static void GetUidname([[maybe_unused]] uid_t uid,
[[maybe_unused]] guitem* item)
{
#ifndef HAVE_WIN32
struct passwd* pwbuf;
lock_mutex(mutex);
pwbuf = getpwuid(uid);
if (pwbuf != NULL && !bstrcmp(pwbuf->pw_name, "????????")) {
item->name = strdup(pwbuf->pw_name);
}
unlock_mutex(mutex);
#endif
}
static void GetGidname([[maybe_unused]] gid_t gid,
[[maybe_unused]] guitem* item)
{
#ifndef HAVE_WIN32
struct group* grbuf;
lock_mutex(mutex);
grbuf = getgrgid(gid);
if (grbuf != NULL && !bstrcmp(grbuf->gr_name, "????????")) {
item->name = strdup(grbuf->gr_name);
}
unlock_mutex(mutex);
#endif
}
char* guid_list::uid_to_name(uid_t uid, char* name, int maxlen)
{
guitem sitem, *item, *fitem;
sitem.uid = uid;
char buf[50];
item = (guitem*)uid_list->binary_search(&sitem, UidCompare);
Dmsg2(900, "uid=%d item=%p\n", uid, item);
if (!item) {
item = (guitem*)malloc(sizeof(guitem));
item->uid = uid;
item->name = NULL;
GetUidname(uid, item);
if (!item->name) {
item->name = strdup(edit_int64(uid, buf));
Dmsg2(900, "set uid=%d name=%s\n", uid, item->name);
}
fitem = (guitem*)uid_list->binary_insert(item, UidCompare);
if (fitem != item) { /* item already there this shouldn't happen */
free(item->name);
free(item);
item = fitem;
}
}
bstrncpy(name, item->name, maxlen);
return name;
}
char* guid_list::gid_to_name(gid_t gid, char* name, int maxlen)
{
guitem sitem, *item, *fitem;
sitem.gid = gid;
char buf[50];
item = (guitem*)gid_list->binary_search(&sitem, GidCompare);
if (!item) {
item = (guitem*)malloc(sizeof(guitem));
item->gid = gid;
item->name = NULL;
GetGidname(gid, item);
if (!item->name) { item->name = strdup(edit_int64(gid, buf)); }
fitem = (guitem*)gid_list->binary_insert(item, GidCompare);
if (fitem != item) { /* item already there this shouldn't happen */
free(item->name);
free(item);
item = fitem;
}
}
bstrncpy(name, item->name, maxlen);
return name;
}