uwrap: Add mamximum for groups we can handle while forking
[uid_wrapper.git] / src / uid_wrapper.c
1 /*
2  * Copyright (c) 2009      Andrew Tridgell
3  * Copyright (c) 2011-2013 Andreas Schneider <asn@samba.org>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "config.h"
20
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <grp.h>
30 #ifdef HAVE_SYS_SYSCALL_H
31 #include <sys/syscall.h>
32 #endif
33 #ifdef HAVE_SYSCALL_H
34 #include <syscall.h>
35 #endif
36 #include <dlfcn.h>
37
38 #include <pthread.h>
39
40 #ifdef HAVE_GCC_THREAD_LOCAL_STORAGE
41 # define UWRAP_THREAD __thread
42 #else
43 # define UWRAP_THREAD
44 #endif
45
46 # define UWRAP_LOCK(m) do { \
47         pthread_mutex_lock(&( m ## _mutex)); \
48 } while(0)
49
50 # define UWRAP_UNLOCK(m) do { \
51         pthread_mutex_unlock(&( m ## _mutex)); \
52 } while(0)
53
54 /* Add new global locks here please */
55 # define UWRAP_LOCK_ALL \
56         UWRAP_LOCK(uwrap_id); \
57         UWRAP_LOCK(libc_symbol_binding); \
58         UWRAP_LOCK(libpthread_symbol_binding)
59
60 # define UWRAP_UNLOCK_ALL \
61         UWRAP_UNLOCK(libpthread_symbol_binding); \
62         UWRAP_UNLOCK(libc_symbol_binding); \
63         UWRAP_UNLOCK(uwrap_id)
64
65 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
66 #define CONSTRUCTOR_ATTRIBUTE __attribute__ ((constructor))
67 #else
68 #define CONSTRUCTOR_ATTRIBUTE
69 #endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
70
71 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
72 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
73 #else
74 #define DESTRUCTOR_ATTRIBUTE
75 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
76
77 #ifdef HAVE_ADDRESS_SANITIZER_ATTRIBUTE
78 #define DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE __attribute__((no_sanitize_address))
79 #else /* DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE */
80 #define DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
81 #endif /* DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE */
82
83 /* GCC have printf type attribute check. */
84 #ifdef HAVE_FUNCTION_ATTRIBUTE_FORMAT
85 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
86 #else
87 #define PRINTF_ATTRIBUTE(a,b)
88 #endif /* HAVE_FUNCTION_ATTRIBUTE_FORMAT */
89
90 #define UWRAP_DLIST_ADD(list,item) do { \
91         if (!(list)) { \
92                 (item)->prev    = NULL; \
93                 (item)->next    = NULL; \
94                 (list)          = (item); \
95         } else { \
96                 (item)->prev    = NULL; \
97                 (item)->next    = (list); \
98                 (list)->prev    = (item); \
99                 (list)          = (item); \
100         } \
101 } while (0)
102
103 #define UWRAP_DLIST_REMOVE(list,item) do { \
104         if ((list) == (item)) { \
105                 (list)          = (item)->next; \
106                 if (list) { \
107                         (list)->prev    = NULL; \
108                 } \
109         } else { \
110                 if ((item)->prev) { \
111                         (item)->prev->next      = (item)->next; \
112                 } \
113                 if ((item)->next) { \
114                         (item)->next->prev      = (item)->prev; \
115                 } \
116         } \
117         (item)->prev    = NULL; \
118         (item)->next    = NULL; \
119 } while (0)
120
121 #ifndef SAFE_FREE
122 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
123 #endif
124
125 /*****************
126  * LOGGING
127  *****************/
128
129 enum uwrap_dbglvl_e {
130         UWRAP_LOG_ERROR = 0,
131         UWRAP_LOG_WARN,
132         UWRAP_LOG_DEBUG,
133         UWRAP_LOG_TRACE
134 };
135
136 #ifdef NDEBUG
137 # define UWRAP_LOG(...)
138 #else /* NDEBUG */
139 static void uwrap_log(enum uwrap_dbglvl_e dbglvl, const char *function, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
140 # define UWRAP_LOG(dbglvl, ...) uwrap_log((dbglvl), __func__, __VA_ARGS__)
141
142 static void uwrap_log(enum uwrap_dbglvl_e dbglvl, const char *function, const char *format, ...)
143 {
144         char buffer[1024];
145         va_list va;
146         const char *d;
147         unsigned int lvl = 0;
148
149         d = getenv("UID_WRAPPER_DEBUGLEVEL");
150         if (d != NULL) {
151                 lvl = atoi(d);
152         }
153
154         va_start(va, format);
155         vsnprintf(buffer, sizeof(buffer), format, va);
156         va_end(va);
157
158         if (lvl >= dbglvl) {
159                 const char *prefix = "UWRAP";
160                 switch (dbglvl) {
161                         case UWRAP_LOG_ERROR:
162                                 prefix = "UWRAP_ERROR";
163                                 break;
164                         case UWRAP_LOG_WARN:
165                                 prefix = "UWRAP_WARN";
166                                 break;
167                         case UWRAP_LOG_DEBUG:
168                                 prefix = "UWRAP_DEBUG";
169                                 break;
170                         case UWRAP_LOG_TRACE:
171                                 prefix = "UWRAP_TRACE";
172                                 break;
173                 }
174
175                 fprintf(stderr,
176                         "%s(%d) - %s: %s\n",
177                         prefix,
178                         (int)getpid(),
179                         function,
180                         buffer);
181         }
182 }
183 #endif /* NDEBUG */
184
185 /*****************
186  * LIBC
187  *****************/
188
189 #define LIBC_NAME "libc.so"
190
191 typedef int (*__libc_setuid)(uid_t uid);
192
193 typedef uid_t (*__libc_getuid)(void);
194
195 #ifdef HAVE_SETEUID
196 typedef int (*__libc_seteuid)(uid_t euid);
197 #endif
198
199 #ifdef HAVE_SETREUID
200 typedef int (*__libc_setreuid)(uid_t ruid, uid_t euid);
201 #endif
202
203 #ifdef HAVE_SETRESUID
204 typedef int (*__libc_setresuid)(uid_t ruid, uid_t euid, uid_t suid);
205 #endif
206
207 #ifdef HAVE_GETRESUID
208 typedef int (*__libc_getresuid)(uid_t *ruid, uid_t *euid, uid_t *suid);
209 #endif
210
211 typedef uid_t (*__libc_geteuid)(void);
212
213 typedef int (*__libc_setgid)(gid_t gid);
214
215 typedef gid_t (*__libc_getgid)(void);
216
217 #ifdef HAVE_SETEGID
218 typedef int (*__libc_setegid)(uid_t egid);
219 #endif
220
221 #ifdef HAVE_SETREGID
222 typedef int (*__libc_setregid)(uid_t rgid, uid_t egid);
223 #endif
224
225 #ifdef HAVE_SETRESGID
226 typedef int (*__libc_setresgid)(uid_t rgid, uid_t egid, uid_t sgid);
227 #endif
228
229 #ifdef HAVE_GETRESGID
230 typedef int (*__libc_getresgid)(gid_t *rgid, gid_t *egid, gid_t *sgid);
231 #endif
232
233 typedef gid_t (*__libc_getegid)(void);
234
235 typedef int (*__libc_getgroups)(int size, gid_t list[]);
236
237 typedef int (*__libc_setgroups)(size_t size, const gid_t *list);
238
239 #ifdef HAVE_SYSCALL
240 typedef long int (*__libc_syscall)(long int sysno, ...);
241 #endif
242
243 #define UWRAP_SYMBOL_ENTRY(i) \
244         union { \
245                 __libc_##i f; \
246                 void *obj; \
247         } _libc_##i
248
249 struct uwrap_libc_symbols {
250         UWRAP_SYMBOL_ENTRY(setuid);
251         UWRAP_SYMBOL_ENTRY(getuid);
252 #ifdef HAVE_SETEUID
253         UWRAP_SYMBOL_ENTRY(seteuid);
254 #endif
255 #ifdef HAVE_SETREUID
256         UWRAP_SYMBOL_ENTRY(setreuid);
257 #endif
258 #ifdef HAVE_SETRESUID
259         UWRAP_SYMBOL_ENTRY(setresuid);
260 #endif
261 #ifdef HAVE_GETRESUID
262         UWRAP_SYMBOL_ENTRY(getresuid);
263 #endif
264         UWRAP_SYMBOL_ENTRY(geteuid);
265         UWRAP_SYMBOL_ENTRY(setgid);
266         UWRAP_SYMBOL_ENTRY(getgid);
267 #ifdef HAVE_SETEGID
268         UWRAP_SYMBOL_ENTRY(setegid);
269 #endif
270 #ifdef HAVE_SETREGID
271         UWRAP_SYMBOL_ENTRY(setregid);
272 #endif
273 #ifdef HAVE_SETRESGID
274         UWRAP_SYMBOL_ENTRY(setresgid);
275 #endif
276 #ifdef HAVE_GETRESGID
277         UWRAP_SYMBOL_ENTRY(getresgid);
278 #endif
279         UWRAP_SYMBOL_ENTRY(getegid);
280         UWRAP_SYMBOL_ENTRY(getgroups);
281         UWRAP_SYMBOL_ENTRY(setgroups);
282 #ifdef HAVE_SYSCALL
283         UWRAP_SYMBOL_ENTRY(syscall);
284 #endif
285 };
286 #undef UWRAP_SYMBOL_ENTRY
287
288 /*****************
289  * LIBPTHREAD
290  *****************/
291 /* Yeah... I'm pig. I overloading macro here... So what? */
292 #define UWRAP_SYMBOL_ENTRY(i) \
293         union { \
294                 __libpthread_##i f; \
295                 void *obj; \
296         } _libpthread_##i
297
298 typedef int (*__libpthread_pthread_create)(pthread_t *thread,
299                                     const pthread_attr_t *attr,
300                                     void *(*start_routine) (void *),
301                                     void *arg);
302 typedef void (*__libpthread_pthread_exit)(void *retval);
303
304 struct uwrap_libpthread_symbols {
305         UWRAP_SYMBOL_ENTRY(pthread_create);
306         UWRAP_SYMBOL_ENTRY(pthread_exit);
307 };
308 #undef UWRAP_SYMBOL_ENTRY
309
310 /*
311  * We keep the virtualised euid/egid/groups information here
312  */
313 struct uwrap_thread {
314         bool enabled;
315
316         uid_t ruid;
317         uid_t euid;
318         uid_t suid;
319
320         gid_t rgid;
321         gid_t egid;
322         gid_t sgid;
323
324         int ngroups;
325         gid_t *groups;
326
327         struct uwrap_thread *next;
328         struct uwrap_thread *prev;
329 };
330
331 struct uwrap {
332         struct {
333                 void *handle;
334                 struct uwrap_libc_symbols symbols;
335         } libc;
336
337         struct {
338                 void *handle;
339                 struct uwrap_libpthread_symbols symbols;
340         } libpthread;
341
342         bool initialised;
343
344         /* Real uid and gid of user who run uid wrapper */
345         uid_t myuid;
346         gid_t mygid;
347
348         struct uwrap_thread *ids;
349 };
350
351 static struct uwrap uwrap;
352
353 /* Shortcut to the list item */
354 static UWRAP_THREAD struct uwrap_thread *uwrap_tls_id;
355
356 /* The mutex or accessing the id */
357 static pthread_mutex_t uwrap_id_mutex = PTHREAD_MUTEX_INITIALIZER;
358
359 /* The mutex for accessing the global libc.symbols */
360 static pthread_mutex_t libc_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
361
362 /* The mutex for accessing the global libpthread.symbols */
363 static pthread_mutex_t libpthread_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
364
365 /*********************************************************
366  * UWRAP PROTOTYPES
367  *********************************************************/
368
369 bool uid_wrapper_enabled(void);
370 void uwrap_constructor(void) CONSTRUCTOR_ATTRIBUTE;
371 void uwrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
372
373 /*********************************************************
374  * UWRAP LIBC LOADER FUNCTIONS
375  *********************************************************/
376
377 enum uwrap_lib {
378     UWRAP_LIBC,
379     UWRAP_LIBNSL,
380     UWRAP_LIBSOCKET,
381     UWRAP_LIBPTHREAD,
382 };
383
384 static void *uwrap_load_lib_handle(enum uwrap_lib lib)
385 {
386         int flags = RTLD_LAZY;
387         void *handle = NULL;
388         int i;
389
390 #ifdef RTLD_DEEPBIND
391         flags |= RTLD_DEEPBIND;
392 #endif
393
394         switch (lib) {
395         case UWRAP_LIBNSL:
396                 /* FALL TROUGH */
397         case UWRAP_LIBSOCKET:
398                 /* FALL TROUGH */
399         case UWRAP_LIBC:
400                 handle = uwrap.libc.handle;
401                 if (handle == NULL) {
402                         for (i = 10; i >= 0; i--) {
403                                 char soname[256] = {0};
404
405                                 snprintf(soname, sizeof(soname), "libc.so.%d", i);
406                                 handle = dlopen(soname, flags);
407                                 if (handle != NULL) {
408                                         break;
409                                 }
410
411                                 /* glibc on Alpha and IA64 is libc.so.6.1 */
412                                 snprintf(soname, sizeof(soname), "libc.so.%d.1", i);
413                                 handle = dlopen(soname, flags);
414                                 if (handle != NULL) {
415                                         break;
416                                 }
417                         }
418
419                         uwrap.libc.handle = handle;
420                 }
421                 break;
422         case UWRAP_LIBPTHREAD:
423                 handle = uwrap.libpthread.handle;
424                 if (handle == NULL) {
425                         handle = dlopen("libpthread.so.0", flags);
426                         if (handle != NULL) {
427                                 break;
428                         }
429                 }
430                 break;
431         }
432
433         if (handle == NULL) {
434 #ifdef RTLD_NEXT
435                 handle = uwrap.libc.handle = RTLD_NEXT;
436 #else
437                 fprintf(stderr,
438                         "Failed to dlopen library: %s\n",
439                         dlerror());
440                 exit(-1);
441 #endif
442         }
443
444         return handle;
445 }
446
447 static void *_uwrap_bind_symbol(enum uwrap_lib lib, const char *fn_name)
448 {
449         void *handle;
450         void *func;
451
452         handle = uwrap_load_lib_handle(lib);
453
454         func = dlsym(handle, fn_name);
455         if (func == NULL) {
456                 fprintf(stderr,
457                         "Failed to find %s: %s\n",
458                         fn_name, dlerror());
459                 exit(-1);
460         }
461
462         return func;
463 }
464
465 #define uwrap_bind_symbol_libc(sym_name) \
466         UWRAP_LOCK(libc_symbol_binding); \
467         if (uwrap.libc.symbols._libc_##sym_name.obj == NULL) { \
468                 uwrap.libc.symbols._libc_##sym_name.obj = \
469                         _uwrap_bind_symbol(UWRAP_LIBC, #sym_name); \
470         } \
471         UWRAP_UNLOCK(libc_symbol_binding)
472
473 #define uwrap_bind_symbol_libpthread(sym_name) \
474         UWRAP_LOCK(libpthread_symbol_binding); \
475         if (uwrap.libpthread.symbols._libpthread_##sym_name.obj == NULL) { \
476                 uwrap.libpthread.symbols._libpthread_##sym_name.obj = \
477                         _uwrap_bind_symbol(UWRAP_LIBPTHREAD, #sym_name); \
478         } \
479         UWRAP_UNLOCK(libpthread_symbol_binding)
480
481 /*
482  * IMPORTANT
483  *
484  * Functions expeciall from libc need to be loaded individually, you can't load
485  * all at once or gdb will segfault at startup. The same applies to valgrind and
486  * has probably something todo with with the linker.
487  * So we need load each function at the point it is called the first time.
488  */
489 static int libc_setuid(uid_t uid)
490 {
491         uwrap_bind_symbol_libc(setuid);
492
493         return uwrap.libc.symbols._libc_setuid.f(uid);
494 }
495
496 static uid_t libc_getuid(void)
497 {
498         uwrap_bind_symbol_libc(getuid);
499
500         return uwrap.libc.symbols._libc_getuid.f();
501 }
502
503 #ifdef HAVE_SETEUID
504 static int libc_seteuid(uid_t euid)
505 {
506         uwrap_bind_symbol_libc(seteuid);
507
508         return uwrap.libc.symbols._libc_seteuid.f(euid);
509 }
510 #endif
511
512 #ifdef HAVE_SETREUID
513 static int libc_setreuid(uid_t ruid, uid_t euid)
514 {
515         uwrap_bind_symbol_libc(setreuid);
516
517         return uwrap.libc.symbols._libc_setreuid.f(ruid, euid);
518 }
519 #endif
520
521 #ifdef HAVE_SETRESUID
522 static int libc_setresuid(uid_t ruid, uid_t euid, uid_t suid)
523 {
524         uwrap_bind_symbol_libc(setresuid);
525
526         return uwrap.libc.symbols._libc_setresuid.f(ruid, euid, suid);
527 }
528 #endif
529
530 #ifdef HAVE_GETRESUID
531 static int libc_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
532 {
533         uwrap_bind_symbol_libc(getresuid);
534
535         return uwrap.libc.symbols._libc_getresuid.f(ruid, euid, suid);
536 }
537 #endif
538
539 static uid_t libc_geteuid(void)
540 {
541         uwrap_bind_symbol_libc(geteuid);
542
543         return uwrap.libc.symbols._libc_geteuid.f();
544 }
545
546 static int libc_setgid(gid_t gid)
547 {
548         uwrap_bind_symbol_libc(setgid);
549
550         return uwrap.libc.symbols._libc_setgid.f(gid);
551 }
552
553 static gid_t libc_getgid(void)
554 {
555         uwrap_bind_symbol_libc(getgid);
556
557         return uwrap.libc.symbols._libc_getgid.f();
558 }
559
560 #ifdef HAVE_SETEGID
561 static int libc_setegid(gid_t egid)
562 {
563         uwrap_bind_symbol_libc(setegid);
564
565         return uwrap.libc.symbols._libc_setegid.f(egid);
566 }
567 #endif
568
569 #ifdef HAVE_SETREGID
570 static int libc_setregid(gid_t rgid, gid_t egid)
571 {
572         uwrap_bind_symbol_libc(setregid);
573
574         return uwrap.libc.symbols._libc_setregid.f(rgid, egid);
575 }
576 #endif
577
578 #ifdef HAVE_SETRESGID
579 static int libc_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
580 {
581         uwrap_bind_symbol_libc(setresgid);
582
583         return uwrap.libc.symbols._libc_setresgid.f(rgid, egid, sgid);
584 }
585 #endif
586
587 #ifdef HAVE_GETRESGID
588 static int libc_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
589 {
590         uwrap_bind_symbol_libc(setresgid);
591
592         return uwrap.libc.symbols._libc_getresgid.f(rgid, egid, sgid);
593 }
594 #endif
595
596 static gid_t libc_getegid(void)
597 {
598         uwrap_bind_symbol_libc(getegid);
599
600         return uwrap.libc.symbols._libc_getegid.f();
601 }
602
603 static int libc_getgroups(int size, gid_t list[])
604 {
605         uwrap_bind_symbol_libc(getgroups);
606
607         return uwrap.libc.symbols._libc_getgroups.f(size, list);
608 }
609
610 static int libc_setgroups(size_t size, const gid_t *list)
611 {
612         uwrap_bind_symbol_libc(setgroups);
613
614         return uwrap.libc.symbols._libc_setgroups.f(size, list);
615 }
616
617 #ifdef HAVE_SYSCALL
618 DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
619 static long int libc_vsyscall(long int sysno, va_list va)
620 {
621         long int args[8];
622         long int rc;
623         int i;
624
625         uwrap_bind_symbol_libc(syscall);
626
627         for (i = 0; i < 8; i++) {
628                 args[i] = va_arg(va, long int);
629         }
630
631         rc = uwrap.libc.symbols._libc_syscall.f(sysno,
632                                           args[0],
633                                           args[1],
634                                           args[2],
635                                           args[3],
636                                           args[4],
637                                           args[5],
638                                           args[6],
639                                           args[7]);
640
641         return rc;
642 }
643 #endif
644
645 /*
646  * This part is "optimistic".
647  * Thread can ends without pthread_exit call.
648  */
649 static void libpthread_pthread_exit(void *retval)
650 {
651         uwrap_bind_symbol_libpthread(pthread_exit);
652
653         uwrap.libpthread.symbols._libpthread_pthread_exit.f(retval);
654 }
655
656 static void uwrap_pthread_exit(void *retval)
657 {
658         struct uwrap_thread *id = uwrap_tls_id;
659
660         UWRAP_LOG(UWRAP_LOG_DEBUG, "Cleanup thread");
661
662         UWRAP_LOCK(uwrap_id);
663         if (id == NULL) {
664                 UWRAP_UNLOCK(uwrap_id);
665                 libpthread_pthread_exit(retval);
666                 return;
667         }
668
669         UWRAP_DLIST_REMOVE(uwrap.ids, id);
670         SAFE_FREE(id->groups);
671         SAFE_FREE(id);
672         uwrap_tls_id = NULL;
673
674         UWRAP_UNLOCK(uwrap_id);
675
676         libpthread_pthread_exit(retval);
677 }
678
679 void pthread_exit(void *retval)
680 {
681         if (!uid_wrapper_enabled()) {
682                 libpthread_pthread_exit(retval);
683         };
684
685         uwrap_pthread_exit(retval);
686
687         /* Calm down gcc warning. */
688         exit(666);
689 }
690
691 static int libpthread_pthread_create(pthread_t *thread,
692                                 const pthread_attr_t *attr,
693                                 void *(*start_routine) (void *),
694                                 void *arg)
695 {
696         uwrap_bind_symbol_libpthread(pthread_create);
697         return uwrap.libpthread.symbols._libpthread_pthread_create.f(thread,
698                                                                      attr,
699                                                                      start_routine,
700                                                                      arg);
701 }
702
703 struct uwrap_pthread_create_args {
704         struct uwrap_thread *id;
705         void *(*start_routine) (void *);
706         void *arg;
707 };
708
709 static void *uwrap_pthread_create_start(void *_a)
710 {
711         struct uwrap_pthread_create_args *a =
712                 (struct uwrap_pthread_create_args *)_a;
713         void *(*start_routine) (void *) = a->start_routine;
714         void *arg = a->arg;
715         struct uwrap_thread *id = a->id;
716
717         SAFE_FREE(a);
718
719         uwrap_tls_id = id;
720
721         return start_routine(arg);
722 }
723
724 static int uwrap_pthread_create(pthread_t *thread,
725                                  const pthread_attr_t *attr,
726                                  void *(*start_routine) (void *),
727                                  void *arg)
728 {
729         struct uwrap_pthread_create_args *args;
730         struct uwrap_thread *src_id = uwrap_tls_id;
731         int ret;
732
733         args = malloc(sizeof(struct uwrap_pthread_create_args));
734         if (args == NULL) {
735                 UWRAP_LOG(UWRAP_LOG_ERROR,
736                           "uwrap_pthread_create: Unable to allocate memory");
737                 errno = ENOMEM;
738                 return -1;
739         }
740         args->start_routine = start_routine;
741         args->arg = arg;
742
743         args->id = calloc(1, sizeof(struct uwrap_thread));
744         if (args->id == NULL) {
745                 SAFE_FREE(args);
746                 UWRAP_LOG(UWRAP_LOG_ERROR,
747                           "uwrap_pthread_create: Unable to allocate memory");
748                 errno = ENOMEM;
749                 return -1;
750         }
751
752         UWRAP_LOCK(uwrap_id);
753
754         args->id->groups = malloc(sizeof(gid_t) * src_id->ngroups);
755         if (args->id->groups == NULL) {
756                 UWRAP_UNLOCK(uwrap_id);
757                 SAFE_FREE(args->id);
758                 SAFE_FREE(args);
759                 UWRAP_LOG(UWRAP_LOG_ERROR,
760                           "uwrap_pthread_create: Unable to allocate memory again");
761                 errno = ENOMEM;
762                 return -1;
763         }
764
765         args->id->ruid = src_id->ruid;
766         args->id->euid = src_id->euid;
767         args->id->suid = src_id->suid;
768
769         args->id->rgid = src_id->rgid;
770         args->id->egid = src_id->egid;
771         args->id->sgid = src_id->sgid;
772
773         args->id->enabled = src_id->enabled;
774
775         args->id->ngroups = src_id->ngroups;
776         if (src_id->groups != NULL) {
777                 memcpy(args->id->groups, src_id->groups,
778                        sizeof(gid_t) * src_id->ngroups);
779         } else {
780                 SAFE_FREE(args->id->groups);
781         }
782
783         UWRAP_DLIST_ADD(uwrap.ids, args->id);
784         UWRAP_UNLOCK(uwrap_id);
785
786         ret = libpthread_pthread_create(thread, attr,
787                                         uwrap_pthread_create_start,
788                                         args);
789         if (ret != 0) {
790                 return ret;
791         }
792
793         return ret;
794 }
795
796 int pthread_create(pthread_t *thread,
797                     const pthread_attr_t *attr,
798                     void *(*start_routine) (void *),
799                     void *arg)
800 {
801         if (!uid_wrapper_enabled()) {
802                 return libpthread_pthread_create(thread,
803                                            attr,
804                                            start_routine,
805                                            arg);
806         };
807
808         return uwrap_pthread_create(thread,
809                                     attr,
810                                     start_routine,
811                                     arg);
812 }
813
814 /*********************************************************
815  * UWRAP ID HANDLING
816  *********************************************************/
817
818 #define GROUP_STRING_SIZE 16384
819 #define GROUP_MAX_COUNT (GROUP_STRING_SIZE / (10 + 1))
820
821 /**
822  * This function exports all the IDs of the current user so if
823  * we fork and then exec we can setup uid_wrapper in the new process
824  * with those IDs.
825  */
826 static void uwrap_export_ids(struct uwrap_thread *id)
827 {
828         char groups_str[GROUP_STRING_SIZE] = {0};
829         size_t groups_str_size = sizeof(groups_str);
830         char unsigned_str[32] = {0};
831         int i;
832
833         /* UIDS */
834         snprintf(unsigned_str, sizeof(unsigned_str), "%u", id->ruid);
835         setenv("UID_WRAPPER_INITIAL_RUID", unsigned_str, 1);
836
837         snprintf(unsigned_str, sizeof(unsigned_str), "%u", id->euid);
838         setenv("UID_WRAPPER_INITIAL_EUID", unsigned_str, 1);
839
840         snprintf(unsigned_str, sizeof(unsigned_str), "%u", id->suid);
841         setenv("UID_WRAPPER_INITIAL_SUID", unsigned_str, 1);
842
843         /* GIDS */
844         snprintf(unsigned_str, sizeof(unsigned_str), "%u", id->rgid);
845         setenv("UID_WRAPPER_INITIAL_RGID", unsigned_str, 1);
846
847         snprintf(unsigned_str, sizeof(unsigned_str), "%u", id->egid);
848         setenv("UID_WRAPPER_INITIAL_EGID", unsigned_str, 1);
849
850         snprintf(unsigned_str, sizeof(unsigned_str), "%u", id->sgid);
851         setenv("UID_WRAPPER_INITIAL_SGID", unsigned_str, 1);
852
853         if (id->ngroups > GROUP_MAX_COUNT) {
854                 UWRAP_LOG(UWRAP_LOG_ERROR,
855                           "ERROR: Number of groups (%u) exceeds maximum value "
856                           "uid_wrapper will handle (%u).",
857                           id->ngroups,
858                           GROUP_MAX_COUNT);
859                 exit(-1);
860         }
861
862         /* GROUPS */
863         snprintf(unsigned_str, sizeof(unsigned_str), "%u", id->ngroups);
864         setenv("UID_WRAPPER_INITIAL_GROUPS_COUNT", unsigned_str, 1);
865
866         for (i = 0; i < id->ngroups; i++) {
867                 size_t groups_str_len = strlen(groups_str);
868                 size_t groups_str_avail = groups_str_size - groups_str_len;
869                 size_t len;
870
871                 len = snprintf(unsigned_str, sizeof(unsigned_str), ",%u", id->groups[i]);
872                 if (len <= 1) {
873                         continue;
874                 }
875                 if (len < groups_str_avail) {
876                         snprintf(groups_str + groups_str_len,
877                                  groups_str_size - groups_str_len,
878                                  "%s",
879                                  i == 0 ? unsigned_str + 1 : unsigned_str);
880                 }
881         }
882
883         if (id->ngroups > 0) {
884                 setenv("UID_WRAPPER_INITIAL_GROUPS", groups_str, 1);
885         }
886 }
887
888 static void uwrap_thread_prepare(void)
889 {
890         struct uwrap_thread *id = uwrap_tls_id;
891
892         /* uid_wrapper is loaded but not enabled */
893         if (id == NULL) {
894                 return;
895         }
896
897         UWRAP_LOCK_ALL;
898
899         /*
900          * What happens if another atfork prepare functions calls a uwrap
901          * function? So disable it in case another atfork prepare function
902          * calls a (s)uid function. We disable uid_wrapper only for thread
903          * (process) which called fork.
904          */
905         id->enabled = false;
906 }
907
908 static void uwrap_thread_parent(void)
909 {
910         struct uwrap_thread *id = uwrap_tls_id;
911
912         /* uid_wrapper is loaded but not enabled */
913         if (id == NULL) {
914                 return;
915         }
916
917         id->enabled = true;
918
919         UWRAP_UNLOCK_ALL;
920 }
921
922 static void uwrap_thread_child(void)
923 {
924         struct uwrap_thread *id = uwrap_tls_id;
925         struct uwrap_thread *u = uwrap.ids;
926
927         /* uid_wrapper is loaded but not enabled */
928         if (id == NULL) {
929                 return;
930         }
931
932         uwrap_export_ids(id);
933
934         /*
935          * "Garbage collector" - Inspired by DESTRUCTOR.
936          * All threads (except one which called fork()) are dead now.. Dave
937          * That's what posix said...
938          */
939         while (u != NULL) {
940                 if (u == id) {
941                         /* Skip this item. */
942                         u = uwrap.ids->next;
943                         continue;
944                 }
945
946                 UWRAP_DLIST_REMOVE(uwrap.ids, u);
947
948                 SAFE_FREE(u->groups);
949                 SAFE_FREE(u);
950
951                 u = uwrap.ids;
952         }
953
954         id->enabled = true;
955
956         UWRAP_UNLOCK_ALL;
957 }
958
959 /*
960  * This initializes uid_wrapper with the IDs exported to the environment. Those
961  * are normally set after we forked and executed.
962  */
963 static void uwrap_init_env(struct uwrap_thread *id)
964 {
965         const char *env;
966         int ngroups = 0;
967
968         env = getenv("UID_WRAPPER_INITIAL_RUID");
969         if (env != NULL && env[0] != '\0') {
970                 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initialize ruid with %s", env);
971                 id->ruid = strtoul(env, (char **)NULL, 10);
972                 unsetenv("UID_WRAPPER_INITIAL_RUID");
973         }
974
975         env = getenv("UID_WRAPPER_INITIAL_EUID");
976         if (env != NULL && env[0] != '\0') {
977                 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initalize euid with %s", env);
978                 id->euid = strtoul(env, (char **)NULL, 10);
979                 unsetenv("UID_WRAPPER_INITIAL_EUID");
980         }
981
982         env = getenv("UID_WRAPPER_INITIAL_SUID");
983         if (env != NULL && env[0] != '\0') {
984                 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initalize suid with %s", env);
985                 id->suid = strtoul(env, (char **)NULL, 10);
986                 unsetenv("UID_WRAPPER_INITIAL_SUID");
987         }
988
989         env = getenv("UID_WRAPPER_INITIAL_RGID");
990         if (env != NULL && env[0] != '\0') {
991                 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initialize ruid with %s", env);
992                 id->rgid = strtoul(env, (char **)NULL, 10);
993                 unsetenv("UID_WRAPPER_INITIAL_RGID");
994         }
995
996         env = getenv("UID_WRAPPER_INITIAL_EGID");
997         if (env != NULL && env[0] != '\0') {
998                 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initalize egid with %s", env);
999                 id->egid = strtoul(env, (char **)NULL, 10);
1000                 unsetenv("UID_WRAPPER_INITIAL_EGID");
1001         }
1002
1003         env = getenv("UID_WRAPPER_INITIAL_SGID");
1004         if (env != NULL && env[0] != '\0') {
1005                 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initalize sgid with %s", env);
1006                 id->sgid = strtoul(env, (char **)NULL, 10);
1007                 unsetenv("UID_WRAPPER_INITIAL_SGID");
1008         }
1009
1010         env = getenv("UID_WRAPPER_INITIAL_GROUPS_COUNT");
1011         if (env != NULL && env[0] != '\0') {
1012                 ngroups = strtol(env, (char **)NULL, 10);
1013                 unsetenv("UID_WRAPPER_INITIAL_GROUPS_COUNT");
1014         }
1015
1016         if (ngroups > 0 && ngroups < GROUP_MAX_COUNT) {
1017                 int i = 0;
1018
1019                 id->ngroups = 0;
1020
1021                 free(id->groups);
1022                 id->groups = malloc(sizeof(gid_t) * ngroups);
1023                 if (id->groups == NULL) {
1024                         UWRAP_LOG(UWRAP_LOG_ERROR,
1025                                   "Unable to allocate memory");
1026                         exit(-1);
1027                 }
1028
1029                 env = getenv("UID_WRAPPER_INITIAL_GROUPS");
1030                 if (env != NULL && env[0] != '\0') {
1031                         char *groups_str = NULL;
1032                         char *saveptr = NULL;
1033                         const char *p = NULL;
1034
1035                         groups_str = strdup(env);
1036                         if (groups_str == NULL) {
1037                                 exit(-1);
1038                         }
1039
1040                         p = strtok_r(groups_str, ",", &saveptr);
1041                         while (p != NULL) {
1042                                 id->groups[i] = strtol(p, (char **)NULL, 10);
1043                                 i++;
1044
1045                                 p = strtok_r(NULL, ",", &saveptr);
1046                         }
1047                         SAFE_FREE(groups_str);
1048                 }
1049
1050                 if (i != ngroups) {
1051                         exit(-1);
1052                 }
1053
1054                 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initalize groups with %s", env);
1055                 id->ngroups = ngroups;
1056         }
1057 }
1058
1059 static void uwrap_init(void)
1060 {
1061         const char *env;
1062
1063         UWRAP_LOCK(uwrap_id);
1064
1065         if (uwrap.initialised) {
1066                 struct uwrap_thread *id = uwrap_tls_id;
1067
1068                 if (uwrap.ids == NULL) {
1069                         UWRAP_UNLOCK(uwrap_id);
1070                         return;
1071                 }
1072
1073                 if (id == NULL) {
1074                         UWRAP_LOG(UWRAP_LOG_ERROR,
1075                                   "Invalid id for thread");
1076                         exit(-1);
1077                 }
1078
1079                 UWRAP_UNLOCK(uwrap_id);
1080                 return;
1081         }
1082
1083         UWRAP_LOG(UWRAP_LOG_DEBUG, "Initialize uid_wrapper");
1084
1085         uwrap.initialised = true;
1086
1087         env = getenv("UID_WRAPPER");
1088         if (env != NULL && env[0] == '1') {
1089                 const char *root = getenv("UID_WRAPPER_ROOT");
1090                 struct uwrap_thread *id;
1091
1092                 id = calloc(1, sizeof(struct uwrap_thread));
1093                 if (id == NULL) {
1094                         UWRAP_LOG(UWRAP_LOG_ERROR,
1095                                   "Unable to allocate memory for main id");
1096                         exit(-1);
1097                 }
1098
1099                 UWRAP_DLIST_ADD(uwrap.ids, id);
1100                 uwrap_tls_id = id;
1101
1102                 uwrap.myuid = libc_geteuid();
1103                 uwrap.mygid = libc_getegid();
1104
1105                 /* put us in one group */
1106                 if (root != NULL && root[0] == '1') {
1107                         id->ruid = id->euid = id->suid = 0;
1108                         id->rgid = id->egid = id->sgid = 0;
1109
1110                         id->groups = malloc(sizeof(gid_t) * 1);
1111                         if (id->groups == NULL) {
1112                                 UWRAP_LOG(UWRAP_LOG_ERROR,
1113                                           "Unable to allocate memory");
1114                                 exit(-1);
1115                         }
1116
1117                         id->ngroups = 1;
1118                         id->groups[0] = 0;
1119
1120                 } else {
1121                         id->ruid = id->euid = id->suid = uwrap.myuid;
1122                         id->rgid = id->egid = id->sgid = uwrap.mygid;
1123
1124                         id->ngroups = libc_getgroups(0, NULL);
1125                         if (id->ngroups == -1) {
1126                                 UWRAP_LOG(UWRAP_LOG_ERROR,
1127                                           "Unable to call libc_getgroups in uwrap_init.");
1128                                 exit(-1);
1129                         }
1130                         id->groups = malloc(sizeof(gid_t) * id->ngroups);
1131                         if (id->groups == NULL) {
1132                                 UWRAP_LOG(UWRAP_LOG_ERROR, "Unable to allocate memory");
1133                                 exit(-1);
1134                         }
1135                         if (libc_getgroups(id->ngroups, id->groups) == -1) {
1136                                 UWRAP_LOG(UWRAP_LOG_ERROR,
1137                                           "Unable to call libc_getgroups again in uwrap_init.");
1138                                 id->groups = 0;
1139                                 /*
1140                                  * Deallocation of uwrap.groups is handled by
1141                                  * library destructor.
1142                                  */
1143                                 exit(-1);
1144                         }
1145                 }
1146
1147                 uwrap_init_env(id);
1148
1149                 id->enabled = true;
1150
1151                 UWRAP_LOG(UWRAP_LOG_DEBUG,
1152                           "Enabled uid_wrapper as %s (real uid=%u)",
1153                           id->ruid == 0 ? "root" : "user",
1154                           (unsigned int)uwrap.myuid);
1155         }
1156
1157         UWRAP_UNLOCK(uwrap_id);
1158
1159         UWRAP_LOG(UWRAP_LOG_DEBUG, "Succeccfully initialized uid_wrapper");
1160 }
1161
1162 bool uid_wrapper_enabled(void)
1163 {
1164         struct uwrap_thread *id = uwrap_tls_id;
1165         bool enabled;
1166
1167         if (id == NULL) {
1168                 return false;
1169         }
1170
1171         UWRAP_LOCK(uwrap_id);
1172         enabled = id->enabled;
1173         UWRAP_UNLOCK(uwrap_id);
1174
1175         return enabled;
1176 }
1177
1178 /*
1179  * UWRAP_SETxUID FUNCTIONS
1180  */
1181
1182 static int uwrap_setresuid_args(uid_t ruid, uid_t euid, uid_t suid)
1183 {
1184         struct uwrap_thread *id = uwrap_tls_id;
1185
1186         UWRAP_LOG(UWRAP_LOG_TRACE,
1187                   "ruid %d -> %d, euid %d -> %d, suid %d -> %d",
1188                   id->ruid, ruid, id->euid, euid, id->suid, suid);
1189
1190         if (id->euid != 0) {
1191                 if (ruid != (uid_t)-1 &&
1192                     ruid != id->ruid &&
1193                     ruid != id->euid &&
1194                     ruid != id->suid) {
1195                         errno = EPERM;
1196                         return -1;
1197                 }
1198                 if (euid != (uid_t)-1 &&
1199                     euid != id->ruid &&
1200                     euid != id->euid &&
1201                     euid != id->suid) {
1202                         errno = EPERM;
1203                         return -1;
1204                 }
1205                 if (suid != (uid_t)-1 &&
1206                     suid != id->ruid &&
1207                     suid != id->euid &&
1208                     suid != id->suid) {
1209                         errno = EPERM;
1210                         return -1;
1211                 }
1212         }
1213
1214         return 0;
1215 }
1216
1217 static int uwrap_setresuid_thread(uid_t ruid, uid_t euid, uid_t suid)
1218 {
1219         struct uwrap_thread *id = uwrap_tls_id;
1220         int rc;
1221
1222         UWRAP_LOG(UWRAP_LOG_TRACE,
1223                   "ruid %d -> %d, euid %d -> %d, suid %d -> %d",
1224                   id->ruid, ruid, id->euid, euid, id->suid, suid);
1225
1226         rc = uwrap_setresuid_args(ruid, euid, suid);
1227         if (rc != 0) {
1228                 return rc;
1229         }
1230
1231         UWRAP_LOCK(uwrap_id);
1232
1233         if (ruid != (uid_t)-1) {
1234                 id->ruid = ruid;
1235         }
1236
1237         if (euid != (uid_t)-1) {
1238                 id->euid = euid;
1239         }
1240
1241         if (suid != (uid_t)-1) {
1242                 id->suid = suid;
1243         }
1244
1245         UWRAP_UNLOCK(uwrap_id);
1246
1247         return 0;
1248 }
1249
1250 static int uwrap_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1251 {
1252         struct uwrap_thread *id = uwrap_tls_id;
1253         int rc;
1254
1255         UWRAP_LOG(UWRAP_LOG_TRACE,
1256                   "ruid %d -> %d, euid %d -> %d, suid %d -> %d",
1257                   id->ruid, ruid, id->euid, euid, id->suid, suid);
1258
1259         rc = uwrap_setresuid_args(ruid, euid, suid);
1260         if (rc != 0) {
1261                 return rc;
1262         }
1263
1264         UWRAP_LOCK(uwrap_id);
1265
1266         for (id = uwrap.ids; id; id = id->next) {
1267                 if (ruid != (uid_t)-1) {
1268                         id->ruid = ruid;
1269                 }
1270
1271                 if (euid != (uid_t)-1) {
1272                         id->euid = euid;
1273                 }
1274
1275                 if (suid != (uid_t)-1) {
1276                         id->suid = suid;
1277                 }
1278         }
1279
1280         UWRAP_UNLOCK(uwrap_id);
1281
1282         return 0;
1283 }
1284
1285 static int uwrap_setreuid_args(uid_t ruid, uid_t euid,
1286                                uid_t *_new_ruid,
1287                                uid_t *_new_euid,
1288                                uid_t *_new_suid)
1289 {
1290         struct uwrap_thread *id = uwrap_tls_id;
1291         uid_t new_ruid = -1, new_euid = -1, new_suid = -1;
1292
1293         UWRAP_LOG(UWRAP_LOG_TRACE,
1294                   "ruid %d -> %d, euid %d -> %d",
1295                   id->ruid, ruid, id->euid, euid);
1296
1297         if (ruid != (uid_t)-1) {
1298                 new_ruid = ruid;
1299                 if (ruid != id->ruid &&
1300                     ruid != id->euid &&
1301                     id->euid != 0) {
1302                         errno = EPERM;
1303                         return -1;
1304                 }
1305         }
1306
1307         if (euid != (uid_t)-1) {
1308                 new_euid = euid;
1309                 if (euid != id->ruid &&
1310                     euid != id->euid &&
1311                     euid != id->suid &&
1312                     id->euid != 0) {
1313                         errno = EPERM;
1314                         return -1;
1315                 }
1316         }
1317
1318         if (ruid != (uid_t) -1 ||
1319             (euid != (uid_t)-1 && id->ruid != euid)) {
1320                 new_suid = new_euid;
1321         }
1322
1323         *_new_ruid = new_ruid;
1324         *_new_euid = new_euid;
1325         *_new_suid = new_suid;
1326
1327         return 0;
1328 }
1329
1330 static int uwrap_setreuid_thread(uid_t ruid, uid_t euid)
1331 {
1332 #ifndef NDEBUG
1333         struct uwrap_thread *id = uwrap_tls_id;
1334 #endif
1335         uid_t new_ruid = -1, new_euid = -1, new_suid = -1;
1336         int rc;
1337
1338         UWRAP_LOG(UWRAP_LOG_TRACE,
1339                   "ruid %d -> %d, euid %d -> %d",
1340                   id->ruid, ruid, id->euid, euid);
1341
1342         rc = uwrap_setreuid_args(ruid, euid, &new_ruid, &new_euid, &new_suid);
1343         if (rc != 0) {
1344                 return rc;
1345         }
1346
1347         return uwrap_setresuid_thread(new_ruid, new_euid, new_suid);
1348 }
1349
1350 #ifdef HAVE_SETREUID
1351 static int uwrap_setreuid(uid_t ruid, uid_t euid)
1352 {
1353 #ifndef NDEBUG
1354         struct uwrap_thread *id = uwrap_tls_id;
1355 #endif
1356         uid_t new_ruid = -1, new_euid = -1, new_suid = -1;
1357         int rc;
1358
1359         UWRAP_LOG(UWRAP_LOG_TRACE,
1360                   "ruid %d -> %d, euid %d -> %d",
1361                   id->ruid, ruid, id->euid, euid);
1362
1363         rc = uwrap_setreuid_args(ruid, euid, &new_ruid, &new_euid, &new_suid);
1364         if (rc != 0) {
1365                 return rc;
1366         }
1367
1368         return uwrap_setresuid(new_ruid, new_euid, new_suid);
1369 }
1370 #endif
1371
1372 static int uwrap_setuid_args(uid_t uid,
1373                              uid_t *new_ruid,
1374                              uid_t *new_euid,
1375                              uid_t *new_suid)
1376 {
1377         struct uwrap_thread *id = uwrap_tls_id;
1378
1379         UWRAP_LOG(UWRAP_LOG_TRACE,
1380                   "uid %d -> %d",
1381                   id->ruid, uid);
1382
1383         if (uid == (uid_t)-1) {
1384                 errno = EINVAL;
1385                 return -1;
1386         }
1387
1388         if (id->euid == 0) {
1389                 *new_suid = *new_ruid = uid;
1390         } else if (uid != id->ruid &&
1391                    uid != id->suid) {
1392                 errno = EPERM;
1393                 return -1;
1394         }
1395
1396         *new_euid = uid;
1397
1398         return 0;
1399 }
1400
1401 static int uwrap_setuid_thread(uid_t uid)
1402 {
1403         uid_t new_ruid = -1, new_euid = -1, new_suid = -1;
1404         int rc;
1405
1406         rc = uwrap_setuid_args(uid, &new_ruid, &new_euid, &new_suid);
1407         if (rc != 0) {
1408                 return rc;
1409         }
1410
1411         return uwrap_setresuid_thread(new_ruid, new_euid, new_suid);
1412 }
1413
1414 static int uwrap_setuid(uid_t uid)
1415 {
1416         uid_t new_ruid = -1, new_euid = -1, new_suid = -1;
1417         int rc;
1418
1419         rc = uwrap_setuid_args(uid, &new_ruid, &new_euid, &new_suid);
1420         if (rc != 0) {
1421                 return rc;
1422         }
1423
1424         return uwrap_setresuid(new_ruid, new_euid, new_suid);
1425 }
1426
1427 /*
1428  * UWRAP_GETxUID FUNCTIONS
1429  */
1430
1431 #ifdef HAVE_GETRESUID
1432 static int uwrap_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
1433 {
1434         struct uwrap_thread *id = uwrap_tls_id;
1435
1436         UWRAP_LOCK(uwrap_id);
1437
1438         *ruid = id->ruid;
1439         *euid = id->euid;
1440         *suid = id->suid;
1441
1442         UWRAP_UNLOCK(uwrap_id);
1443
1444         return 0;
1445 }
1446 #endif
1447
1448 #ifdef HAVE_GETRESGID
1449 static int uwrap_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
1450 {
1451         struct uwrap_thread *id = uwrap_tls_id;
1452
1453         UWRAP_LOCK(uwrap_id);
1454
1455         *rgid = id->rgid;
1456         *egid = id->egid;
1457         *sgid = id->sgid;
1458
1459         UWRAP_UNLOCK(uwrap_id);
1460
1461         return 0;
1462 }
1463 #endif
1464
1465 /*
1466  * UWRAP_SETxGID FUNCTIONS
1467  */
1468
1469 static int uwrap_setresgid_args(gid_t rgid, gid_t egid, gid_t sgid)
1470 {
1471         struct uwrap_thread *id = uwrap_tls_id;
1472
1473         UWRAP_LOG(UWRAP_LOG_TRACE,
1474                   "rgid %d -> %d, egid %d -> %d, sgid %d -> %d",
1475                   id->rgid, rgid, id->egid, egid, id->sgid, sgid);
1476
1477         if (id->euid != 0) {
1478                 if (rgid != (gid_t)-1 &&
1479                     rgid != id->rgid &&
1480                     rgid != id->egid &&
1481                     rgid != id->sgid) {
1482                         errno = EPERM;
1483                         return -1;
1484                 }
1485                 if (egid != (gid_t)-1 &&
1486                     egid != id->rgid &&
1487                     egid != id->egid &&
1488                     egid != id->sgid) {
1489                         errno = EPERM;
1490                         return -1;
1491                 }
1492                 if (sgid != (gid_t)-1 &&
1493                     sgid != id->rgid &&
1494                     sgid != id->egid &&
1495                     sgid != id->sgid) {
1496                         errno = EPERM;
1497                         return -1;
1498                 }
1499         }
1500
1501         return 0;
1502 }
1503
1504 static int uwrap_setresgid_thread(gid_t rgid, gid_t egid, gid_t sgid)
1505 {
1506         struct uwrap_thread *id = uwrap_tls_id;
1507         int rc;
1508
1509         UWRAP_LOG(UWRAP_LOG_TRACE,
1510                   "rgid %d -> %d, egid %d -> %d, sgid %d -> %d",
1511                   id->rgid, rgid, id->egid, egid, id->sgid, sgid);
1512
1513         rc = uwrap_setresgid_args(rgid, egid, sgid);
1514         if (rc != 0) {
1515                 return rc;
1516         }
1517
1518         UWRAP_LOCK(uwrap_id);
1519
1520         if (rgid != (gid_t)-1) {
1521                 id->rgid = rgid;
1522         }
1523
1524         if (egid != (gid_t)-1) {
1525                 id->egid = egid;
1526         }
1527
1528         if (sgid != (gid_t)-1) {
1529                 id->sgid = sgid;
1530         }
1531
1532         UWRAP_UNLOCK(uwrap_id);
1533
1534         return 0;
1535 }
1536
1537 static int uwrap_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1538 {
1539         struct uwrap_thread *id = uwrap_tls_id;
1540         int rc;
1541
1542         UWRAP_LOG(UWRAP_LOG_TRACE,
1543                   "rgid %d -> %d, egid %d -> %d, sgid %d -> %d",
1544                   id->rgid, rgid, id->egid, egid, id->sgid, sgid);
1545
1546         rc = uwrap_setresgid_args(rgid, egid, sgid);
1547         if (rc != 0) {
1548                 return rc;
1549         }
1550
1551         UWRAP_LOCK(uwrap_id);
1552
1553         for (id = uwrap.ids; id; id = id->next) {
1554                 if (rgid != (gid_t)-1) {
1555                         id->rgid = rgid;
1556                 }
1557
1558                 if (egid != (gid_t)-1) {
1559                         id->egid = egid;
1560                 }
1561
1562                 if (sgid != (gid_t)-1) {
1563                         id->sgid = sgid;
1564                 }
1565         }
1566
1567         UWRAP_UNLOCK(uwrap_id);
1568
1569         return 0;
1570 }
1571
1572 static int uwrap_setregid_args(gid_t rgid, gid_t egid,
1573                                gid_t *_new_rgid,
1574                                gid_t *_new_egid,
1575                                gid_t *_new_sgid)
1576 {
1577         struct uwrap_thread *id = uwrap_tls_id;
1578         gid_t new_rgid = -1, new_egid = -1, new_sgid = -1;
1579
1580         UWRAP_LOG(UWRAP_LOG_TRACE,
1581                   "rgid %d -> %d, egid %d -> %d",
1582                   id->rgid, rgid, id->egid, egid);
1583
1584         if (rgid != (gid_t)-1) {
1585                 new_rgid = rgid;
1586                 if (rgid != id->rgid &&
1587                     rgid != id->egid &&
1588                     id->euid != 0) {
1589                         errno = EPERM;
1590                         return -1;
1591                 }
1592         }
1593
1594         if (egid != (gid_t)-1) {
1595                 new_egid = egid;
1596                 if (egid != id->rgid &&
1597                     egid != id->egid &&
1598                     egid != id->sgid &&
1599                     id->euid != 0) {
1600                         errno = EPERM;
1601                         return -1;
1602                 }
1603         }
1604
1605         if (rgid != (gid_t) -1 ||
1606             (egid != (gid_t)-1 && id->rgid != egid)) {
1607                 new_sgid = new_egid;
1608         }
1609
1610         *_new_rgid = new_rgid;
1611         *_new_egid = new_egid;
1612         *_new_sgid = new_sgid;
1613
1614         return 0;
1615 }
1616
1617 static int uwrap_setregid_thread(gid_t rgid, gid_t egid)
1618 {
1619 #ifndef NDEBUG
1620         struct uwrap_thread *id = uwrap_tls_id;
1621 #endif
1622         gid_t new_rgid = -1, new_egid = -1, new_sgid = -1;
1623         int rc;
1624
1625         UWRAP_LOG(UWRAP_LOG_TRACE,
1626                   "rgid %d -> %d, egid %d -> %d",
1627                   id->rgid, rgid, id->egid, egid);
1628
1629         rc = uwrap_setregid_args(rgid, egid, &new_rgid, &new_egid, &new_sgid);
1630         if (rc != 0) {
1631                 return rc;
1632         }
1633
1634         return uwrap_setresgid_thread(new_rgid, new_egid, new_sgid);
1635 }
1636
1637 #ifdef HAVE_SETREGID
1638 static int uwrap_setregid(gid_t rgid, gid_t egid)
1639 {
1640 #ifndef NDEBUG
1641         struct uwrap_thread *id = uwrap_tls_id;
1642 #endif
1643         gid_t new_rgid = -1, new_egid = -1, new_sgid = -1;
1644         int rc;
1645
1646         UWRAP_LOG(UWRAP_LOG_TRACE,
1647                   "rgid %d -> %d, egid %d -> %d",
1648                   id->rgid, rgid, id->egid, egid);
1649
1650         rc = uwrap_setregid_args(rgid, egid, &new_rgid, &new_egid, &new_sgid);
1651         if (rc != 0) {
1652                 return rc;
1653         }
1654
1655         return uwrap_setresgid(new_rgid, new_egid, new_sgid);
1656 }
1657 #endif
1658
1659 static int uwrap_setgid_args(gid_t gid,
1660                              gid_t *new_rgid,
1661                              gid_t *new_egid,
1662                              gid_t *new_sgid)
1663 {
1664         struct uwrap_thread *id = uwrap_tls_id;
1665
1666         UWRAP_LOG(UWRAP_LOG_TRACE,
1667                   "gid %d -> %d",
1668                   id->rgid, gid);
1669
1670         if (gid == (gid_t)-1) {
1671                 errno = EINVAL;
1672                 return -1;
1673         }
1674
1675         if (id->euid == 0) {
1676                 *new_sgid = *new_rgid = gid;
1677         } else if (gid != id->rgid &&
1678                    gid != id->sgid) {
1679                 errno = EPERM;
1680                 return -1;
1681         }
1682
1683         *new_egid = gid;
1684
1685         return 0;
1686 }
1687
1688 static int uwrap_setgid_thread(gid_t gid)
1689 {
1690         gid_t new_rgid = -1, new_egid = -1, new_sgid = -1;
1691         int rc;
1692
1693         rc = uwrap_setgid_args(gid, &new_rgid, &new_egid, &new_sgid);
1694         if (rc != 0) {
1695                 return rc;
1696         }
1697
1698         return uwrap_setresgid_thread(new_rgid, new_egid, new_sgid);
1699 }
1700
1701 static int uwrap_setgid(gid_t gid)
1702 {
1703         gid_t new_rgid = -1, new_egid = -1, new_sgid = -1;
1704         int rc;
1705
1706         rc = uwrap_setgid_args(gid, &new_rgid, &new_egid, &new_sgid);
1707         if (rc != 0) {
1708                 return rc;
1709         }
1710
1711         return uwrap_setresgid(new_rgid, new_egid, new_sgid);
1712 }
1713
1714 /*
1715  * SETUID
1716  */
1717 int setuid(uid_t uid)
1718 {
1719         if (!uid_wrapper_enabled()) {
1720                 return libc_setuid(uid);
1721         }
1722
1723         uwrap_init();
1724         return uwrap_setuid(uid);
1725 }
1726
1727 #ifdef HAVE_SETEUID
1728 int seteuid(uid_t euid)
1729 {
1730         if (!uid_wrapper_enabled()) {
1731                 return libc_seteuid(euid);
1732         }
1733
1734         /* On FreeBSD the uid_t -1 is set and doesn't produce and error */
1735         if (euid == (uid_t)-1) {
1736                 errno = EINVAL;
1737                 return -1;
1738         }
1739
1740         uwrap_init();
1741         return uwrap_setresuid(-1, euid, -1);
1742 }
1743 #endif
1744
1745 #ifdef HAVE_SETREUID
1746 int setreuid(uid_t ruid, uid_t euid)
1747 {
1748         if (!uid_wrapper_enabled()) {
1749                 return libc_setreuid(ruid, euid);
1750         }
1751
1752         uwrap_init();
1753         return uwrap_setreuid(ruid, euid);
1754 }
1755 #endif
1756
1757 #ifdef HAVE_SETRESUID
1758 int setresuid(uid_t ruid, uid_t euid, uid_t suid)
1759 {
1760         if (!uid_wrapper_enabled()) {
1761                 return libc_setresuid(ruid, euid, suid);
1762         }
1763
1764         uwrap_init();
1765         return uwrap_setresuid(ruid, euid, suid);
1766 }
1767 #endif
1768
1769 #ifdef HAVE_GETRESUID
1770 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
1771 {
1772         if (!uid_wrapper_enabled()) {
1773                 return libc_getresuid(ruid, euid, suid);
1774         }
1775
1776         uwrap_init();
1777         return uwrap_getresuid(ruid, euid, suid);
1778 }
1779 #endif
1780
1781 /*
1782  * GETUID
1783  */
1784 static uid_t uwrap_getuid(void)
1785 {
1786         struct uwrap_thread *id = uwrap_tls_id;
1787         uid_t uid;
1788
1789         UWRAP_LOCK(uwrap_id);
1790         uid = id->ruid;
1791         UWRAP_UNLOCK(uwrap_id);
1792
1793         return uid;
1794 }
1795
1796 uid_t getuid(void)
1797 {
1798         if (!uid_wrapper_enabled()) {
1799                 return libc_getuid();
1800         }
1801
1802         uwrap_init();
1803         return uwrap_getuid();
1804 }
1805
1806 /*
1807  * GETEUID
1808  */
1809 static uid_t uwrap_geteuid(void)
1810 {
1811         const char *env = getenv("UID_WRAPPER_MYUID");
1812         struct uwrap_thread *id = uwrap_tls_id;
1813         uid_t uid;
1814
1815         UWRAP_LOCK(uwrap_id);
1816         uid = id->euid;
1817         UWRAP_UNLOCK(uwrap_id);
1818
1819         /* Disable root and return myuid */
1820         if (env != NULL && env[0] == '1') {
1821                 uid = uwrap.myuid;
1822         }
1823
1824         return uid;
1825 }
1826
1827 uid_t geteuid(void)
1828 {
1829         if (!uid_wrapper_enabled()) {
1830                 return libc_geteuid();
1831         }
1832
1833         uwrap_init();
1834         return uwrap_geteuid();
1835 }
1836
1837 /*
1838  * SETGID
1839  */
1840 int setgid(gid_t gid)
1841 {
1842         if (!uid_wrapper_enabled()) {
1843                 return libc_setgid(gid);
1844         }
1845
1846         uwrap_init();
1847         return uwrap_setgid(gid);
1848 }
1849
1850 #ifdef HAVE_SETEGID
1851 int setegid(gid_t egid)
1852 {
1853         if (!uid_wrapper_enabled()) {
1854                 return libc_setegid(egid);
1855         }
1856
1857         /* On FreeBSD the uid_t -1 is set and doesn't produce and error */
1858         if (egid == (gid_t)-1) {
1859                 errno = EINVAL;
1860                 return -1;
1861         }
1862
1863         uwrap_init();
1864         return uwrap_setresgid(-1, egid, -1);
1865 }
1866 #endif
1867
1868 #ifdef HAVE_SETREGID
1869 int setregid(gid_t rgid, gid_t egid)
1870 {
1871         if (!uid_wrapper_enabled()) {
1872                 return libc_setregid(rgid, egid);
1873         }
1874
1875         uwrap_init();
1876         return uwrap_setregid(rgid, egid);
1877 }
1878 #endif
1879
1880 #ifdef HAVE_SETRESGID
1881 int setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1882 {
1883         if (!uid_wrapper_enabled()) {
1884                 return libc_setresgid(rgid, egid, sgid);
1885         }
1886
1887         uwrap_init();
1888         return uwrap_setresgid(rgid, egid, sgid);
1889 }
1890 #endif
1891
1892 #ifdef HAVE_GETRESGID
1893 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
1894 {
1895         if (!uid_wrapper_enabled()) {
1896                 return libc_getresgid(rgid, egid, sgid);
1897         }
1898
1899         uwrap_init();
1900         return uwrap_getresgid(rgid, egid, sgid);
1901 }
1902 #endif
1903
1904 /*
1905  * GETGID
1906  */
1907 static gid_t uwrap_getgid(void)
1908 {
1909         struct uwrap_thread *id = uwrap_tls_id;
1910         gid_t gid;
1911
1912         UWRAP_LOCK(uwrap_id);
1913         gid = id->rgid;
1914         UWRAP_UNLOCK(uwrap_id);
1915
1916         return gid;
1917 }
1918
1919 gid_t getgid(void)
1920 {
1921         if (!uid_wrapper_enabled()) {
1922                 return libc_getgid();
1923         }
1924
1925         uwrap_init();
1926         return uwrap_getgid();
1927 }
1928
1929 /*
1930  * GETEGID
1931  */
1932 static uid_t uwrap_getegid(void)
1933 {
1934         struct uwrap_thread *id = uwrap_tls_id;
1935         gid_t gid;
1936
1937         UWRAP_LOCK(uwrap_id);
1938         gid = id->egid;
1939         UWRAP_UNLOCK(uwrap_id);
1940
1941         return gid;
1942 }
1943
1944 uid_t getegid(void)
1945 {
1946         if (!uid_wrapper_enabled()) {
1947                 return libc_getegid();
1948         }
1949
1950         uwrap_init();
1951         return uwrap_getegid();
1952 }
1953
1954 static int uwrap_setgroups_thread(size_t size, const gid_t *list)
1955 {
1956         struct uwrap_thread *id = uwrap_tls_id;
1957         int rc = -1;
1958
1959         UWRAP_LOCK(uwrap_id);
1960
1961         if (size == 0) {
1962                 SAFE_FREE(id->groups);
1963                 id->ngroups = 0;
1964         } else if (size > 0) {
1965                 gid_t *tmp;
1966
1967                 tmp = realloc(id->groups, sizeof(gid_t) * size);
1968                 if (tmp == NULL) {
1969                         errno = ENOMEM;
1970                         goto out;
1971                 }
1972                 id->groups = tmp;
1973                 id->ngroups = size;
1974                 memcpy(id->groups, list, size * sizeof(gid_t));
1975         }
1976
1977         rc = 0;
1978 out:
1979         UWRAP_UNLOCK(uwrap_id);
1980
1981         return rc;
1982 }
1983
1984 static int uwrap_setgroups(size_t size, const gid_t *list)
1985 {
1986         struct uwrap_thread *id;
1987         int rc = -1;
1988
1989         UWRAP_LOCK(uwrap_id);
1990
1991         if (size == 0) {
1992                 for (id = uwrap.ids; id; id = id->next) {
1993                         SAFE_FREE(id->groups);
1994                         id->ngroups = 0;
1995
1996                 }
1997         } else if (size > 0) {
1998                 gid_t *tmp;
1999
2000                 for (id = uwrap.ids; id; id = id->next) {
2001                         tmp = realloc(id->groups, sizeof(gid_t) * size);
2002                         if (tmp == NULL) {
2003                                 errno = ENOMEM;
2004                                 goto out;
2005                         }
2006                         id->groups = tmp;
2007
2008                         id->ngroups = size;
2009                         memcpy(id->groups, list, size * sizeof(gid_t));
2010                 }
2011         }
2012
2013         rc = 0;
2014 out:
2015         UWRAP_UNLOCK(uwrap_id);
2016
2017         return rc;
2018 }
2019
2020 #ifdef HAVE_SETGROUPS_INT
2021 int setgroups(int size, const gid_t *list)
2022 #else
2023 int setgroups(size_t size, const gid_t *list)
2024 #endif
2025 {
2026         if (!uid_wrapper_enabled()) {
2027                 return libc_setgroups(size, list);
2028         }
2029
2030         uwrap_init();
2031         return uwrap_setgroups(size, list);
2032 }
2033
2034 static int uwrap_getgroups(int size, gid_t *list)
2035 {
2036         struct uwrap_thread *id = uwrap_tls_id;
2037         int ngroups;
2038
2039         UWRAP_LOCK(uwrap_id);
2040         ngroups = id->ngroups;
2041
2042         if (size > ngroups) {
2043                 size = ngroups;
2044         }
2045         if (size == 0) {
2046                 goto out;
2047         }
2048         if (size < ngroups) {
2049                 errno = EINVAL;
2050                 ngroups = -1;
2051         }
2052         memcpy(list, id->groups, size * sizeof(gid_t));
2053
2054 out:
2055         UWRAP_UNLOCK(uwrap_id);
2056
2057         return ngroups;
2058 }
2059
2060 int getgroups(int size, gid_t *list)
2061 {
2062         if (!uid_wrapper_enabled()) {
2063                 return libc_getgroups(size, list);
2064         }
2065
2066         uwrap_init();
2067         return uwrap_getgroups(size, list);
2068 }
2069
2070 #if (defined(HAVE_SYS_SYSCALL_H) || defined(HAVE_SYSCALL_H)) \
2071     && (defined(SYS_setreuid) || defined(SYS_setreuid32))
2072 static long int uwrap_syscall (long int sysno, va_list vp)
2073 {
2074         long int rc;
2075
2076         switch (sysno) {
2077                 /* gid */
2078 #ifdef __alpha__
2079                 case SYS_getxgid:
2080 #else
2081                 case SYS_getgid:
2082 #endif
2083 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2084                 case SYS_getgid32:
2085 #endif
2086                         {
2087                                 rc = uwrap_getgid();
2088                         }
2089                         break;
2090 #ifdef SYS_getegid
2091                 case SYS_getegid:
2092 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2093                 case SYS_getegid32:
2094 #endif
2095                         {
2096                                 rc = uwrap_getegid();
2097                         }
2098                         break;
2099 #endif /* SYS_getegid */
2100                 case SYS_setgid:
2101 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2102                 case SYS_setgid32:
2103 #endif
2104                         {
2105                                 gid_t gid = (gid_t) va_arg(vp, gid_t);
2106
2107                                 rc = uwrap_setgid_thread(gid);
2108                         }
2109                         break;
2110                 case SYS_setregid:
2111 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2112                 case SYS_setregid32:
2113 #endif
2114                         {
2115                                 gid_t rgid = (gid_t) va_arg(vp, gid_t);
2116                                 gid_t egid = (gid_t) va_arg(vp, gid_t);
2117
2118                                 rc = uwrap_setregid_thread(rgid, egid);
2119                         }
2120                         break;
2121 #ifdef SYS_setresgid
2122                 case SYS_setresgid:
2123 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2124                 case SYS_setresgid32:
2125 #endif
2126                         {
2127                                 gid_t rgid = (gid_t) va_arg(vp, gid_t);
2128                                 gid_t egid = (gid_t) va_arg(vp, gid_t);
2129                                 gid_t sgid = (gid_t) va_arg(vp, gid_t);
2130
2131                                 rc = uwrap_setresgid_thread(rgid, egid, sgid);
2132                         }
2133                         break;
2134 #endif /* SYS_setresgid */
2135 #if defined(SYS_getresgid) && defined(HAVE_GETRESGID)
2136                 case SYS_getresgid:
2137 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2138                 case SYS_getresgid32:
2139 #endif
2140                         {
2141                                 gid_t *rgid = (gid_t *) va_arg(vp, gid_t *);
2142                                 gid_t *egid = (gid_t *) va_arg(vp, gid_t *);
2143                                 gid_t *sgid = (gid_t *) va_arg(vp, gid_t *);
2144
2145                                 rc = uwrap_getresgid(rgid, egid, sgid);
2146                         }
2147                         break;
2148 #endif /* SYS_getresgid && HAVE_GETRESGID */
2149
2150                 /* uid */
2151 #ifdef __alpha__
2152                 case SYS_getxuid:
2153 #else
2154                 case SYS_getuid:
2155 #endif
2156 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2157                 case SYS_getuid32:
2158 #endif
2159                         {
2160                                 rc = uwrap_getuid();
2161                         }
2162                         break;
2163 #ifdef SYS_geteuid
2164                 case SYS_geteuid:
2165 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2166                 case SYS_geteuid32:
2167 #endif
2168                         {
2169                                 rc = uwrap_geteuid();
2170                         }
2171                         break;
2172 #endif /* SYS_geteuid */
2173                 case SYS_setuid:
2174 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2175                 case SYS_setuid32:
2176 #endif
2177                         {
2178                                 uid_t uid = (uid_t) va_arg(vp, uid_t);
2179
2180                                 rc = uwrap_setuid_thread(uid);
2181                         }
2182                         break;
2183                 case SYS_setreuid:
2184 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2185                 case SYS_setreuid32:
2186 #endif
2187                         {
2188                                 uid_t ruid = (uid_t) va_arg(vp, uid_t);
2189                                 uid_t euid = (uid_t) va_arg(vp, uid_t);
2190
2191                                 rc = uwrap_setreuid_thread(ruid, euid);
2192                         }
2193                         break;
2194 #ifdef SYS_setresuid
2195                 case SYS_setresuid:
2196 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2197                 case SYS_setresuid32:
2198 #endif
2199                         {
2200                                 uid_t ruid = (uid_t) va_arg(vp, uid_t);
2201                                 uid_t euid = (uid_t) va_arg(vp, uid_t);
2202                                 uid_t suid = (uid_t) va_arg(vp, uid_t);
2203
2204                                 rc = uwrap_setresuid_thread(ruid, euid, suid);
2205                         }
2206                         break;
2207 #endif /* SYS_setresuid */
2208 #if defined(SYS_getresuid) && defined(HAVE_GETRESUID)
2209                 case SYS_getresuid:
2210 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2211                 case SYS_getresuid32:
2212 #endif
2213                         {
2214                                 uid_t *ruid = (uid_t *) va_arg(vp, uid_t *);
2215                                 uid_t *euid = (uid_t *) va_arg(vp, uid_t *);
2216                                 uid_t *suid = (uid_t *) va_arg(vp, uid_t *);
2217
2218                                 rc = uwrap_getresuid(ruid, euid, suid);
2219                         }
2220                         break;
2221 #endif /* SYS_getresuid && HAVE_GETRESUID*/
2222                 /* groups */
2223                 case SYS_setgroups:
2224 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2225                 case SYS_setgroups32:
2226 #endif
2227                         {
2228                                 size_t size = (size_t) va_arg(vp, size_t);
2229                                 gid_t *list = (gid_t *) va_arg(vp, int *);
2230
2231                                 rc = uwrap_setgroups_thread(size, list);
2232                         }
2233                         break;
2234                 default:
2235                         UWRAP_LOG(UWRAP_LOG_DEBUG,
2236                                   "UID_WRAPPER calling non-wrapped syscall %lu",
2237                                   sysno);
2238
2239                         rc = libc_vsyscall(sysno, vp);
2240                         break;
2241         }
2242
2243         return rc;
2244 }
2245
2246 #ifdef HAVE_SYSCALL
2247 #ifdef HAVE_SYSCALL_INT
2248 int syscall (int sysno, ...)
2249 #else
2250 long int syscall (long int sysno, ...)
2251 #endif
2252 {
2253 #ifdef HAVE_SYSCALL_INT
2254         int rc;
2255 #else
2256         long int rc;
2257 #endif
2258         va_list va;
2259
2260         va_start(va, sysno);
2261
2262         if (!uid_wrapper_enabled()) {
2263                 rc = libc_vsyscall(sysno, va);
2264                 va_end(va);
2265                 return rc;
2266         }
2267
2268         uwrap_init();
2269         rc = uwrap_syscall(sysno, va);
2270         va_end(va);
2271
2272         return rc;
2273 }
2274 #endif /* HAVE_SYSCALL */
2275 #endif /* HAVE_SYS_SYSCALL_H || HAVE_SYSCALL_H */
2276
2277 /****************************
2278  * CONSTRUCTOR
2279  ***************************/
2280 void uwrap_constructor(void)
2281 {
2282         /*
2283         * If we hold a lock and the application forks, then the child
2284         * is not able to unlock the mutex and we are in a deadlock.
2285         * This should prevent such deadlocks.
2286         */
2287         pthread_atfork(&uwrap_thread_prepare,
2288                        &uwrap_thread_parent,
2289                        &uwrap_thread_child);
2290
2291         /* Here is safe place to call uwrap_init() and initialize data
2292          * for main process.
2293          */
2294         uwrap_init();
2295 }
2296
2297 /****************************
2298  * DESTRUCTOR
2299  ***************************/
2300
2301 /*
2302  * This function is called when the library is unloaded and makes sure that
2303  * resources are freed.
2304  */
2305 void uwrap_destructor(void)
2306 {
2307         struct uwrap_thread *u = uwrap.ids;
2308
2309         UWRAP_LOCK_ALL;
2310
2311         while (u != NULL) {
2312                 UWRAP_DLIST_REMOVE(uwrap.ids, u);
2313
2314                 SAFE_FREE(u->groups);
2315                 SAFE_FREE(u);
2316
2317                 u = uwrap.ids;
2318         }
2319
2320
2321         if (uwrap.libc.handle != NULL) {
2322                 dlclose(uwrap.libc.handle);
2323         }
2324
2325         if (uwrap.libpthread.handle != NULL) {
2326                 dlclose(uwrap.libpthread.handle);
2327         }
2328
2329         UWRAP_UNLOCK_ALL;
2330 }