]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blob - nss/pw.c
c612026aa132a96cd7a6a303016e7bc7a27d5ad3
[nsscash/nsscash.git] / nss / pw.c
1 /*
2  * Handle passwd entries via struct passwd
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 gr.c, keep in sync!
34
35 struct passwd_entry {
36     uint64_t uid;
37     uint64_t gid;
38
39     //       off_name = 0, not stored on disk
40     uint16_t off_passwd;
41     uint16_t off_gecos;
42     uint16_t off_dir;
43     uint16_t off_shell;
44
45     /*
46      * Data contains all strings (name, passwd, gecos, dir, shell)
47      * concatenated, with their trailing NUL. The off_* variables point to
48      * beginning of each string.
49      */
50     uint16_t data_size; // size of data in bytes
51     const char data[];
52 } __attribute__((packed));
53
54 static bool entry_to_passwd(const struct passwd_entry *e, struct passwd *p, char *tmp, size_t space) {
55     if (space < e->data_size) {
56         return false;
57     }
58
59     memcpy(tmp, e->data, e->data_size);
60     p->pw_uid = (uid_t)e->uid;
61     p->pw_gid = (gid_t)e->gid;
62     p->pw_name = tmp + 0;
63     p->pw_passwd = tmp + e->off_passwd;
64     p->pw_gecos = tmp + e->off_gecos;
65     p->pw_dir = tmp + e->off_dir;
66     p->pw_shell = tmp + e->off_shell;
67
68     return true;
69 }
70
71
72 static struct file static_file = {
73     .fd = -1,
74 };
75 static pthread_mutex_t static_file_lock = PTHREAD_MUTEX_INITIALIZER;
76
77 static void internal_unmap_static_file(void) {
78     pthread_mutex_lock(&static_file_lock);
79     unmap_file(&static_file);
80     pthread_mutex_unlock(&static_file_lock);
81 }
82
83 enum nss_status _nss_cash_setpwent(int x) {
84     (void)x;
85
86     // Unmap is necessary to detect changes when the file was replaced on
87     // disk; getpwent_r will open the file if necessary when called
88     internal_unmap_static_file();
89     return NSS_STATUS_SUCCESS;
90 }
91
92 enum nss_status _nss_cash_endpwent(void) {
93     internal_unmap_static_file();
94     return NSS_STATUS_SUCCESS;
95 }
96
97 static enum nss_status internal_getpwent_r(struct passwd *result, char *buffer, size_t buflen) {
98     // First call to getpwent_r, load file from disk
99     if (static_file.header == NULL) {
100         if (!map_file(NSSCASH_PASSWD_FILE, &static_file)) {
101             return NSS_STATUS_UNAVAIL;
102         }
103     }
104
105     const struct header *h = static_file.header;
106     // End of "file", stop
107     if (static_file.next_index >= h->count) {
108         errno = ENOENT;
109         return NSS_STATUS_NOTFOUND;
110     }
111
112     uint64_t *off_orig = (uint64_t *)(h->data + h->off_orig_index);
113     const char *e = h->data + h->off_data + off_orig[static_file.next_index];
114     if (!entry_to_passwd((struct passwd_entry *)e, result, buffer, buflen)) {
115         errno = ERANGE;
116         return NSS_STATUS_TRYAGAIN;
117     }
118     static_file.next_index++;
119
120     return NSS_STATUS_SUCCESS;
121 }
122 enum nss_status _nss_cash_getpwent_r(struct passwd *result, char *buffer, size_t buflen, int *errnop) {
123     pthread_mutex_lock(&static_file_lock);
124     enum nss_status s = internal_getpwent_r(result, buffer, buflen);
125     pthread_mutex_unlock(&static_file_lock);
126     if (s != NSS_STATUS_SUCCESS) {
127         *errnop = errno;
128     }
129     return s;
130 }
131
132
133 static enum nss_status internal_getpw(struct search_key *key, struct passwd *result, char *buffer, size_t buflen, int *errnop) {
134     struct file f;
135     if (!map_file(NSSCASH_PASSWD_FILE, &f)) {
136         *errnop = errno;
137         return NSS_STATUS_UNAVAIL;
138     }
139     const struct header *h = f.header;
140
141     key->data = h->data + h->off_data;
142     uint64_t off_index = (key->name != NULL)
143                        ? h->off_name_index
144                        : h->off_id_index;
145     uint64_t *off = search(key, h->data + off_index, h->count);
146     if (off == NULL) {
147         unmap_file(&f);
148         errno = ENOENT;
149         *errnop = errno;
150         return NSS_STATUS_NOTFOUND;
151     }
152
153     const char *e = key->data + *off;
154     if (!entry_to_passwd((struct passwd_entry *)e, result, buffer, buflen)) {
155         unmap_file(&f);
156         errno = ERANGE;
157         *errnop = errno;
158         return NSS_STATUS_TRYAGAIN;
159     }
160
161     unmap_file(&f);
162     return NSS_STATUS_SUCCESS;
163 }
164
165 enum nss_status _nss_cash_getpwuid_r(uid_t uid, struct passwd *result, char *buffer, size_t buflen, int *errnop) {
166     uint64_t id = (uint64_t)uid;
167     struct search_key key = {
168         .id = &id,
169         .offset = offsetof(struct passwd_entry, uid),
170     };
171     return internal_getpw(&key, result, buffer, buflen, errnop);
172 }
173
174 enum nss_status _nss_cash_getpwnam_r(const char *name, struct passwd *result, char *buffer, size_t buflen, int *errnop) {
175     struct search_key key = {
176         .name = name,
177         .offset = sizeof(struct passwd_entry), // name is first value in data[]
178     };
179     return internal_getpw(&key, result, buffer, buflen, errnop);
180 }