| 1 | #define _GNU_SOURCE |
| 2 | #include <dlfcn.h> |
| 3 | #include <errno.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <grp.h> |
| 6 | #include <stdarg.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | int open64(const char *path, int flags, ...) { |
| 11 | int (*original)(const char *, int, ...) = dlsym(RTLD_NEXT, "open64"); |
| 12 | mode_t mode = 0; |
| 13 | if (flags & O_CREAT) { |
| 14 | va_list args; |
| 15 | va_start(args, flags); |
| 16 | mode = va_arg(args, unsigned int); |
| 17 | va_end(args); |
| 18 | } |
| 19 | if (!strcmp(path, "/etc/passwd")) { |
| 20 | path = getenv("ZIPELO_GROUP_PASSWD"); |
| 21 | } else if (!strcmp(path, "/etc/nsswitch.conf")) { |
| 22 | path = getenv("ZIPELO_GROUP_NSS"); |
| 23 | } |
| 24 | if (!path) { |
| 25 | errno = EACCES; |
| 26 | return -1; |
| 27 | } |
| 28 | return original(path, flags, mode); |
| 29 | } |
| 30 | |
| 31 | int getgrgid_r(gid_t gid, struct group *group, char *buffer, size_t length, |
| 32 | struct group **result) { |
| 33 | (void)buffer; |
| 34 | (void)length; |
| 35 | const char *phase = getenv("ZIPELO_GROUP_CASE"); |
| 36 | *result = NULL; |
| 37 | if (!phase || !strcmp(phase, "group-error")) { |
| 38 | return EIO; |
| 39 | } |
| 40 | if (!strcmp(phase, "group-missing")) { |
| 41 | return 0; |
| 42 | } |
| 43 | static char *empty[] = {NULL}; |
| 44 | static char *shared[] = {"other", NULL}; |
| 45 | *group = (struct group){ |
| 46 | .gr_name = "fixture", |
| 47 | .gr_passwd = "x", |
| 48 | .gr_gid = !strcmp(phase, "group-wrong-gid") ? gid + 1 : gid, |
| 49 | .gr_mem = !strcmp(phase, "group-null-members") ? NULL : |
| 50 | (!strcmp(phase, "supplementary-member") ? shared : empty) |
| 51 | }; |
| 52 | *result = group; |
| 53 | return 0; |
| 54 | } |