• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define _GNU_SOURCE
2 #include "pwf.h"
3 #include <grp.h>
4 #include <string.h>
5 #include <limits.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <byteswap.h>
9 #include <errno.h>
10 #include "nscd.h"
11 
getgrouplist(const char * user,gid_t gid,gid_t * groups,int * ngroups)12 int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
13 {
14 	int rv, nlim, ret = -1;
15 	ssize_t i, n = 1;
16 	struct group gr;
17 	struct group *res;
18 	FILE *f;
19 	int swap = 0;
20 	int32_t resp[INITGR_LEN];
21 	uint32_t *nscdbuf = 0;
22 	char *buf = 0;
23 	char **mem = 0;
24 	size_t nmem = 0;
25 	size_t size;
26 	nlim = *ngroups;
27 	if (nlim >= 1) *groups++ = gid;
28 
29 	f = __nscd_query(GETINITGR, user, resp, sizeof resp, &swap);
30 	if (!f) goto cleanup;
31 	if (resp[INITGRFOUND]) {
32 		nscdbuf = calloc(resp[INITGRNGRPS], sizeof(uint32_t));
33 		if (!nscdbuf) goto cleanup;
34 		size_t nbytes = sizeof(*nscdbuf)*resp[INITGRNGRPS];
35 		if (nbytes && !fread(nscdbuf, nbytes, 1, f)) {
36 			if (!ferror(f)) errno = EIO;
37 			goto cleanup;
38 		}
39 		if (swap) {
40 			for (i = 0; i < resp[INITGRNGRPS]; i++)
41 				nscdbuf[i] = bswap_32(nscdbuf[i]);
42 		}
43 	}
44 	fclose(f);
45 
46 	f = fopen("/etc/group", "rbe");
47 	if (!f && errno != ENOENT && errno != ENOTDIR)
48 		goto cleanup;
49 
50 	if (f) {
51 		while (!(rv = __getgrent_a(f, &gr, &buf, &size, &mem, &nmem, &res)) && res) {
52 			if (nscdbuf)
53 				for (i=0; i < resp[INITGRNGRPS]; i++) {
54 					if (nscdbuf[i] == gr.gr_gid) nscdbuf[i] = gid;
55 				}
56 			for (i=0; gr.gr_mem[i] && strcmp(user, gr.gr_mem[i]); i++);
57 			if (!gr.gr_mem[i]) continue;
58 			if (++n <= nlim) *groups++ = gr.gr_gid;
59 		}
60 		if (rv) {
61 			errno = rv;
62 			goto cleanup;
63 		}
64 	}
65 	if (nscdbuf) {
66 		for(i=0; i < resp[INITGRNGRPS]; i++) {
67 			if (nscdbuf[i] != gid)
68 				if(++n <= nlim) *groups++ = nscdbuf[i];
69 		}
70 	}
71 
72 	ret = n > nlim ? -1 : n;
73 	*ngroups = n;
74 
75 cleanup:
76 	if (f) fclose(f);
77 	free(nscdbuf);
78 	free(buf);
79 	free(mem);
80 	return ret;
81 }
82