Whamcloud - gitweb
LU-13048 mdd: allow release after a non-blocking migrate
[fs/lustre-release.git] / lustre / utils / l_getidentity.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #include <stdbool.h>
33 #include <stdlib.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <fcntl.h>
40 #include <pwd.h>
41 #include <grp.h>
42 #include <stdarg.h>
43 #include <stddef.h>
44 #include <libgen.h>
45 #include <syslog.h>
46 #include <sys/time.h>
47 #include <limits.h>
48 #include <ctype.h>
49 #include <nss.h>
50 #include <dlfcn.h>
51
52 #include <libcfs/util/param.h>
53 #include <linux/lnet/nidstr.h>
54 #include <linux/lustre/lustre_user.h>
55 #include <linux/lustre/lustre_idl.h>
56
57 #define PERM_PATHNAME "/etc/lustre/perm.conf"
58 #define LUSTRE_PASSWD "/etc/lustre/passwd"
59 #define LUSTRE_GROUP  "/etc/lustre/group"
60
61 #define L_GETIDENTITY_LOOKUP_CMD "lookup"
62 #define NSS_MODULES_MAX_NR 8
63 #define NSS_MODULE_NAME_SIZE 32
64 #define NSS_SYMBOL_NAME_LEN_MAX 256
65
66 static int nss_pw_buf_len;
67 static void *nss_pw_buf;
68 static int nss_grent_buf_len;
69 static void *nss_grent_buf;
70 static int g_n_nss_modules;
71 static int grent_mod_no = -1;
72
73 struct nss_module {
74         char name[NSS_MODULE_NAME_SIZE];
75         int (*getpwuid)(struct nss_module *mod, uid_t, struct passwd *pwd);
76         int (*getgrent)(struct nss_module *mod, struct group *result);
77         void (*endgrent)(struct nss_module *mod);
78         void (*fini)(struct nss_module *mod);
79
80         union {
81                 struct {
82                         void *l_ptr;
83                         int  (*l_getpwuid)(uid_t, struct passwd *pwd,
84                                            char *buffer, size_t buflen,
85                                            int *errnop);
86                         int  (*l_getgrent)(struct group *result, char *buffer,
87                                            size_t buflen, int *errnop);
88                         int  (*l_endgrent)(void);
89                 } lib;
90                 struct {
91                         FILE *f_passwd;
92                         FILE *f_group;
93                 } files;
94         } u;
95 };
96
97 static struct nss_module g_nss_modules[NSS_MODULES_MAX_NR];
98
99 #define NSS_LIB_NAME_PATTERN "libnss_%s.so.2"
100
101 /*
102  * permission file format is like this:
103  * {nid} {uid} {perms}
104  *
105  * '*' nid means any nid
106  * '*' uid means any uid
107  * the valid values for perms are:
108  * setuid/setgid/setgrp         -- enable corresponding perm
109  * nosetuid/nosetgid/nosetgrp   -- disable corresponding perm
110  * they can be listed together, separated by ',',
111  * when perm and noperm are in the same line (item), noperm is preferential,
112  * when they are in different lines (items), the latter is preferential,
113  * '*' nid is as default perm, and is not preferential.
114  */
115
116 static char *progname;
117
118 static void usage(void)
119 {
120         fprintf(stderr,
121                 "\nusage: %s {-d|mdtname} {uid}\n"
122                 "Normally invoked as an upcall from Lustre, set via:\n"
123                 "lctl set_param mdt.${mdtname}.identity_upcall={path to upcall}\n"
124                 "\t-d: debug, print values to stdout instead of Lustre\n"
125                 "\tNSS support enabled\n",
126                 progname);
127 }
128
129 static void errlog(const char *fmt, ...)
130 {
131         va_list args;
132
133         openlog(progname, LOG_PERROR | LOG_PID, LOG_AUTHPRIV);
134
135         va_start(args, fmt);
136         vsyslog(LOG_WARNING, fmt, args);
137         va_end(args);
138
139         closelog();
140 }
141
142 static int compare_gids(const void *v1, const void *v2)
143 {
144         return (*(gid_t *)v1 - *(gid_t *)v2);
145 }
146
147 /** getpwuid() replacement */
148 static struct passwd *getpwuid_nss(uid_t uid)
149 {
150         static struct passwd pw;
151         int i;
152
153         for (i = 0; i < g_n_nss_modules; i++) {
154                 struct nss_module *mod = g_nss_modules + i;
155
156                 if (mod->getpwuid(mod, uid, &pw) == 0)
157                         return &pw;
158         }
159         return NULL;
160 }
161
162 /**
163  * getgrent() replacement.
164  *  simulate getgrent(3) across nss modules
165  */
166 static struct group *getgrent_nss(void)
167 {
168         static struct group grp;
169
170         if (grent_mod_no < 0)
171                 grent_mod_no = 0;
172
173         while (grent_mod_no < g_n_nss_modules) {
174                 struct nss_module *mod = g_nss_modules + grent_mod_no;
175
176                 if (mod->getgrent(mod, &grp) == 0)
177                         return &grp;
178                 mod->endgrent(mod);
179                 grent_mod_no++;
180         }
181         return NULL;
182 }
183
184 /** endgrent() replacement */
185 static void endgrent_nss(void)
186 {
187         if (grent_mod_no < g_n_nss_modules
188                 && grent_mod_no >= 0) {
189                 struct nss_module *mod = g_nss_modules+grent_mod_no;
190
191                 mod->endgrent(mod);
192         }
193         grent_mod_no = -1;
194 }
195
196 /** lookup symbol in dynamically loaded nss module */
197 static void *get_nss_sym(struct nss_module *mod, const char *op)
198 {
199         void *res;
200         int bytes;
201         char symbuf[NSS_SYMBOL_NAME_LEN_MAX];
202
203         bytes = snprintf(symbuf, NSS_SYMBOL_NAME_LEN_MAX - 1, "_nss_%s_%s",
204                         mod->name, op);
205         if (bytes >= NSS_SYMBOL_NAME_LEN_MAX - 1) {
206                 errlog("symbol name too long\n");
207                 return NULL;
208         }
209         res = dlsym(mod->u.lib.l_ptr, symbuf);
210         if (res == NULL)
211                 errlog("cannot find symbol %s in nss module \"%s\": %s\n",
212                         symbuf, mod->name, dlerror());
213         return res;
214 }
215
216 /** allocate bigger buffer */
217 static void enlarge_nss_buffer(void **buf, int *bufsize)
218 {
219         free(*buf);
220         *bufsize = *bufsize * 2;
221         *buf = malloc(*bufsize);
222         if (*buf == NULL) {
223                 errlog("no memory to allocate bigger buffer of %d bytes\n",
224                         *bufsize);
225                 exit(-1);
226         }
227 }
228
229 static int getpwuid_nss_lib(struct nss_module *nss, uid_t uid,
230                             struct passwd *pw)
231 {
232         int tmp_errno, err;
233
234         while (1) {
235                 err = nss->u.lib.l_getpwuid(uid, pw, nss_pw_buf,
236                                 nss_pw_buf_len, &tmp_errno);
237                 if (err == NSS_STATUS_TRYAGAIN) {
238                         if (tmp_errno == ERANGE) {
239                                 /* buffer too small */
240                                 enlarge_nss_buffer(&nss_pw_buf,
241                                                 &nss_pw_buf_len);
242                         }
243                         continue;
244                 }
245                 break;
246         }
247         if (err == NSS_STATUS_SUCCESS)
248                 return 0;
249         return -ENOENT;
250 }
251
252 static int getgrent_nss_lib(struct nss_module *nss, struct group *gr)
253 {
254         int tmp_errno, err;
255
256         while (1) {
257                 err = nss->u.lib.l_getgrent(gr, nss_grent_buf,
258                                 nss_grent_buf_len, &tmp_errno);
259                 if (err == NSS_STATUS_TRYAGAIN) {
260                         if (tmp_errno == ERANGE) {
261                                 /* buffer too small */
262                                 enlarge_nss_buffer(&nss_grent_buf,
263                                                 &nss_grent_buf_len);
264                         }
265                         continue;
266                 }
267                 break;
268         }
269         if (err == NSS_STATUS_SUCCESS)
270                 return 0;
271         return -ENOENT;
272 }
273
274 static void endgrent_nss_lib(struct nss_module *mod)
275 {
276         mod->u.lib.l_endgrent();
277 }
278
279 /** destroy a "shared lib" nss module */
280 static void fini_nss_lib_module(struct nss_module *mod)
281 {
282         if (mod->u.lib.l_ptr)
283                 dlclose(mod->u.lib.l_ptr);
284 }
285
286 /** load and initialize a "shared lib" nss module */
287 static int init_nss_lib_module(struct nss_module *mod, char *name)
288 {
289         char lib_file_name[sizeof(NSS_LIB_NAME_PATTERN) + sizeof(mod->name)];
290
291         if (strlen(name) >= sizeof(mod->name)) {
292                 errlog("module name (%s) too long\n", name);
293                 exit(1);
294         }
295
296         strncpy(mod->name, name, sizeof(mod->name));
297         mod->name[sizeof(mod->name) - 1] = '\0';
298
299         snprintf(lib_file_name, sizeof(lib_file_name), NSS_LIB_NAME_PATTERN,
300                  name);
301
302         mod->getpwuid = getpwuid_nss_lib;
303         mod->getgrent = getgrent_nss_lib;
304         mod->endgrent = endgrent_nss_lib;
305         mod->fini = fini_nss_lib_module;
306
307         mod->u.lib.l_ptr = dlopen(lib_file_name, RTLD_NOW);
308         if (mod->u.lib.l_ptr == NULL) {
309                 errlog("dl error %s\n", dlerror());
310                 exit(1);
311         }
312         mod->u.lib.l_getpwuid = get_nss_sym(mod, "getpwuid_r");
313         if (mod->getpwuid == NULL)
314                 exit(1);
315
316         mod->u.lib.l_getgrent = get_nss_sym(mod, "getgrent_r");
317         if (mod->getgrent == NULL)
318                 exit(1);
319
320         mod->u.lib.l_endgrent = get_nss_sym(mod, "endgrent");
321         if (mod->endgrent == NULL)
322                 exit(1);
323
324         return 0;
325 }
326
327 static void fini_lustre_nss_module(struct nss_module *mod)
328 {
329         if (mod->u.files.f_passwd)
330                 fclose(mod->u.files.f_passwd);
331         if (mod->u.files.f_group)
332                 fclose(mod->u.files.f_group);
333 }
334
335 static int getpwuid_lustre_nss(struct nss_module *mod, uid_t uid,
336                               struct passwd *pw)
337 {
338         struct passwd *pos;
339
340         while ((pos = fgetpwent(mod->u.files.f_passwd)) != NULL) {
341                 if (pos->pw_uid == uid) {
342                         *pw = *pos;
343                         return 0;
344                 }
345         }
346         return -1;
347 }
348
349 static int getgrent_lustre_nss(struct nss_module *mod, struct group *gr)
350 {
351         struct group *pos;
352
353         pos = fgetgrent(mod->u.files.f_group);
354         if (pos) {
355                 *gr = *pos;
356                 return 0;
357         }
358         return 1;
359 }
360
361 static void endgrent_lustre_nss(struct nss_module *mod)
362 {
363 }
364
365 /** initialize module to access local /etc/lustre/passwd,group files */
366 static int init_lustre_module(struct nss_module *mod)
367 {
368         mod->fini = fini_lustre_nss_module;
369         mod->getpwuid = getpwuid_lustre_nss;
370         mod->getgrent = getgrent_lustre_nss;
371         mod->endgrent = endgrent_lustre_nss;
372
373         mod->u.files.f_passwd = fopen(LUSTRE_PASSWD, "r");
374         if (mod->u.files.f_passwd == NULL)
375                 exit(1);
376
377         mod->u.files.f_group = fopen(LUSTRE_GROUP, "r");
378         if (mod->u.files.f_group == NULL)
379                 exit(1);
380
381         snprintf(mod->name, sizeof(mod->name), "lustre");
382         return 0;
383 }
384
385 /** load and initialize the "nss" system */
386 static void init_nss(void)
387 {
388         nss_pw_buf_len = sysconf(_SC_GETPW_R_SIZE_MAX);
389         if (nss_pw_buf_len == -1) {
390                 perror("sysconf");
391                 exit(1);
392         }
393         nss_pw_buf = malloc(nss_pw_buf_len);
394         if (nss_pw_buf == NULL) {
395                 perror("pw buffer allocation");
396                 exit(1);
397         }
398
399         nss_grent_buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
400         if (nss_grent_buf_len == -1) {
401                 perror("sysconf");
402                 exit(1);
403         }
404         nss_grent_buf = malloc(nss_grent_buf_len);
405         if (nss_grent_buf == NULL) {
406                 perror("grent buffer allocation");
407                 exit(1);
408         }
409 }
410
411 /** unload "nss" */
412 static void fini_nss(void)
413 {
414         int i;
415
416         for (i = 0; i < g_n_nss_modules; i++) {
417                 struct nss_module *mod = g_nss_modules + i;
418
419                 mod->fini(mod);
420         }
421
422         free(nss_pw_buf);
423         free(nss_grent_buf);
424 }
425
426 /** get supplementary group info and fill downcall data */
427 static int get_groups_nss(struct identity_downcall_data *data,
428                           unsigned int maxgroups)
429 {
430         struct passwd *pw;
431         struct group *gr;
432         gid_t *groups;
433         unsigned int ngroups = 0;
434         char *pw_name;
435         int i;
436
437         pw = getpwuid_nss(data->idd_uid);
438         if (pw == NULL) {
439                 data->idd_err = errno ? errno : EIDRM;
440                 errlog("no such user %u\n", data->idd_uid);
441                 return -1;
442         }
443
444         data->idd_gid = pw->pw_gid;
445         pw_name = strdup(pw->pw_name);
446         groups = data->idd_groups;
447
448         while ((gr = getgrent_nss()) != NULL && ngroups < maxgroups) {
449                 if (gr->gr_gid == pw->pw_gid)
450                         continue;
451                 if (!gr->gr_mem)
452                         continue;
453                 for (i = 0; gr->gr_mem[i]; i++) {
454                         if (!strcmp(gr->gr_mem[i], pw_name)) {
455                                 groups[ngroups++] = gr->gr_gid;
456                                 break;
457                         }
458                 }
459         }
460
461         endgrent_nss();
462
463         if (ngroups > 0)
464                 qsort(groups, ngroups, sizeof(*groups), compare_gids);
465         data->idd_ngroups = ngroups;
466
467         free(pw_name);
468         return 0;
469 }
470
471 int get_groups_local(struct identity_downcall_data *data,
472                      unsigned int maxgroups)
473 {
474         gid_t *groups, *groups_tmp = NULL;
475         unsigned int ngroups = 0;
476         int ngroups_tmp;
477         struct passwd *pw;
478         int i;
479
480         pw = getpwuid(data->idd_uid);
481         if (!pw) {
482                 errlog("no such user %u\n", data->idd_uid);
483                 data->idd_err = errno ? errno : EIDRM;
484                 return -1;
485         }
486
487         data->idd_gid = pw->pw_gid;
488
489         groups = data->idd_groups;
490
491         /*
492          * Allocate array of size maxgroups instead of handling two
493          * consecutive and potentially racy getgrouplist() calls.
494          */
495         groups_tmp = malloc(maxgroups * sizeof(gid_t));
496         if (!groups_tmp) {
497                 data->idd_err = errno ? errno : ENOMEM;
498                 errlog("malloc error=%u\n", data->idd_err);
499                 return -1;
500         }
501
502         ngroups_tmp = maxgroups;
503         if (getgrouplist(pw->pw_name, pw->pw_gid, groups_tmp, &ngroups_tmp) <
504             0) {
505                 free(groups_tmp);
506                 data->idd_err = errno ? errno : EIDRM;
507                 errlog("getgrouplist() error for uid %u: error=%u\n",
508                        data->idd_uid, data->idd_err);
509                 return -1;
510         }
511
512         /* Do not place user's group ID in to the resulting groups list */
513         for (i = 0; i < ngroups_tmp; i++)
514                 if (pw->pw_gid != groups_tmp[i])
515                         groups[ngroups++] = groups_tmp[i];
516
517         if (ngroups > 0)
518                 qsort(groups, ngroups, sizeof(*groups), compare_gids);
519         data->idd_ngroups = ngroups;
520
521         free(groups_tmp);
522         return 0;
523 }
524
525 int get_groups_common(struct identity_downcall_data *data,
526                       unsigned int maxgroups)
527 {
528         if (g_n_nss_modules)
529                 return get_groups_nss(data, maxgroups);
530         return get_groups_local(data, maxgroups);
531 }
532
533 static inline int comment_line(char *line)
534 {
535         char *p = line;
536
537         while (*p && (*p == ' ' || *p == '\t'))
538                 p++;
539
540         if (!*p || *p == '\n' || *p == '#')
541                 return 1;
542         return 0;
543 }
544
545 static inline int match_uid(uid_t uid, const char *str)
546 {
547         char *end;
548         uid_t uid2;
549
550         if (!strcmp(str, "*"))
551                 return -1;
552
553         uid2 = strtoul(str, &end, 0);
554         if (*end)
555                 return 0;
556         return (uid == uid2);
557 }
558
559 struct perm_type {
560         char *name;
561         __u32 bit;
562 };
563
564 static struct perm_type perm_types[] = {
565         { "setuid", CFS_SETUID_PERM },
566         { "setgid", CFS_SETGID_PERM },
567         { "setgrp", CFS_SETGRP_PERM },
568         { "rmtacl", 0 },
569         { "rmtown", 0 },
570         { 0 }
571 };
572
573 static struct perm_type noperm_types[] = {
574         { "nosetuid", CFS_SETUID_PERM },
575         { "nosetgid", CFS_SETGID_PERM },
576         { "nosetgrp", CFS_SETGRP_PERM },
577         { "normtacl", 0 },
578         { "normtown", 0 },
579         { 0 }
580 };
581
582 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
583 {
584         char *start, *end;
585         char name[64];
586         struct perm_type *pt;
587
588         *perm = 0;
589         *noperm = 0;
590         start = str;
591         while (1) {
592                 size_t len;
593
594                 memset(name, 0, sizeof(name));
595                 end = strchr(start, ',');
596                 if (!end)
597                         end = str + strlen(str);
598                 if (start >= end)
599                         break;
600                 len = end - start;
601                 if (len >= sizeof(name))
602                         return -E2BIG;
603                 strncpy(name, start, len);
604                 name[len] = '\0';
605                 for (pt = perm_types; pt->name; pt++) {
606                         if (!strcasecmp(name, pt->name)) {
607                                 *perm |= pt->bit;
608                                 break;
609                         }
610                 }
611
612                 if (!pt->name) {
613                         for (pt = noperm_types; pt->name; pt++) {
614                                 if (!strcasecmp(name, pt->name)) {
615                                         *noperm |= pt->bit;
616                                         break;
617                                 }
618                         }
619
620                         if (!pt->name) {
621                                 printf("unkown type: %s\n", name);
622                                 return -1;
623                         }
624                 }
625
626                 start = end + 1;
627         }
628         return 0;
629 }
630
631 static int
632 parse_perm_line(struct identity_downcall_data *data, char *line, size_t size)
633 {
634         char uid_str[size];
635         char nid_str[size];
636         char perm_str[size];
637         lnet_nid_t nid;
638         __u32 perm, noperm;
639         int rc, i;
640
641         if (data->idd_nperms >= N_PERMS_MAX) {
642                 errlog("permission count %d > max %d\n",
643                        data->idd_nperms, N_PERMS_MAX);
644                 return -1;
645         }
646
647         rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
648         if (rc != 3) {
649                 errlog("can't parse line %s\n", line);
650                 return -1;
651         }
652
653         if (!match_uid(data->idd_uid, uid_str))
654                 return 0;
655
656         if (!strcmp(nid_str, "*")) {
657                 nid = LNET_NID_ANY;
658         } else {
659                 nid = libcfs_str2nid(nid_str);
660                 if (nid == LNET_NID_ANY) {
661                         errlog("can't parse nid %s\n", nid_str);
662                         return -1;
663                 }
664         }
665
666         if (parse_perm(&perm, &noperm, perm_str)) {
667                 errlog("invalid perm %s\n", perm_str);
668                 return -1;
669         }
670
671         /*
672          * merge the perms with the same nid.
673          *
674          * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
675          * it must be data->idd_perms[0].pdd_nid, and act as default perm.
676          */
677         if (nid != LNET_NID_ANY) {
678                 int found = 0;
679
680                 /* search for the same nid */
681                 for (i = data->idd_nperms - 1; i >= 0; i--) {
682                         if (data->idd_perms[i].pdd_nid == nid) {
683                                 data->idd_perms[i].pdd_perm =
684                                         (data->idd_perms[i].pdd_perm | perm) &
685                                         ~noperm;
686                                 found = 1;
687                                 break;
688                         }
689                 }
690
691                 /* NOT found, add to tail */
692                 if (!found) {
693                         data->idd_perms[data->idd_nperms].pdd_nid = nid;
694                         data->idd_perms[data->idd_nperms].pdd_perm =
695                                 perm & ~noperm;
696                         data->idd_nperms++;
697                 }
698         } else {
699                 if (data->idd_nperms > 0) {
700                         /* the first one isn't LNET_NID_ANY, need exchange */
701                         if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
702                                 data->idd_perms[data->idd_nperms].pdd_nid =
703                                         data->idd_perms[0].pdd_nid;
704                                 data->idd_perms[data->idd_nperms].pdd_perm =
705                                         data->idd_perms[0].pdd_perm;
706                                 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
707                                 data->idd_perms[0].pdd_perm = perm & ~noperm;
708                                 data->idd_nperms++;
709                         } else {
710                                 /* only fix LNET_NID_ANY item */
711                                 data->idd_perms[0].pdd_perm =
712                                         (data->idd_perms[0].pdd_perm | perm) &
713                                         ~noperm;
714                         }
715                 } else {
716                         /* it is the first one, only add to head */
717                         data->idd_perms[0].pdd_nid = LNET_NID_ANY;
718                         data->idd_perms[0].pdd_perm = perm & ~noperm;
719                         data->idd_nperms = 1;
720                 }
721         }
722
723         return 0;
724 }
725
726 static char *striml(char *s)
727 {
728         while (isspace(*s))
729                 s++;
730         return s;
731 }
732
733 static void check_new_nss_module(struct nss_module *mod)
734 {
735         int i;
736
737         for (i = 0; i < g_n_nss_modules; i++) {
738                 struct nss_module *pos = g_nss_modules + i;
739
740                 if (!strcmp(mod->name, pos->name)) {
741                         errlog("attempt to initialize \"%s\" module twice\n",
742                                 pos->name);
743                         exit(-1);
744                 }
745         }
746 }
747
748 /**
749  * Check and parse lookup db config line.
750  * File should start with 'lookup' followed by the modules
751  * to be loaded, for example:
752  *
753  *  [/etc/lustre/perm.conf]
754  *  lookup lustre ldap
755  *
756  * Should search, in order, first found wins:
757  *    lustre [/etc/lustre/passwd and /etc/lustre/group]
758  *    ldap
759  *
760  * Other common nss modules: nis sss db files
761  * Since historically 'files' has been used exclusively
762  *  to mean 'lustre auth files' and disabled using local auth
763  *  via libnss_files users must select 'nss_files' to explicitly
764  *  enable libnss_files, which is an uncommon configuration.
765  */
766 static int lookup_db_line_nss(char *line)
767 {
768         char *p, *tok;
769         int ret = 0;
770
771         p = striml(line);
772         if (strncmp(p, L_GETIDENTITY_LOOKUP_CMD,
773             sizeof(L_GETIDENTITY_LOOKUP_CMD) - 1))
774                 return -EAGAIN;
775
776         tok = strtok(p, " \t");
777         if (tok == NULL || strcmp(tok, L_GETIDENTITY_LOOKUP_CMD))
778                 return -EIO;
779
780         while ((tok = strtok(NULL, " \t\n")) != NULL) {
781                 struct nss_module *newmod = NULL;
782
783                 if (g_n_nss_modules < NSS_MODULES_MAX_NR)
784                         newmod = &g_nss_modules[g_n_nss_modules];
785                 else
786                         return -ERANGE;
787
788                 if (!strcmp(tok, "files") || !strcmp(tok, "lustre"))
789                         ret = init_lustre_module(newmod);
790                 else if (!strcmp(tok, "nss_files"))
791                         ret = init_nss_lib_module(newmod, "files");
792                 else
793                         ret = init_nss_lib_module(newmod, tok);
794
795                 if (ret)
796                         break;
797                 check_new_nss_module(newmod);
798                 g_n_nss_modules++;
799         }
800
801         return ret;
802 }
803
804 int get_perms(struct identity_downcall_data *data)
805 {
806         FILE *fp;
807         char line[PATH_MAX];
808         int ret;
809
810         fp = fopen(PERM_PATHNAME, "r");
811         if (!fp) {
812                 if (errno == ENOENT)
813                         return 0;
814                 errlog("open %s failed: %s\n",
815                        PERM_PATHNAME, strerror(errno));
816                 data->idd_err = errno;
817                 return -1;
818         }
819
820         while (fgets(line, sizeof(line), fp)) {
821                 if (comment_line(line))
822                         continue;
823                 ret = lookup_db_line_nss(line); /* lookup parsed */
824                 if (ret == 0)
825                         continue;
826                 if (parse_perm_line(data, line, sizeof(line))) {
827                         errlog("parse line %s failed!\n", line);
828                         data->idd_err = EINVAL;
829                         fclose(fp);
830                         return -1;
831                 }
832         }
833
834         fclose(fp);
835         return 0;
836 }
837
838 static void show_result(struct identity_downcall_data *data)
839 {
840         int i;
841
842         if (data->idd_err) {
843                 errlog("failed to get identity for uid %d: %s\n",
844                        data->idd_uid, strerror(data->idd_err));
845                 return;
846         }
847
848         printf("uid=%d gid=%d", data->idd_uid, data->idd_gid);
849         for (i = 0; i < data->idd_ngroups; i++)
850                 printf(",%u", data->idd_groups[i]);
851         printf("\n");
852         printf("permissions:\n"
853                "  nid\t\t\tperm\n");
854         for (i = 0; i < data->idd_nperms; i++) {
855                 struct perm_downcall_data *pdd;
856
857                 pdd = &data->idd_perms[i];
858
859                 printf("  %#jx\t0x%x\n", (uintmax_t)pdd->pdd_nid,
860                        pdd->pdd_perm);
861         }
862         printf("\n");
863 }
864
865 #define difftime(a, b)                                  \
866         ((a).tv_sec - (b).tv_sec +                      \
867          ((a).tv_usec - (b).tv_usec) / 1000000.0)
868
869 int main(int argc, char **argv)
870 {
871         char *end;
872         struct identity_downcall_data *data = NULL;
873         glob_t path;
874         unsigned long uid;
875         struct timeval start, idgot, fini;
876         int fd, rc = -EINVAL, size, maxgroups;
877         bool alreadyfailed = false;
878
879         progname = basename(argv[0]);
880         if (argc != 3) {
881                 usage();
882                 goto out_no_nss;
883         }
884
885         errno = 0;
886         uid = strtoul(argv[2], &end, 0);
887         if (*end != '\0' || end == argv[2] || errno != 0) {
888                 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
889                 goto out_no_nss;
890         }
891         gettimeofday(&start, NULL);
892
893         maxgroups = sysconf(_SC_NGROUPS_MAX);
894         if (maxgroups > NGROUPS_MAX)
895                 maxgroups = NGROUPS_MAX;
896         if (maxgroups == -1) {
897                 rc = -EINVAL;
898                 goto out_no_nss;
899         }
900
901 retry:
902         size = offsetof(struct identity_downcall_data, idd_groups[maxgroups]);
903         data = malloc(size);
904         if (!data) {
905                 errlog("malloc identity downcall data(%d) failed!\n", size);
906                 if (!alreadyfailed) {
907                         alreadyfailed = true;
908                         goto retry;
909                 }
910                 rc = -ENOMEM;
911                 goto out_no_nss;
912         }
913
914         memset(data, 0, size);
915         data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
916         data->idd_uid = uid;
917
918         init_nss();
919
920         /* read permission database and/or load nss modules
921          * rc is -1 only when file exists and is not readable or
922          * content has format / syntax errors
923          */
924         rc = get_perms(data);
925         if (rc)
926                 goto downcall;
927
928         /* get groups for uid */
929         rc = get_groups_common(data, maxgroups);
930         if (rc)
931                 goto downcall;
932
933         size = offsetof(struct identity_downcall_data,
934                         idd_groups[data->idd_ngroups]);
935
936         gettimeofday(&idgot, NULL);
937 downcall:
938         if (strcmp(argv[1], "-d") == 0 || getenv("L_GETIDENTITY_TEST")) {
939                 show_result(data);
940                 rc = 0;
941                 goto out;
942         }
943
944         rc = cfs_get_param_paths(&path, "mdt/%s/identity_info", argv[1]);
945         if (rc != 0) {
946                 rc = -errno;
947                 goto out;
948         }
949
950         fd = open(path.gl_pathv[0], O_WRONLY);
951         if (fd < 0) {
952                 errlog("can't open file '%s':%s\n", path.gl_pathv[0],
953                        strerror(errno));
954                 rc = -errno;
955                 goto out_params;
956         }
957
958         rc = write(fd, data, size);
959         gettimeofday(&fini, NULL);
960         close(fd);
961         if (rc != size) {
962                 errlog("partial write ret %d: %s\n", rc, strerror(errno));
963                 if (!alreadyfailed) {
964                         alreadyfailed = true;
965                         cfs_free_param_data(&path);
966                         if (data)
967                                 free(data);
968                         goto retry;
969                 }
970                 rc = -1;
971         } else {
972                 rc = 0;
973         }
974         /* log if it takes more than 20 second to avoid rate limite */
975         if (rc || difftime(fini, start) > 20)
976                 errlog("get identity for uid %lu start time %ld.%06ld got time %ld.%06ld end time %ld.%06ld: rc = %d\n",
977                        uid, start.tv_sec, start.tv_usec, idgot.tv_sec,
978                        idgot.tv_usec, fini.tv_sec, fini.tv_usec, rc);
979
980 out_params:
981         cfs_free_param_data(&path);
982 out:
983         fini_nss();
984 out_no_nss:
985         if (data)
986                 free(data);
987         return rc;
988 }