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