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