Whamcloud - gitweb
LU-5149 utils: Create debug upcall to dump ldlm namespaces
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <unistd.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <fcntl.h>
44 #include <pwd.h>
45 #include <grp.h>
46 #include <stdarg.h>
47 #include <stddef.h>
48 #include <libgen.h>
49 #include <syslog.h>
50
51 #include <libcfs/util/string.h>
52 #include <lnet/nidstr.h>
53 #include <lustre/lustre_user.h>
54 #include <lustre/lustre_idl.h>
55
56 #define PERM_PATHNAME "/etc/lustre/perm.conf"
57
58 /*
59  * permission file format is like this:
60  * {nid} {uid} {perms}
61  *
62  * '*' nid means any nid
63  * '*' uid means any uid
64  * the valid values for perms are:
65  * setuid/setgid/setgrp/rmtacl           -- enable corresponding perm
66  * nosetuid/nosetgid/nosetgrp/normtacl   -- disable corresponding perm
67  * they can be listed together, separated by ',',
68  * when perm and noperm are in the same line (item), noperm is preferential,
69  * when they are in different lines (items), the latter is preferential,
70  * '*' nid is as default perm, and is not preferential.
71  */
72
73 static char *progname;
74
75 static void usage(void)
76 {
77         fprintf(stderr,
78                 "\nusage: %s {mdtname} {uid}\n"
79                 "Normally invoked as an upcall from Lustre, set via:\n"
80                 "/proc/fs/lustre/mdt/${mdtname}/identity_upcall\n",
81                 progname);
82 }
83
84 static int compare_u32(const void *v1, const void *v2)
85 {
86         return (*(__u32 *)v1 - *(__u32 *)v2);
87 }
88
89 static void errlog(const char *fmt, ...)
90 {
91         va_list args;
92
93         openlog(progname, LOG_PERROR | LOG_PID, LOG_AUTHPRIV);
94
95         va_start(args, fmt);
96         vsyslog(LOG_NOTICE, fmt, args);
97         va_end(args);
98
99         closelog();
100 }
101
102 int get_groups_local(struct identity_downcall_data *data,
103                      unsigned int maxgroups)
104 {
105         gid_t *groups, *groups_tmp = NULL;
106         unsigned int ngroups = 0;
107         int ngroups_tmp;
108         struct passwd *pw;
109         char *pw_name;
110         int namelen;
111         int i;
112
113         pw = getpwuid(data->idd_uid);
114         if (!pw) {
115                 errlog("no such user %u\n", data->idd_uid);
116                 data->idd_err = errno ? errno : EIDRM;
117                 return -1;
118         }
119
120         data->idd_gid = pw->pw_gid;
121         namelen = sysconf(_SC_LOGIN_NAME_MAX);
122         if (namelen < _POSIX_LOGIN_NAME_MAX)
123                 namelen = _POSIX_LOGIN_NAME_MAX;
124
125         pw_name = malloc(namelen);
126         if (!pw_name) {
127                 errlog("malloc error\n");
128                 data->idd_err = errno;
129                 return -1;
130         }
131
132         strlcpy(pw_name, pw->pw_name, namelen);
133         groups = data->idd_groups;
134
135         /* Allocate array of size maxgroups instead of handling two
136          * consecutive and potentially racy getgrouplist() calls. */
137         groups_tmp = malloc(maxgroups * sizeof(gid_t));
138         if (groups_tmp == NULL) {
139                 free(pw_name);
140                 data->idd_err = errno ? errno : ENOMEM;
141                 errlog("malloc error=%u\n",data->idd_err);
142                 return -1;
143         }
144
145         ngroups_tmp = maxgroups;
146         if (getgrouplist(pw->pw_name, pw->pw_gid, groups_tmp, &ngroups_tmp) <
147             0) {
148                 free(pw_name);
149                 free(groups_tmp);
150                 data->idd_err = errno ? errno : EIDRM;
151                 errlog("getgrouplist() error for uid %u: error=%u\n",
152                         data->idd_uid, data->idd_err);
153                 return -1;
154         }
155
156         /* Do not place user's group ID in to the resulting groups list */
157         for (i = 0; i < ngroups_tmp; i++)
158                 if (pw->pw_gid != groups_tmp[i])
159                         groups[ngroups++] = groups_tmp[i];
160
161         if (ngroups > 0)
162                 qsort(groups, ngroups, sizeof(*groups), compare_u32);
163         data->idd_ngroups = ngroups;
164
165         free(pw_name);
166         free(groups_tmp);
167         return 0;
168 }
169
170 static inline int comment_line(char *line)
171 {
172         char *p = line;
173
174         while (*p && (*p == ' ' || *p == '\t')) p++;
175
176         if (!*p || *p == '\n' || *p == '#')
177                 return 1;
178         return 0;
179 }
180
181 static inline int match_uid(uid_t uid, const char *str)
182 {
183         char *end;
184         uid_t uid2;
185
186         if(!strcmp(str, "*"))
187                 return -1;
188
189         uid2 = strtoul(str, &end, 0);
190         if (*end)
191                 return 0;
192         return (uid == uid2);
193 }
194
195 typedef struct {
196         char   *name;
197         __u32   bit;
198 } perm_type_t;
199
200 static perm_type_t perm_types[] = {
201         { "setuid", CFS_SETUID_PERM },
202         { "setgid", CFS_SETGID_PERM },
203         { "setgrp", CFS_SETGRP_PERM },
204         { "rmtacl", CFS_RMTACL_PERM },
205         { "rmtown", CFS_RMTOWN_PERM },
206         { 0 }
207 };
208
209 static perm_type_t noperm_types[] = {
210         { "nosetuid", CFS_SETUID_PERM },
211         { "nosetgid", CFS_SETGID_PERM },
212         { "nosetgrp", CFS_SETGRP_PERM },
213         { "normtacl", CFS_RMTACL_PERM },
214         { "normtown", CFS_RMTOWN_PERM },
215         { 0 }
216 };
217
218 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
219 {
220         char *start, *end;
221         char name[64];
222         perm_type_t *pt;
223
224         *perm = 0;
225         *noperm = 0;
226         start = str;
227         while (1) {
228                 size_t len;
229                 memset(name, 0, sizeof(name));
230                 end = strchr(start, ',');
231                 if (end == NULL)
232                         end = str + strlen(str);
233                 if (start >= end)
234                         break;
235                 len = end - start;
236                 if (len >= sizeof(name))
237                         return -E2BIG;
238                 strncpy(name, start, len);
239                 name[len] = '\0';
240                 for (pt = perm_types; pt->name; pt++) {
241                         if (!strcasecmp(name, pt->name)) {
242                                 *perm |= pt->bit;
243                                 break;
244                         }
245                 }
246
247                 if (!pt->name) {
248                         for (pt = noperm_types; pt->name; pt++) {
249                                 if (!strcasecmp(name, pt->name)) {
250                                         *noperm |= pt->bit;
251                                         break;
252                                 }
253                         }
254
255                         if (!pt->name) {
256                                 printf("unkown type: %s\n", name);
257                                 return -1;
258                         }
259                 }
260
261                 start = end + 1;
262         }
263         return 0;
264 }
265
266 static int
267 parse_perm_line(struct identity_downcall_data *data, char *line, size_t size)
268 {
269         char uid_str[size];
270         char nid_str[size];
271         char perm_str[size];
272         lnet_nid_t nid;
273         __u32 perm, noperm;
274         int rc, i;
275
276         if (data->idd_nperms >= N_PERMS_MAX) {
277                 errlog("permission count %d > max %d\n",
278                         data->idd_nperms, N_PERMS_MAX);
279                 return -1;
280         }
281
282         rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
283         if (rc != 3) {
284                 errlog("can't parse line %s\n", line);
285                 return -1;
286         }
287
288         if (!match_uid(data->idd_uid, uid_str))
289                 return 0;
290
291         if (!strcmp(nid_str, "*")) {
292                 nid = LNET_NID_ANY;
293         } else {
294                 nid = libcfs_str2nid(nid_str);
295                 if (nid == LNET_NID_ANY) {
296                         errlog("can't parse nid %s\n", nid_str);
297                         return -1;
298                 }
299         }
300
301         if (parse_perm(&perm, &noperm, perm_str)) {
302                 errlog("invalid perm %s\n", perm_str);
303                 return -1;
304         }
305
306         /* merge the perms with the same nid.
307          *
308          * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
309          * it must be data->idd_perms[0].pdd_nid, and act as default perm.
310          */
311         if (nid != LNET_NID_ANY) {
312                 int found = 0;
313
314                 /* search for the same nid */
315                 for (i = data->idd_nperms - 1; i >= 0; i--) {
316                         if (data->idd_perms[i].pdd_nid == nid) {
317                                 data->idd_perms[i].pdd_perm =
318                                         (data->idd_perms[i].pdd_perm | perm) &
319                                         ~noperm;
320                                 found = 1;
321                                 break;
322                         }
323                 }
324
325                 /* NOT found, add to tail */
326                 if (!found) {
327                         data->idd_perms[data->idd_nperms].pdd_nid = nid;
328                         data->idd_perms[data->idd_nperms].pdd_perm =
329                                 perm & ~noperm;
330                         data->idd_nperms++;
331                 }
332         } else {
333                 if (data->idd_nperms > 0) {
334                         /* the first one isn't LNET_NID_ANY, need exchange */
335                         if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
336                                 data->idd_perms[data->idd_nperms].pdd_nid =
337                                         data->idd_perms[0].pdd_nid;
338                                 data->idd_perms[data->idd_nperms].pdd_perm =
339                                         data->idd_perms[0].pdd_perm;
340                                 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
341                                 data->idd_perms[0].pdd_perm = perm & ~noperm;
342                                 data->idd_nperms++;
343                         } else {
344                                 /* only fix LNET_NID_ANY item */
345                                 data->idd_perms[0].pdd_perm =
346                                         (data->idd_perms[0].pdd_perm | perm) &
347                                         ~noperm;
348                         }
349                 } else {
350                         /* it is the first one, only add to head */
351                         data->idd_perms[0].pdd_nid = LNET_NID_ANY;
352                         data->idd_perms[0].pdd_perm = perm & ~noperm;
353                         data->idd_nperms = 1;
354                 }
355         }
356
357         return 0;
358 }
359
360 int get_perms(struct identity_downcall_data *data)
361 {
362         FILE *fp;
363         char line[PATH_MAX];
364
365         fp = fopen(PERM_PATHNAME, "r");
366         if (fp == NULL) {
367                 if (errno == ENOENT) {
368                         return 0;
369                 } else {
370                         errlog("open %s failed: %s\n",
371                                PERM_PATHNAME, strerror(errno));
372                         data->idd_err = errno;
373                         return -1;
374                 }
375         }
376
377         while (fgets(line, sizeof(line), fp)) {
378                 if (comment_line(line))
379                         continue;
380
381                 if (parse_perm_line(data, line, sizeof(line))) {
382                         errlog("parse line %s failed!\n", line);
383                         data->idd_err = EINVAL;
384                         fclose(fp);
385                         return -1;
386                 }
387         }
388
389         fclose(fp);
390         return 0;
391 }
392
393 static void show_result(struct identity_downcall_data *data)
394 {
395         int i;
396
397         if (data->idd_err) {
398                 errlog("failed to get identity for uid %d: %s\n",
399                        data->idd_uid, strerror(data->idd_err));
400                 return;
401         }
402
403         printf("uid=%d gid=%d", data->idd_uid, data->idd_gid);
404         for (i = 0; i < data->idd_ngroups; i++)
405                 printf(",%u", data->idd_groups[i]);
406         printf("\n");
407         printf("permissions:\n"
408                "  nid\t\t\tperm\n");
409         for (i = 0; i < data->idd_nperms; i++) {
410                 struct perm_downcall_data *pdd;
411
412                 pdd = &data->idd_perms[i];
413
414                 printf("  "LPX64"\t0x%x\n", pdd->pdd_nid, pdd->pdd_perm);
415         }
416         printf("\n");
417 }
418
419 int main(int argc, char **argv)
420 {
421         char *end;
422         struct identity_downcall_data *data = NULL;
423         char procname[1024];
424         unsigned long uid;
425         int fd, rc = -EINVAL, size, maxgroups;
426
427         progname = basename(argv[0]);
428         if (argc != 3) {
429                 usage();
430                 goto out;
431         }
432
433         uid = strtoul(argv[2], &end, 0);
434         if (*end) {
435                 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
436                 goto out;
437         }
438
439         maxgroups = sysconf(_SC_NGROUPS_MAX);
440         if (maxgroups > NGROUPS_MAX)
441                 maxgroups = NGROUPS_MAX;
442         if (maxgroups == -1) {
443                 rc = -EINVAL;
444                 goto out;
445         }
446
447         size = offsetof(struct identity_downcall_data, idd_groups[maxgroups]);
448         data = malloc(size);
449         if (!data) {
450                 errlog("malloc identity downcall data(%d) failed!\n", size);
451                 rc = -ENOMEM;
452                 goto out;
453         }
454
455         memset(data, 0, size);
456         data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
457         data->idd_uid = uid;
458         /* get groups for uid */
459         rc = get_groups_local(data, maxgroups);
460         if (rc)
461                 goto downcall;
462
463         size = offsetof(struct identity_downcall_data,
464                         idd_groups[data->idd_ngroups]);
465         /* read permission database */
466         rc = get_perms(data);
467
468 downcall:
469         if (getenv("L_GETIDENTITY_TEST")) {
470                 show_result(data);
471                 rc = 0;
472                 goto out;
473         }
474
475         snprintf(procname, sizeof(procname),
476                  "/proc/fs/lustre/mdt/%s/identity_info", argv[1]);
477         fd = open(procname, O_WRONLY);
478         if (fd < 0) {
479                 errlog("can't open file %s: %s\n", procname, strerror(errno));
480                 rc = -1;
481                 goto out;
482         }
483
484         rc = write(fd, data, size);
485         close(fd);
486         if (rc != size) {
487                 errlog("partial write ret %d: %s\n", rc, strerror(errno));
488                 rc = -1;
489         } else {
490                 rc = 0;
491         }
492
493 out:
494         if (data != NULL)
495                 free(data);
496         return rc;
497 }