2 * Handle group entries via struct group
4 * Copyright (C) 2019 Simon Ruderich
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
33 // NOTE: This file is very similar to pw.c, keep in sync!
42 uint16_t mem_count; // group member count
45 * Data contains all strings (name, passwd) concatenated, with their
46 * trailing NUL. The off_* variables point to beginning of each string.
48 * After that the offsets of the members of the group are stored as
49 * mem_count uint16_t values, followed by the member names concatenated as
50 * with the strings above.
52 * All offsets are relative to the beginning of data.
56 } __attribute__((packed));
58 static bool entry_to_group(const struct group_entry *e, struct group *g, char *tmp, size_t space) {
59 const size_t mem_size = (size_t)(e->mem_count + 1) * sizeof(char *);
61 if (space < e->data_size + mem_size) {
65 char **groups = (char **)tmp;
67 const uint16_t *offs_mem = (const uint16_t *)(e->data + e->off_mem_off);
68 for (uint16_t i = 0; i < e->mem_count; i++) {
69 groups[i] = tmp + mem_size + offs_mem[i];
71 groups[e->mem_count] = NULL;
73 // This unnecessarily copies offs_mem[] as well but keeps the code simpler
74 // and the meaning of variables consistent with pw.c
75 memcpy(tmp + mem_size, e->data, e->data_size);
77 g->gr_gid = (gid_t)e->gid;
78 g->gr_name = tmp + mem_size + 0;
79 g->gr_passwd = tmp + mem_size + e->off_passwd;
86 static struct file static_file = {
89 static pthread_mutex_t static_file_lock = PTHREAD_MUTEX_INITIALIZER;
91 enum nss_status _nss_cash_setgrent(int x) {
94 pthread_mutex_lock(&static_file_lock);
95 // Unmap is necessary to detect changes when the file was replaced on
97 unmap_file(&static_file);
98 // getgrent_r will open the file if necessary when called
99 pthread_mutex_unlock(&static_file_lock);
101 return NSS_STATUS_SUCCESS;
104 enum nss_status _nss_cash_endgrent(void) {
105 pthread_mutex_lock(&static_file_lock);
106 unmap_file(&static_file);
107 pthread_mutex_unlock(&static_file_lock);
109 return NSS_STATUS_SUCCESS;
112 static enum nss_status internal_getgrent_r(struct group *result, char *buffer, size_t buflen) {
113 // First call to getgrent_r, load file from disk
114 if (static_file.header == NULL) {
115 if (!map_file(NSSCASH_GROUP_FILE, &static_file)) {
116 return NSS_STATUS_UNAVAIL;
120 const struct header *h = static_file.header;
121 // End of "file", stop
122 if (static_file.next_index >= h->count) {
124 return NSS_STATUS_NOTFOUND;
127 uint64_t *off_orig = (uint64_t *)(h->data + h->off_orig_index);
128 const char *e = h->data + h->off_data + off_orig[static_file.next_index];
129 if (!entry_to_group((struct group_entry *)e, result, buffer, buflen)) {
131 return NSS_STATUS_TRYAGAIN;
133 static_file.next_index++;
135 return NSS_STATUS_SUCCESS;
137 enum nss_status _nss_cash_getgrent_r(struct group *result, char *buffer, size_t buflen, int *errnop) {
138 pthread_mutex_lock(&static_file_lock);
139 enum nss_status s = internal_getgrent_r(result, buffer, buflen);
140 pthread_mutex_unlock(&static_file_lock);
141 if (s != NSS_STATUS_SUCCESS) {
148 static enum nss_status internal_getgr(struct search_key *key, struct group *result, char *buffer, size_t buflen, int *errnop) {
150 if (!map_file(NSSCASH_GROUP_FILE, &f)) {
152 return NSS_STATUS_UNAVAIL;
154 const struct header *h = f.header;
156 key->data = h->data + h->off_data;
157 uint64_t off_index = (key->id != NULL)
160 uint64_t *off = search(key, h->data + off_index, h->count);
165 return NSS_STATUS_NOTFOUND;
168 const char *e = h->data + h->off_data + *off;
169 if (!entry_to_group((struct group_entry *)e, result, buffer, buflen)) {
173 return NSS_STATUS_TRYAGAIN;
177 return NSS_STATUS_SUCCESS;
180 enum nss_status _nss_cash_getgrgid_r(gid_t gid, struct group *result, char *buffer, size_t buflen, int *errnop) {
181 uint64_t id = (uint64_t)gid;
182 struct search_key key = {
184 .offset = offsetof(struct group_entry, gid),
186 return internal_getgr(&key, result, buffer, buflen, errnop);
189 enum nss_status _nss_cash_getgrnam_r(const char *name, struct group *result, char *buffer, size_t buflen, int *errnop) {
190 struct search_key key = {
192 .offset = sizeof(struct group_entry), // name is first value in data[]
194 return internal_getgr(&key, result, buffer, buflen, errnop);