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