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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/*
SSSD
Create uid table
Authors:
Sumit Bose <sbose@redhat.com>
Copyright (C) 2009 Red Hat
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <talloc.h>
#include <ctype.h>
#include <sys/time.h>
#include "dhash.h"
#include "util/util.h"
#define INITIAL_TABLE_SIZE 64
#define PATHLEN (NAME_MAX + 14)
#define BUFSIZE 4096
static void *hash_talloc(const size_t size, void *pvt)
{
return talloc_size(pvt, size);
}
static void hash_talloc_free(void *ptr, void *pvt)
{
talloc_free(ptr);
}
static errno_t get_uid_from_pid(const pid_t pid, uid_t *uid)
{
int ret;
char path[PATHLEN];
struct stat stat_buf;
int fd;
char buf[BUFSIZE];
char *p;
char *e;
char *endptr;
long num=0;
ret = snprintf(path, PATHLEN, "/proc/%d/status", pid);
if (ret < 0) {
DEBUG(1, ("snprintf failed"));
return EINVAL;
} else if (ret >= PATHLEN) {
DEBUG(1, ("path too long?!?!\n"));
return EINVAL;
}
ret = lstat(path, &stat_buf);
if (ret == -1) {
if (errno == ENOENT) {
DEBUG(7, ("Proc file [%s] is not available anymore, continuing.\n",
path));
return EOK;
}
DEBUG(1, ("lstat failed [%d][%s].\n", errno, strerror(errno)));
return errno;
}
if (!S_ISREG(stat_buf.st_mode)) {
DEBUG(1, ("not a regular file\n"));
return EINVAL;
}
fd = open(path, O_RDONLY);
if (fd == -1) {
if (errno == ENOENT) {
DEBUG(7, ("Proc file [%s] is not available anymore, continuing.\n",
path));
return EOK;
}
DEBUG(1, ("open failed [%d][%s].\n", errno, strerror(errno)));
return errno;
}
ret = read(fd, buf, BUFSIZE);
if (ret == -1) {
DEBUG(1, ("read failed [%d][%s].\n", errno, strerror(errno)));
return errno;
}
ret = close(fd);
if (ret == -1) {
DEBUG(1, ("close failed [%d][%s].\n", errno, strerror(errno)));
}
p = strstr(buf, "\nUid:\t");
if (p != NULL) {
p += 6;
e = strchr(p,'\t');
if (e == NULL) {
DEBUG(1, ("missing delimiter.\n"));
return EINVAL;
} else {
*e = '\0';
}
num = strtol(p, &endptr, 10);
if(errno == ERANGE) {
DEBUG(1, ("strtol failed [%s].\n", strerror(errno)));
return errno;
}
if (*endptr != '\0') {
DEBUG(1, ("uid contains extra characters\n"));
return EINVAL;
}
if (num < 0 || num >= INT_MAX) {
DEBUG(1, ("uid out of range.\n"));
return ERANGE;
}
} else {
DEBUG(1, ("format error\n"));
return EINVAL;
}
*uid = num;
return EOK;
}
static errno_t name_to_pid(const char *name, pid_t *pid)
{
long num;
char *endptr;
errno = 0;
num = strtol(name, &endptr, 10);
if(errno == ERANGE) {
perror("strtol");
return errno;
}
if (*endptr != '\0') {
DEBUG(1, ("pid string contains extra characters.\n"));
return EINVAL;
}
if (num <= 0 || num >= INT_MAX) {
DEBUG(1, ("pid out of range.\n"));
return ERANGE;
}
*pid = num;
return EOK;
}
static int only_numbers(char *p)
{
while(*p!='\0' && isdigit(*p)) ++p;
return *p;
}
static errno_t get_active_uid_linux(hash_table_t *table, uid_t search_uid)
{
DIR *proc_dir = NULL;
struct dirent *dirent;
int ret;
pid_t pid = -1;
uid_t uid;
hash_key_t key;
hash_value_t value;
proc_dir = opendir("/proc");
if (proc_dir == NULL) {
DEBUG(1, ("Cannot open proc dir.\n"));
ret = errno;
goto done;
};
errno = 0;
while ((dirent = readdir(proc_dir)) != NULL) {
if (only_numbers(dirent->d_name) != 0) continue;
ret = name_to_pid(dirent->d_name, &pid);
if (ret != EOK) {
DEBUG(1, ("name_to_pid failed.\n"));
goto done;
}
ret = get_uid_from_pid(pid, &uid);
if (ret != EOK) {
DEBUG(1, ("get_uid_from_pid failed.\n"));
goto done;
}
if (table != NULL) {
key.type = HASH_KEY_ULONG;
key.ul = (unsigned long) uid;
value.type = HASH_VALUE_ULONG;
value.ul = (unsigned long) uid;
ret = hash_enter(table, &key, &value);
if (ret != HASH_SUCCESS) {
DEBUG(1, ("cannot add to table [%s]\n", hash_error_string(ret)));
ret = ENOMEM;
goto done;
}
} else {
if (uid == search_uid) {
ret = EOK;
goto done;
}
}
errno = 0;
}
if (errno != 0 && dirent == NULL) {
DEBUG(1 ,("readdir failed.\n"));
ret = errno;
goto done;
}
ret = closedir(proc_dir);
proc_dir = NULL;
if (ret == -1) {
DEBUG(1 ,("closedir failed, watch out.\n"));
}
if (table != NULL) {
ret = EOK;
} else {
ret = ENOENT;
}
done:
if (proc_dir != NULL) closedir(proc_dir);
return ret;
}
errno_t get_uid_table(TALLOC_CTX *mem_ctx, hash_table_t **table)
{
#ifdef __linux__
int ret;
ret = hash_create_ex(INITIAL_TABLE_SIZE, table, 0, 0, 0, 0,
hash_talloc, hash_talloc_free, mem_ctx,
NULL, NULL);
if (ret != HASH_SUCCESS) {
DEBUG(1, ("hash_create_ex failed [%s]\n", hash_error_string(ret)));
return ENOMEM;
}
return get_active_uid_linux(*table, 0);
#else
return ENOSYS;
#endif
}
errno_t check_if_uid_is_active(uid_t uid, bool *result)
{
int ret;
ret = get_active_uid_linux(NULL, uid);
if (ret != EOK && ret != ENOENT) {
DEBUG(1, ("get_uid_table failed.\n"));
return ret;
}
if (ret == EOK) {
*result = true;
} else {
*result = false;
}
return EOK;
}
|