2 * Handle passwd entries via struct passwd
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 gr.c, keep in sync!
39 // off_name = 0, not stored on disk
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.
50 uint16_t data_size; // size of data in bytes
52 } __attribute__((packed));
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) {
59 memcpy(tmp, e->data, e->data_size);
60 p->pw_uid = (uid_t)e->uid;
61 p->pw_gid = (gid_t)e->gid;
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;
72 static struct file static_file = {
75 static pthread_mutex_t static_file_lock = PTHREAD_MUTEX_INITIALIZER;
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);
83 enum nss_status _nss_cash_setpwent(int x) {
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;
92 enum nss_status _nss_cash_endpwent(void) {
93 internal_unmap_static_file();
94 return NSS_STATUS_SUCCESS;
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;
105 const struct header *h = static_file.header;
106 // End of "file", stop
107 if (static_file.next_index >= h->count) {
109 return NSS_STATUS_NOTFOUND;
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)) {
116 return NSS_STATUS_TRYAGAIN;
118 static_file.next_index++;
120 return NSS_STATUS_SUCCESS;
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) {
133 static enum nss_status internal_getpw(struct search_key *key, struct passwd *result, char *buffer, size_t buflen, int *errnop) {
135 if (!map_file(NSSCASH_PASSWD_FILE, &f)) {
137 return NSS_STATUS_UNAVAIL;
139 const struct header *h = f.header;
141 key->data = h->data + h->off_data;
142 uint64_t off_index = (key->name != NULL)
145 uint64_t *off = search(key, h->data + off_index, h->count);
150 return NSS_STATUS_NOTFOUND;
153 const char *e = key->data + *off;
154 if (!entry_to_passwd((struct passwd_entry *)e, result, buffer, buflen)) {
158 return NSS_STATUS_TRYAGAIN;
162 return NSS_STATUS_SUCCESS;
165 enum nss_status _nss_cash_getpwuid_r(uid_t uid, struct passwd *result, char *buffer, size_t buflen, int *errnop) {
166 struct search_key key = {
168 .offset = offsetof(struct passwd_entry, uid),
170 return internal_getpw(&key, result, buffer, buflen, errnop);
173 enum nss_status _nss_cash_getpwnam_r(const char *name, struct passwd *result, char *buffer, size_t buflen, int *errnop) {
174 struct search_key key = {
176 .offset = sizeof(struct passwd_entry), // name is first value in data[]
178 return internal_getpw(&key, result, buffer, buflen, errnop);