]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blob - nss/pw.c
nss: merge cash.h into file.h
[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
40     uint16_t off_passwd;
41     uint16_t off_gecos;
42     uint16_t off_dir;
43     uint16_t off_shell;
44
45     uint16_t data_size;
46     /*
47      * Data contains all strings (name, passwd, gecos, dir, shell)
48      * concatenated, with their trailing NUL. The off_* variables point to
49      * beginning of each string.
50      */
51     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 enum nss_status _nss_cash_setpwent(int x) {
78     (void)x;
79
80     pthread_mutex_lock(&static_file_lock);
81     // Unmap is necessary to detect changes when the file was replaced on
82     // disk
83     unmap_file(&static_file);
84     // getpwent_r will open the file if necessary when called
85     pthread_mutex_unlock(&static_file_lock);
86
87     return NSS_STATUS_SUCCESS;
88 }
89
90 enum nss_status _nss_cash_endpwent(void) {
91     pthread_mutex_lock(&static_file_lock);
92     unmap_file(&static_file);
93     pthread_mutex_unlock(&static_file_lock);
94
95     return NSS_STATUS_SUCCESS;
96 }
97
98 static enum nss_status internal_getpwent_r(struct passwd *result, char *buffer, size_t buflen) {
99     // First call to getpwent_r, load file from disk
100     if (static_file.header == NULL) {
101         if (!map_file(NSSCASH_PASSWD_FILE, &static_file)) {
102             return NSS_STATUS_UNAVAIL;
103         }
104     }
105
106     const struct header *h = static_file.header;
107     // End of "file", stop
108     if (static_file.next_index >= h->count) {
109         errno = ENOENT;
110         return NSS_STATUS_NOTFOUND;
111     }
112
113     uint64_t *off_orig = (uint64_t *)(h->data + h->off_orig_index);
114     const char *e = h->data + h->off_data + off_orig[static_file.next_index];
115     if (!entry_to_passwd((struct passwd_entry *)e, result, buffer, buflen)) {
116         errno = ERANGE;
117         return NSS_STATUS_TRYAGAIN;
118     }
119     static_file.next_index++;
120
121     return NSS_STATUS_SUCCESS;
122 }
123 enum nss_status _nss_cash_getpwent_r(struct passwd *result, char *buffer, size_t buflen, int *errnop) {
124     pthread_mutex_lock(&static_file_lock);
125     enum nss_status s = internal_getpwent_r(result, buffer, buflen);
126     pthread_mutex_unlock(&static_file_lock);
127     if (s != NSS_STATUS_SUCCESS) {
128         *errnop = errno;
129     }
130     return s;
131 }
132
133
134 static enum nss_status internal_getpw(struct search_key *key, struct passwd *result, char *buffer, size_t buflen, int *errnop) {
135     struct file f;
136     if (!map_file(NSSCASH_PASSWD_FILE, &f)) {
137         *errnop = errno;
138         return NSS_STATUS_UNAVAIL;
139     }
140     const struct header *h = f.header;
141
142     key->data = h->data + h->off_data;
143     uint64_t off_index = (key->id != NULL)
144                        ? h->off_id_index
145                        : h->off_name_index;
146     uint64_t *off = search(key, h->data + off_index, h->count);
147     if (off == NULL) {
148         unmap_file(&f);
149         errno = ENOENT;
150         *errnop = errno;
151         return NSS_STATUS_NOTFOUND;
152     }
153
154     const char *e = h->data + h->off_data + *off;
155     if (!entry_to_passwd((struct passwd_entry *)e, result, buffer, buflen)) {
156         unmap_file(&f);
157         errno = ERANGE;
158         *errnop = errno;
159         return NSS_STATUS_TRYAGAIN;
160     }
161
162     unmap_file(&f);
163     return NSS_STATUS_SUCCESS;
164 }
165
166 enum nss_status _nss_cash_getpwuid_r(uid_t uid, struct passwd *result, char *buffer, size_t buflen, int *errnop) {
167     uint64_t id = (uint64_t)uid;
168     struct search_key key = {
169         .id = &id,
170         .offset = offsetof(struct passwd_entry, uid),
171     };
172     return internal_getpw(&key, result, buffer, buflen, errnop);
173 }
174
175 enum nss_status _nss_cash_getpwnam_r(const char *name, struct passwd *result, char *buffer, size_t buflen, int *errnop) {
176     struct search_key key = {
177         .name = name,
178         .offset = sizeof(struct passwd_entry), // name is first value in data[]
179     };
180     return internal_getpw(&key, result, buffer, buflen, errnop);
181 }