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/>.
34 // NOTE: This file is very similar to gr.c, keep in sync!
48 * Data contains all strings (name, passwd, gecos, dir, shell)
49 * concatenated, with their trailing NUL. The off_* variables point to
50 * beginning of each string.
53 } __attribute__((packed));
55 static bool entry_to_passwd(const struct passwd_entry *e, struct passwd *p, char *tmp, size_t space) {
56 if (space < e->data_size) {
60 memcpy(tmp, e->data, e->data_size);
61 p->pw_uid = (uid_t)e->uid;
62 p->pw_gid = (gid_t)e->gid;
64 p->pw_passwd = tmp + e->off_passwd;
65 p->pw_gecos = tmp + e->off_gecos;
66 p->pw_dir = tmp + e->off_dir;
67 p->pw_shell = tmp + e->off_shell;
73 static struct file static_file = {
76 static pthread_mutex_t static_file_lock = PTHREAD_MUTEX_INITIALIZER;
78 enum nss_status _nss_cash_setpwent(int x) {
81 pthread_mutex_lock(&static_file_lock);
82 // Unmap is necessary to detect changes when the file was replaced on
84 unmap_file(&static_file);
85 // getpwent_r will open the file if necessary when called
86 pthread_mutex_unlock(&static_file_lock);
88 return NSS_STATUS_SUCCESS;
91 enum nss_status _nss_cash_endpwent(void) {
92 pthread_mutex_lock(&static_file_lock);
93 unmap_file(&static_file);
94 pthread_mutex_unlock(&static_file_lock);
96 return NSS_STATUS_SUCCESS;
99 static enum nss_status internal_getpwent_r(struct passwd *result, char *buffer, size_t buflen) {
100 // First call to getpwent_r, load file from disk
101 if (static_file.header == NULL) {
102 if (!map_file(NSSCASH_PASSWD_FILE, &static_file)) {
103 return NSS_STATUS_UNAVAIL;
107 const struct header *h = static_file.header;
108 // End of "file", stop
109 if (static_file.next_index >= h->count) {
111 return NSS_STATUS_NOTFOUND;
114 uint64_t *off_orig = (uint64_t *)(h->data + h->off_orig_index);
115 const char *e = h->data + h->off_data + off_orig[static_file.next_index];
116 if (!entry_to_passwd((struct passwd_entry *)e, result, buffer, buflen)) {
118 return NSS_STATUS_TRYAGAIN;
120 static_file.next_index++;
122 return NSS_STATUS_SUCCESS;
124 enum nss_status _nss_cash_getpwent_r(struct passwd *result, char *buffer, size_t buflen, int *errnop) {
125 pthread_mutex_lock(&static_file_lock);
126 enum nss_status s = internal_getpwent_r(result, buffer, buflen);
127 pthread_mutex_unlock(&static_file_lock);
128 if (s != NSS_STATUS_SUCCESS) {
135 static enum nss_status internal_getpw(struct search_key *key, struct passwd *result, char *buffer, size_t buflen, int *errnop) {
137 if (!map_file(NSSCASH_PASSWD_FILE, &f)) {
139 return NSS_STATUS_UNAVAIL;
141 const struct header *h = f.header;
143 key->data = h->data + h->off_data;
144 uint64_t off_index = (key->id != NULL)
147 uint64_t *off = search(key, h->data + off_index, h->count);
152 return NSS_STATUS_NOTFOUND;
155 const char *e = h->data + h->off_data + *off;
156 if (!entry_to_passwd((struct passwd_entry *)e, result, buffer, buflen)) {
160 return NSS_STATUS_TRYAGAIN;
164 return NSS_STATUS_SUCCESS;
167 enum nss_status _nss_cash_getpwuid_r(uid_t uid, struct passwd *result, char *buffer, size_t buflen, int *errnop) {
168 uint64_t id = (uint64_t)uid;
169 struct search_key key = {
171 .offset = offsetof(struct passwd_entry, uid),
173 return internal_getpw(&key, result, buffer, buflen, errnop);
176 enum nss_status _nss_cash_getpwnam_r(const char *name, struct passwd *result, char *buffer, size_t buflen, int *errnop) {
177 struct search_key key = {
179 .offset = sizeof(struct passwd_entry), // name is first value in data[]
181 return internal_getpw(&key, result, buffer, buflen, errnop);