Lines 58-63
static void hash_talloc_free(void *ptr, void *pvt)
Link Here
|
58 |
talloc_free(ptr); |
58 |
talloc_free(ptr); |
59 |
} |
59 |
} |
60 |
|
60 |
|
|
|
61 |
static int parse_procfs_linux(const char* buf, uid_t *uid, bool *is_systemd) |
62 |
{ |
63 |
char *p; |
64 |
char *e; |
65 |
char *endptr; |
66 |
uint32_t num=0; |
67 |
errno_t error=EOK; |
68 |
|
69 |
/* Get uid */ |
70 |
p = strstr(buf, "\nUid:\t"); |
71 |
if (p != NULL) { |
72 |
p += 6; |
73 |
e = strchr(p,'\t'); |
74 |
if (e == NULL) { |
75 |
DEBUG(SSSDBG_CRIT_FAILURE, "missing delimiter.\n"); |
76 |
return EINVAL; |
77 |
} else { |
78 |
*e = '\0'; |
79 |
} |
80 |
num = (uint32_t) strtoint32(p, &endptr, 10); |
81 |
error = errno; |
82 |
if (error != 0) { |
83 |
DEBUG(SSSDBG_CRIT_FAILURE, |
84 |
"strtol failed [%s].\n", strerror(error)); |
85 |
return error; |
86 |
} |
87 |
if (*endptr != '\0') { |
88 |
DEBUG(SSSDBG_CRIT_FAILURE, "uid contains extra characters\n"); |
89 |
return EINVAL; |
90 |
} |
91 |
|
92 |
} else { |
93 |
DEBUG(SSSDBG_CRIT_FAILURE, "format error\n"); |
94 |
return EINVAL; |
95 |
} |
96 |
|
97 |
/* Get process name. */ |
98 |
p = strstr(buf, "Name:\t"); |
99 |
if (p == NULL) { |
100 |
DEBUG(SSSDBG_CRIT_FAILURE, "format error\n"); |
101 |
return EINVAL; |
102 |
} |
103 |
p += 6; |
104 |
e = strchr(p,'\n'); |
105 |
if (e == NULL) { |
106 |
DEBUG(SSSDBG_CRIT_FAILURE, "format error\n"); |
107 |
return EINVAL; |
108 |
} |
109 |
if (strncmp(p, "systemd", e-p) == 0 || strncmp(p, "(sd-pam)", e-p) == 0) { |
110 |
*is_systemd = true; |
111 |
} else { |
112 |
*is_systemd = false; |
113 |
} |
114 |
|
115 |
*uid = num; |
116 |
|
117 |
return error; |
118 |
} |
119 |
|
120 |
static int parse_procfs_freebsd(char* buf, uid_t *uid, bool *is_systemd) |
121 |
{ |
122 |
uint32_t field_num=0; |
123 |
errno_t error=EOK; |
124 |
char** str = &buf, *token; |
125 |
|
126 |
/* See man procfs |
127 |
nextcloud 21186 4726 110 90383 ttyv0 ctty 1718001838,183475 11,76617 2,473238 select 1001 1001 1001,1001,0,5,44,920 - |
128 |
| interesting part | |
129 |
|uid| |
130 |
*/ |
131 |
while ((token = strsep(str, " ")) != NULL && field_num < 13) { |
132 |
field_num++; |
133 |
} |
134 |
|
135 |
if (token == NULL || field_num != 13) { |
136 |
DEBUG(SSSDBG_CRIT_FAILURE, "format error %d %d\n", token, field_num); |
137 |
return EINVAL; |
138 |
} |
139 |
|
140 |
*uid = (uint32_t) strtoint32(token, NULL, 10); |
141 |
error = errno; |
142 |
if (error != 0) { |
143 |
DEBUG(SSSDBG_CRIT_FAILURE, |
144 |
"strtol failed [%s].\n", strerror(error)); |
145 |
return error; |
146 |
} |
147 |
|
148 |
*is_systemd = false; |
149 |
|
150 |
return error; |
151 |
} |
152 |
|
61 |
static errno_t get_uid_from_pid(const pid_t pid, uid_t *uid, bool *is_systemd) |
153 |
static errno_t get_uid_from_pid(const pid_t pid, uid_t *uid, bool *is_systemd) |
62 |
{ |
154 |
{ |
63 |
int ret; |
155 |
int ret; |
Lines 65-74
static errno_t get_uid_from_pid(const pid_t pid, uid_t
Link Here
|
65 |
struct stat stat_buf; |
157 |
struct stat stat_buf; |
66 |
int fd; |
158 |
int fd; |
67 |
char buf[BUFSIZE]; |
159 |
char buf[BUFSIZE]; |
68 |
char *p; |
|
|
69 |
char *e; |
70 |
char *endptr; |
71 |
uint32_t num=0; |
72 |
errno_t error; |
160 |
errno_t error; |
73 |
|
161 |
|
74 |
ret = snprintf(path, PATHLEN, "/proc/%d/status", pid); |
162 |
ret = snprintf(path, PATHLEN, "/proc/%d/status", pid); |
Lines 138-193
static errno_t get_uid_from_pid(const pid_t pid, uid_t
Link Here
|
138 |
"close failed [%d][%s].\n", error, strerror(error)); |
226 |
"close failed [%d][%s].\n", error, strerror(error)); |
139 |
} |
227 |
} |
140 |
|
228 |
|
141 |
/* Get uid */ |
229 |
#if defined(__linux__) |
142 |
p = strstr(buf, "\nUid:\t"); |
230 |
return parse_procfs_linux(buf, uid, is_systemd); |
143 |
if (p != NULL) { |
231 |
#elif defined(__FreeBSD__) |
144 |
p += 6; |
232 |
return parse_procfs_freebsd(buf, uid, is_systemd); |
145 |
e = strchr(p,'\t'); |
233 |
#else |
146 |
if (e == NULL) { |
234 |
return ENOSYS; |
147 |
DEBUG(SSSDBG_CRIT_FAILURE, "missing delimiter.\n"); |
235 |
#endif |
148 |
return EINVAL; |
|
|
149 |
} else { |
150 |
*e = '\0'; |
151 |
} |
152 |
num = (uint32_t) strtoint32(p, &endptr, 10); |
153 |
error = errno; |
154 |
if (error != 0) { |
155 |
DEBUG(SSSDBG_CRIT_FAILURE, |
156 |
"strtol failed [%s].\n", strerror(error)); |
157 |
return error; |
158 |
} |
159 |
if (*endptr != '\0') { |
160 |
DEBUG(SSSDBG_CRIT_FAILURE, "uid contains extra characters\n"); |
161 |
return EINVAL; |
162 |
} |
163 |
|
236 |
|
164 |
} else { |
|
|
165 |
DEBUG(SSSDBG_CRIT_FAILURE, "format error\n"); |
166 |
return EINVAL; |
167 |
} |
168 |
|
169 |
/* Get process name. */ |
170 |
p = strstr(buf, "Name:\t"); |
171 |
if (p == NULL) { |
172 |
DEBUG(SSSDBG_CRIT_FAILURE, "format error\n"); |
173 |
return EINVAL; |
174 |
} |
175 |
p += 6; |
176 |
e = strchr(p,'\n'); |
177 |
if (e == NULL) { |
178 |
DEBUG(SSSDBG_CRIT_FAILURE, "format error\n"); |
179 |
return EINVAL; |
180 |
} |
181 |
if (strncmp(p, "systemd", e-p) == 0 || strncmp(p, "(sd-pam)", e-p) == 0) { |
182 |
*is_systemd = true; |
183 |
} else { |
184 |
*is_systemd = false; |
185 |
} |
186 |
|
187 |
*uid = num; |
188 |
|
189 |
return EOK; |
190 |
|
191 |
fail_fd: |
237 |
fail_fd: |
192 |
close(fd); |
238 |
close(fd); |
193 |
return error; |
239 |
return error; |
Lines 212-218
static errno_t name_to_pid(const char *name, pid_t *pi
Link Here
|
212 |
return EINVAL; |
258 |
return EINVAL; |
213 |
} |
259 |
} |
214 |
|
260 |
|
|
|
261 |
/* FreeBSD has /proc/0/... */ |
262 |
#if defined(__FreeBSD__) |
263 |
if (num < 0 || num >= INT_MAX) { |
264 |
#else |
215 |
if (num <= 0 || num >= INT_MAX) { |
265 |
if (num <= 0 || num >= INT_MAX) { |
|
|
266 |
#endif |
216 |
DEBUG(SSSDBG_CRIT_FAILURE, "pid out of range.\n"); |
267 |
DEBUG(SSSDBG_CRIT_FAILURE, "pid out of range.\n"); |
217 |
return ERANGE; |
268 |
return ERANGE; |
218 |
} |
269 |
} |
Lines 228-234
static int only_numbers(char *p)
Link Here
|
228 |
return *p; |
279 |
return *p; |
229 |
} |
280 |
} |
230 |
|
281 |
|
231 |
static errno_t get_active_uid_linux(hash_table_t *table, uid_t search_uid) |
282 |
static errno_t get_active_uid_procfs(hash_table_t *table, uid_t search_uid) |
232 |
{ |
283 |
{ |
233 |
DIR *proc_dir = NULL; |
284 |
DIR *proc_dir = NULL; |
234 |
struct dirent *dirent; |
285 |
struct dirent *dirent; |
Lines 327-333
errno_t get_uid_table(TALLOC_CTX *mem_ctx, hash_table_
Link Here
|
327 |
|
378 |
|
328 |
errno_t get_uid_table(TALLOC_CTX *mem_ctx, hash_table_t **table) |
379 |
errno_t get_uid_table(TALLOC_CTX *mem_ctx, hash_table_t **table) |
329 |
{ |
380 |
{ |
330 |
#ifdef __linux__ |
381 |
#if defined(__linux__) || defined(__FreeBSD__) |
331 |
int ret; |
382 |
int ret; |
332 |
|
383 |
|
333 |
ret = hash_create_ex(0, table, 0, 0, 0, 0, |
384 |
ret = hash_create_ex(0, table, 0, 0, 0, 0, |
Lines 339-345
errno_t get_uid_table(TALLOC_CTX *mem_ctx, hash_table_
Link Here
|
339 |
return ENOMEM; |
390 |
return ENOMEM; |
340 |
} |
391 |
} |
341 |
|
392 |
|
342 |
return get_active_uid_linux(*table, 0); |
393 |
return get_active_uid_procfs(*table, 0); |
343 |
#else |
394 |
#else |
344 |
return ENOSYS; |
395 |
return ENOSYS; |
345 |
#endif |
396 |
#endif |
Lines 365-373
errno_t check_if_uid_is_active(uid_t uid, bool *result
Link Here
|
365 |
/* fall back to the old method */ |
416 |
/* fall back to the old method */ |
366 |
#endif |
417 |
#endif |
367 |
|
418 |
|
368 |
ret = get_active_uid_linux(NULL, uid); |
419 |
ret = get_active_uid_procfs(NULL, uid); |
369 |
if (ret != EOK && ret != ENOENT) { |
420 |
if (ret != EOK && ret != ENOENT) { |
370 |
DEBUG(SSSDBG_CRIT_FAILURE, "get_active_uid_linux() failed.\n"); |
421 |
DEBUG(SSSDBG_CRIT_FAILURE, "get_active_uid_procfs() failed.\n"); |
371 |
return ret; |
422 |
return ret; |
372 |
} |
423 |
} |
373 |
|
424 |
|