2 * Copyright (c) 2023 Klara, Inc.
4 * SPDX-License-Identifier: BSD-2-Clause
8 #include <sys/limits.h>
13 #include <pthread_np.h>
23 extern int __isthreaded;
29 #define DEF_SHELL "/bin/sh"
31 ns_mtab *nss_module_register(const char *, unsigned int *,
32 nss_module_unregister_fn *);
35 tacplus_error(struct tac_handle *h, const char *func)
38 syslog(LOG_ERR, "%s(): %m", func);
40 syslog(LOG_ERR, "%s(): %s", func, tac_strerror(h));
43 static pthread_key_t tacplus_key;
48 struct tac_handle **h = p;
57 (void)pthread_key_create(&tacplus_key, tacplus_fini);
60 static struct tac_handle *
61 tacplus_get_handle(void)
63 static pthread_once_t keyinit = PTHREAD_ONCE_INIT;
64 static struct tac_handle *sth;
65 struct tac_handle **h = &sth;
68 if (__isthreaded && !pthread_main_np()) {
69 if ((ret = pthread_once(&keyinit, tacplus_keyinit)) != 0)
71 if ((h = pthread_getspecific(tacplus_key)) == NULL) {
72 if ((h = calloc(1, sizeof(*h))) == NULL)
74 if ((pthread_setspecific(tacplus_key, h)) != 0) {
81 if ((*h = tac_open()) == NULL) {
82 tacplus_error(*h, "tac_open");
85 if (tac_config(*h, NULL) != 0) {
86 tacplus_error(*h, "tac_config");
96 tacplus_copystr(const char *str, char **buffer, size_t *bufsize)
99 size_t len = strlen(str) + 1;
101 if (len > *bufsize) {
105 memcpy(copy, str, len);
112 tacplus_getpwnam_r(const char *name, struct passwd *pwd, char *buffer,
115 struct tac_handle *h;
116 char *av, *key, *value, *end;
120 if ((h = tacplus_get_handle()) == NULL)
122 ret = tac_create_author(h, TAC_AUTHEN_METH_NOT_SET,
123 TAC_AUTHEN_TYPE_NOT_SET, TAC_AUTHEN_SVC_LOGIN);
125 tacplus_error(h, "tac_create_author");
126 return (NS_TRYAGAIN);
128 if (tac_set_user(h, name) < 0) {
129 tacplus_error(h, "tac_set_user");
130 return (NS_TRYAGAIN);
132 if (tac_set_av(h, 0, "service=shell") < 0) {
133 tacplus_error(h, "tac_set_av");
134 return (NS_TRYAGAIN);
136 ret = tac_send_author(h);
137 switch (TAC_AUTHOR_STATUS(ret)) {
138 case TAC_AUTHOR_STATUS_PASS_ADD:
139 case TAC_AUTHOR_STATUS_PASS_REPL:
142 case TAC_AUTHOR_STATUS_FAIL:
143 return (NS_NOTFOUND);
144 case TAC_AUTHOR_STATUS_ERROR:
147 tacplus_error(h, "tac_send_author");
150 memset(pwd, 0, sizeof(*pwd));
153 pwd->pw_name = tacplus_copystr(name, &buffer, &bufsize);
154 if (pwd->pw_name == NULL)
158 pwd->pw_passwd = tacplus_copystr("*", &buffer, &bufsize);
162 /* default uid and gid */
163 pwd->pw_uid = DEF_UID;
164 pwd->pw_gid = DEF_GID;
166 /* get attribute-value pairs from TACACS+ response */
167 for (i = 0; i < TAC_AUTHEN_AV_COUNT(ret); i++) {
168 if ((av = tac_get_av(h, i)) == NULL) {
169 tacplus_error(h, "tac_get_av");
173 if ((value = strchr(av, '=')) == NULL) {
178 if (strcasecmp(key, "uid") == 0) {
179 num = strtoimax(value, &end, 10);
180 if (end == value || *end != '\0' ||
181 num < 0 || num > (intmax_t)UID_MAX) {
187 } else if (strcasecmp(key, "gid") == 0) {
188 num = strtoimax(value, &end, 10);
189 if (end == value || *end != '\0' ||
190 num < 0 || num > (intmax_t)GID_MAX) {
196 } else if (strcasecmp(av, "class") == 0) {
197 pwd->pw_class = tacplus_copystr(value, &buffer,
199 if (pwd->pw_class == NULL) {
203 } else if (strcasecmp(av, "gecos") == 0) {
204 pwd->pw_gecos = tacplus_copystr(value, &buffer,
206 if (pwd->pw_gecos == NULL) {
210 } else if (strcasecmp(av, "home") == 0) {
211 pwd->pw_dir = tacplus_copystr(value, &buffer,
213 if (pwd->pw_dir == NULL) {
217 } else if (strcasecmp(av, "shell") == 0) {
218 pwd->pw_shell = tacplus_copystr(value, &buffer,
220 if (pwd->pw_shell == NULL) {
228 /* default class if none was provided */
229 if (pwd->pw_class == NULL)
230 pwd->pw_class = tacplus_copystr(DEF_CLASS, &buffer, &bufsize);
232 /* gecos equal to name if none was provided */
233 if (pwd->pw_gecos == NULL)
234 pwd->pw_gecos = pwd->pw_name;
236 /* default home directory if none was provided */
237 if (pwd->pw_dir == NULL)
238 pwd->pw_dir = tacplus_copystr(DEF_DIR, &buffer, &bufsize);
239 if (pwd->pw_dir == NULL)
242 /* default shell if none was provided */
243 if (pwd->pw_shell == NULL)
244 pwd->pw_shell = tacplus_copystr(DEF_SHELL, &buffer, &bufsize);
245 if (pwd->pw_shell == NULL)
253 nss_tacplus_getpwnam_r(void *retval, void *mdata __unused, va_list ap)
255 char *name = va_arg(ap, char *);
256 struct passwd *pwd = va_arg(ap, struct passwd *);
257 char *buffer = va_arg(ap, char *);
258 size_t bufsize = va_arg(ap, size_t);
259 int *result = va_arg(ap, int *);
263 ret = tacplus_getpwnam_r(name, pwd, buffer, bufsize);
264 if (ret == NS_SUCCESS) {
265 *(void **)retval = pwd;
268 *(void **)retval = NULL;
275 nss_tacplus_setpwent(void *retval __unused, void *mdata __unused,
282 nss_tacplus_getpwent_r(void *retval, void *mdata __unused, va_list ap)
284 struct passwd *pwd __unused = va_arg(ap, struct passwd *);
285 char *buffer __unused = va_arg(ap, char *);
286 size_t bufsize __unused = va_arg(ap, size_t);
287 int *result = va_arg(ap, int *);
289 *(void **)retval = NULL;
296 nss_tacplus_endpwent(void *retval __unused, void *mdata __unused,
303 nss_module_register(const char *name __unused, unsigned int *plen,
304 nss_module_unregister_fn *unreg)
306 static ns_mtab mtab[] = {
307 { "passwd", "getpwnam_r", &nss_tacplus_getpwnam_r, NULL },
308 { "passwd", "setpwent", &nss_tacplus_setpwent, NULL },
309 { "passwd", "getpwent_r", &nss_tacplus_getpwent_r, NULL },
310 { "passwd", "endpwent", &nss_tacplus_endpwent, NULL },
313 *plen = nitems(mtab);