-
Notifications
You must be signed in to change notification settings - Fork 19
/
uid.c
377 lines (308 loc) · 8.54 KB
/
uid.c
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2011-2015, VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <config.h>
#include <SWI-Prolog.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
static int
error(int error, const char *op, const char *type, term_t culprit)
{ if ( error == EAGAIN )
return PL_resource_error("rlimit_nproc");
else if ( error == EMFILE )
return PL_resource_error("max_files");
else if ( error == ENOMEM )
return PL_resource_error("memory");
else if ( error == ERANGE )
return PL_resource_error("buffer_space");
else
return PL_permission_error(op, type, culprit);
}
static foreign_t
pl_getuid(term_t uid)
{ return PL_unify_integer(uid, getuid());
}
static foreign_t
pl_geteuid(term_t uid)
{ return PL_unify_integer(uid, geteuid());
}
static foreign_t
pl_getgid(term_t gid)
{ return PL_unify_integer(gid, getgid());
}
static foreign_t
pl_getegid(term_t gid)
{ return PL_unify_integer(gid, getegid());
}
static foreign_t
pl_user_info(term_t user, term_t info)
{ int uid;
struct passwd pwd, *pwdp;
char buf[1000];
char *name;
if ( PL_get_integer(user, &uid) )
{ again1:
errno = 0;
if ( getpwuid_r(uid, &pwd, buf, sizeof(buf), &pwdp) != 0 )
{ if ( errno == EINTR )
{ if ( PL_handle_signals() < 0 )
return FALSE;
goto again1;
}
return error(errno, "info", "user", user);
}
} else if ( PL_get_chars(user, &name, CVT_ATOMIC|REP_MB) )
{ again2:
errno = 0;
if ( getpwnam_r(name, &pwd, buf, sizeof(buf), &pwdp) != 0 )
{ if ( errno == EINTR )
{ if ( PL_handle_signals() < 0 )
return FALSE;
goto again2;
}
return error(errno, "info", "user", user);
}
} else
{ return PL_type_error("user", user);
}
if ( !pwdp )
return PL_existence_error("user", user);
return PL_unify_term(info,
PL_FUNCTOR_CHARS, "user_info", 7,
PL_MBCHARS, pwdp->pw_name,
PL_MBCHARS, pwdp->pw_passwd,
PL_INT, (int)pwdp->pw_uid,
PL_INT, (int)pwdp->pw_gid,
PL_MBCHARS, pwdp->pw_gecos,
PL_MBCHARS, pwdp->pw_dir,
PL_MBCHARS, pwdp->pw_shell
);
}
static foreign_t
pl_group_info(term_t group, term_t info)
{ int gid;
struct group grp, *pgrp;
char buf[1000];
char *name;
term_t members = PL_new_term_ref();
term_t tail = PL_copy_term_ref(members);
term_t head = PL_new_term_ref();
char **memp;
if ( PL_get_integer(group, &gid) )
{ again1:
errno = 0;
if ( getgrgid_r(gid, &grp, buf, sizeof(buf), &pgrp) != 0 )
{ if ( errno == EINTR )
{ if ( PL_handle_signals() < 0 )
return FALSE;
goto again1;
}
return error(errno, "info", "group", group);
}
} else if ( PL_get_chars(group, &name, CVT_ATOMIC|REP_MB) )
{ again2:
errno = 0;
if ( getgrnam_r(name, &grp, buf, sizeof(buf), &pgrp) != 0 )
{ if ( errno == EINTR )
{ if ( PL_handle_signals() < 0 )
return FALSE;
goto again2;
}
return error(errno, "info", "group", group);
}
} else
{ return PL_type_error("group", group);
}
if ( !pgrp )
return PL_existence_error("group", group);
for(memp=pgrp->gr_mem; *memp; memp++)
{ if ( !PL_unify_list(tail, head, tail) ||
!PL_unify_chars(head, PL_ATOM|REP_MB, -1, *memp) )
return FALSE;
}
if ( !PL_unify_nil(tail) )
return FALSE;
return PL_unify_term(info,
PL_FUNCTOR_CHARS, "group_info", 4,
PL_MBCHARS, pgrp->gr_name,
PL_MBCHARS, pgrp->gr_passwd,
PL_INT, (int)pgrp->gr_gid,
PL_TERM, members
);
}
static foreign_t
pl_setuid(term_t uid)
{ int id;
if ( !PL_get_integer_ex(uid, &id) )
return FALSE;
if ( setuid(id) == 0 )
return TRUE;
return error(errno, "setuid", "uid", uid);
}
static foreign_t
pl_setgid(term_t gid)
{ int id;
if ( !PL_get_integer_ex(gid, &id) )
return FALSE;
if ( setgid(id) == 0 )
return TRUE;
return error(errno, "setgid", "gid", gid);
}
static foreign_t
pl_seteuid(term_t uid)
{ int id;
if ( !PL_get_integer_ex(uid, &id) )
return FALSE;
if ( seteuid(id) == 0 )
return TRUE;
return error(errno, "seteuid", "uid", uid);
}
static foreign_t
pl_setegid(term_t gid)
{ int id;
if ( !PL_get_integer_ex(gid, &id) )
return FALSE;
if ( setegid(id) == 0 )
return TRUE;
return error(errno, "setegid", "gid", gid);
}
#ifdef HAVE_INITGROUPS
static foreign_t
pl_initgroups(term_t user, term_t group)
{ char *u;
int g;
if ( !PL_get_integer_ex(group, &g) ||
!PL_get_chars(user, &u, CVT_ATOMIC|REP_MB|CVT_EXCEPTION) )
return FALSE;
if ( initgroups(u, g) == 0 )
return TRUE;
return error(errno, "initgroups", "user", user);
}
#endif
static foreign_t
pl_getgroups(term_t Groups)
{ gid_t buf[32];
gid_t *list = buf;
int size = sizeof(buf)/sizeof(gid_t);
int rc;
for(;;)
{ rc = getgroups(size, list);
if ( rc == -1 && errno == EINVAL )
{ size *= 2;
gid_t *l2;
if ( list == buf )
l2 = malloc(sizeof(gid_t)*size);
else
l2 = realloc(list, sizeof(gid_t)*size);
if ( !l2 )
{ if ( list != buf )
free(list);
return PL_resource_error("memory");
}
list = l2;
} else
break;
}
if ( rc >= 0 )
{ term_t tail = PL_copy_term_ref(Groups);
term_t head = PL_new_term_ref();
int i;
for(i=0; i<rc; i++)
{ if ( !PL_unify_list(tail, head, tail) ||
!PL_unify_integer(head, list[i]) )
{ rc = FALSE;
goto out;
}
}
rc = PL_unify_nil(tail);
} else
{ rc = error(errno, "getgroups", "", Groups);
}
out:
if ( list != buf )
free(list);
return rc;
}
#ifdef HAVE_SETGROUPS
static foreign_t
pl_setgroups(term_t Groups)
{ size_t len;
if ( PL_skip_list(Groups, 0, &len) == PL_LIST )
{ gid_t *list;
int rc;
if ( (list=malloc(len*sizeof(gid_t))) )
{ term_t tail = PL_copy_term_ref(Groups);
term_t head = PL_new_term_ref();
int i = 0;
while((rc=PL_get_list_ex(tail,head,tail)))
{ int gid;
if ( (rc=PL_get_integer_ex(head, &gid)) )
list[i++] = gid;
else
goto out;
}
rc = PL_get_nil_ex(tail);
if ( rc )
{ if ( setgroups(i, list) < 0 )
rc = error(errno, "setgroups", "", Groups);
else
rc = TRUE;
}
} else
rc = PL_resource_error("memory");
out:
if ( list )
free(list);
return rc;
}
return PL_type_error("list", Groups);
}
#endif /*HAVE_SETGROUPS*/
install_t
install_uid()
{ PL_register_foreign("getuid", 1, pl_getuid, 0);
PL_register_foreign("geteuid", 1, pl_geteuid, 0);
PL_register_foreign("getgid", 1, pl_getgid, 0);
PL_register_foreign("getegid", 1, pl_getegid, 0);
PL_register_foreign("getgroups", 1, pl_getgroups, 0);
PL_register_foreign("user_info", 2, pl_user_info, 0);
PL_register_foreign("group_info", 2, pl_group_info, 0);
PL_register_foreign("setuid", 1, pl_setuid, 0);
PL_register_foreign("setgid", 1, pl_setgid, 0);
PL_register_foreign("seteuid", 1, pl_seteuid, 0);
PL_register_foreign("setegid", 1, pl_setegid, 0);
#ifdef HAVE_INITGROUPS
PL_register_foreign("initgroups", 2, pl_initgroups, 0);
#endif
#ifdef HAVE_SETGROUPS
PL_register_foreign("setgroups", 1, pl_setgroups, 0);
#endif
}