]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blob - nss/gr.c
c49ef4799be15db6e4996b4c01042eb5965d8a75
[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.h"
29 #include "cash_nss.h"
30 #include "file.h"
31 #include "search.h"
32
33
34 // NOTE: This file is very similar to pw.c, keep in sync!
35
36 struct group_entry {
37     uint64_t gid;
38
39     //       off_name = 0
40     uint16_t off_passwd;
41     uint16_t off_mem_off;
42
43     uint16_t mem_count; // group member count
44
45     /*
46      * Data contains all strings (name, passwd) concatenated, with their
47      * trailing NUL. The off_* variables point to beginning of each string.
48      *
49      * After that the offsets of the members of the group are stored as
50      * mem_count uint16_t values, followed by the member names concatenated as
51      * with the strings above.
52      *
53      * All offsets are relative to the beginning of data.
54      */
55     uint16_t data_size;
56     char data[];
57 } __attribute__((packed));
58
59 static bool entry_to_group(const struct group_entry *e, struct group *g, char *tmp, size_t space) {
60     const size_t mem_size = (size_t)(e->mem_count + 1) * sizeof(char *);
61
62     if (space < e->data_size + mem_size) {
63         return false;
64     }
65
66     char **groups = (char **)tmp;
67
68     const uint16_t *offs_mem = (const uint16_t *)(e->data + e->off_mem_off);
69     for (uint16_t i = 0; i < e->mem_count; i++) {
70         groups[i] = tmp + mem_size + offs_mem[i];
71     }
72     groups[e->mem_count] = NULL;
73
74     // This unnecessarily copies offs_mem[] as well but keeps the code simpler
75     // and the meaning of variables consistent with pw.c
76     memcpy(tmp + mem_size, e->data, e->data_size);
77
78     g->gr_gid = (gid_t)e->gid;
79     g->gr_name = tmp + mem_size + 0;
80     g->gr_passwd = tmp + mem_size + e->off_passwd;
81     g->gr_mem = groups;
82
83     return true;
84 }
85
86
87 static struct file static_file = {
88     .fd = -1,
89 };
90 static pthread_mutex_t static_file_lock = PTHREAD_MUTEX_INITIALIZER;
91
92 enum nss_status _nss_cash_setgrent(int x) {
93     (void)x;
94
95     pthread_mutex_lock(&static_file_lock);
96     // Unmap is necessary to detect changes when the file was replaced on
97     // disk
98     unmap_file(&static_file);
99     // getgrent_r will open the file if necessary when called
100     pthread_mutex_unlock(&static_file_lock);
101
102     return NSS_STATUS_SUCCESS;
103 }
104
105 enum nss_status _nss_cash_endgrent(void) {
106     pthread_mutex_lock(&static_file_lock);
107     unmap_file(&static_file);
108     pthread_mutex_unlock(&static_file_lock);
109
110     return NSS_STATUS_SUCCESS;
111 }
112
113 static enum nss_status internal_getgrent_r(struct group *result, char *buffer, size_t buflen) {
114     // First call to getgrent_r, load file from disk
115     if (static_file.header == NULL) {
116         if (!map_file(NSSCASH_GROUP_FILE, &static_file)) {
117             return NSS_STATUS_UNAVAIL;
118         }
119     }
120
121     const struct header *h = static_file.header;
122     // End of "file", stop
123     if (static_file.next_index >= h->count) {
124         errno = ENOENT;
125         return NSS_STATUS_NOTFOUND;
126     }
127
128     uint64_t *off_orig = (uint64_t *)(h->data + h->off_orig_index);
129     const char *e = h->data + h->off_data + off_orig[static_file.next_index];
130     if (!entry_to_group((struct group_entry *)e, result, buffer, buflen)) {
131         errno = ERANGE;
132         return NSS_STATUS_TRYAGAIN;
133     }
134     static_file.next_index++;
135
136     return NSS_STATUS_SUCCESS;
137 }
138 enum nss_status _nss_cash_getgrent_r(struct group *result, char *buffer, size_t buflen, int *errnop) {
139     pthread_mutex_lock(&static_file_lock);
140     enum nss_status s = internal_getgrent_r(result, buffer, buflen);
141     pthread_mutex_unlock(&static_file_lock);
142     if (s != NSS_STATUS_SUCCESS) {
143         *errnop = errno;
144     }
145     return s;
146 }
147
148
149 static enum nss_status internal_getgr(struct search_key *key, struct group *result, char *buffer, size_t buflen, int *errnop) {
150     struct file f;
151     if (!map_file(NSSCASH_GROUP_FILE, &f)) {
152         *errnop = errno;
153         return NSS_STATUS_UNAVAIL;
154     }
155     const struct header *h = f.header;
156
157     key->data = h->data + h->off_data;
158     uint64_t off_index = (key->id != NULL)
159                        ? h->off_id_index
160                        : h->off_name_index;
161     uint64_t *off = search(key, h->data + off_index, h->count);
162     if (off == NULL) {
163         unmap_file(&f);
164         errno = ENOENT;
165         *errnop = errno;
166         return NSS_STATUS_NOTFOUND;
167     }
168
169     const char *e = h->data + h->off_data + *off;
170     if (!entry_to_group((struct group_entry *)e, result, buffer, buflen)) {
171         unmap_file(&f);
172         errno = ERANGE;
173         *errnop = errno;
174         return NSS_STATUS_TRYAGAIN;
175     }
176
177     unmap_file(&f);
178     return NSS_STATUS_SUCCESS;
179 }
180
181 enum nss_status _nss_cash_getgrgid_r(gid_t gid, struct group *result, char *buffer, size_t buflen, int *errnop) {
182     uint64_t id = (uint64_t)gid;
183     struct search_key key = {
184         .id = &id,
185         .offset = offsetof(struct group_entry, gid),
186     };
187     return internal_getgr(&key, result, buffer, buflen, errnop);
188 }
189
190 enum nss_status _nss_cash_getgrnam_r(const char *name, struct group *result, char *buffer, size_t buflen, int *errnop) {
191     struct search_key key = {
192         .name = name,
193         .offset = sizeof(struct group_entry), // name is first value in data[]
194     };
195     return internal_getgr(&key, result, buffer, buflen, errnop);
196 }