]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blob - nss/gr.c
nss: merge cash.h into file.h
[nsscash/nsscash.git] / nss / gr.c
1 /*
2  * Handle group entries via struct group
3  *
4  * Copyright (C) 2019  Simon Ruderich
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <errno.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <pthread.h>
27
28 #include "cash_nss.h"
29 #include "file.h"
30 #include "search.h"
31
32
33 // NOTE: This file is very similar to pw.c, keep in sync!
34
35 struct group_entry {
36     uint64_t gid;
37
38     //       off_name = 0
39     uint16_t off_passwd;
40     uint16_t off_mem_off;
41
42     uint16_t mem_count; // group member count
43
44     /*
45      * Data contains all strings (name, passwd) concatenated, with their
46      * trailing NUL. The off_* variables point to beginning of each string.
47      *
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.
51      *
52      * All offsets are relative to the beginning of data.
53      */
54     uint16_t data_size;
55     char data[];
56 } __attribute__((packed));
57
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 *);
60
61     if (space < e->data_size + mem_size) {
62         return false;
63     }
64
65     char **groups = (char **)tmp;
66
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];
70     }
71     groups[e->mem_count] = NULL;
72
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);
76
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;
80     g->gr_mem = groups;
81
82     return true;
83 }
84
85
86 static struct file static_file = {
87     .fd = -1,
88 };
89 static pthread_mutex_t static_file_lock = PTHREAD_MUTEX_INITIALIZER;
90
91 enum nss_status _nss_cash_setgrent(int x) {
92     (void)x;
93
94     pthread_mutex_lock(&static_file_lock);
95     // Unmap is necessary to detect changes when the file was replaced on
96     // disk
97     unmap_file(&static_file);
98     // getgrent_r will open the file if necessary when called
99     pthread_mutex_unlock(&static_file_lock);
100
101     return NSS_STATUS_SUCCESS;
102 }
103
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);
108
109     return NSS_STATUS_SUCCESS;
110 }
111
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;
117         }
118     }
119
120     const struct header *h = static_file.header;
121     // End of "file", stop
122     if (static_file.next_index >= h->count) {
123         errno = ENOENT;
124         return NSS_STATUS_NOTFOUND;
125     }
126
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)) {
130         errno = ERANGE;
131         return NSS_STATUS_TRYAGAIN;
132     }
133     static_file.next_index++;
134
135     return NSS_STATUS_SUCCESS;
136 }
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) {
142         *errnop = errno;
143     }
144     return s;
145 }
146
147
148 static enum nss_status internal_getgr(struct search_key *key, struct group *result, char *buffer, size_t buflen, int *errnop) {
149     struct file f;
150     if (!map_file(NSSCASH_GROUP_FILE, &f)) {
151         *errnop = errno;
152         return NSS_STATUS_UNAVAIL;
153     }
154     const struct header *h = f.header;
155
156     key->data = h->data + h->off_data;
157     uint64_t off_index = (key->id != NULL)
158                        ? h->off_id_index
159                        : h->off_name_index;
160     uint64_t *off = search(key, h->data + off_index, h->count);
161     if (off == NULL) {
162         unmap_file(&f);
163         errno = ENOENT;
164         *errnop = errno;
165         return NSS_STATUS_NOTFOUND;
166     }
167
168     const char *e = h->data + h->off_data + *off;
169     if (!entry_to_group((struct group_entry *)e, result, buffer, buflen)) {
170         unmap_file(&f);
171         errno = ERANGE;
172         *errnop = errno;
173         return NSS_STATUS_TRYAGAIN;
174     }
175
176     unmap_file(&f);
177     return NSS_STATUS_SUCCESS;
178 }
179
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 = {
183         .id = &id,
184         .offset = offsetof(struct group_entry, gid),
185     };
186     return internal_getgr(&key, result, buffer, buflen, errnop);
187 }
188
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 = {
191         .name = name,
192         .offset = sizeof(struct group_entry), // name is first value in data[]
193     };
194     return internal_getgr(&key, result, buffer, buflen, errnop);
195 }