Whamcloud - gitweb
New release 2.12.7
[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  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #include <stdbool.h>
34 #include <stdlib.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <fcntl.h>
41 #include <pwd.h>
42 #include <grp.h>
43 #include <stdarg.h>
44 #include <stddef.h>
45 #include <libgen.h>
46 #include <syslog.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         /* Allocate array of size maxgroups instead of handling two
121          * consecutive and potentially racy getgrouplist() calls. */
122         groups_tmp = malloc(maxgroups * sizeof(gid_t));
123         if (groups_tmp == NULL) {
124                 data->idd_err = errno ? errno : ENOMEM;
125                 errlog("malloc error=%u\n",data->idd_err);
126                 return -1;
127         }
128
129         ngroups_tmp = maxgroups;
130         if (getgrouplist(pw->pw_name, pw->pw_gid, groups_tmp, &ngroups_tmp) <
131             0) {
132                 free(groups_tmp);
133                 data->idd_err = errno ? errno : EIDRM;
134                 errlog("getgrouplist() error for uid %u: error=%u\n",
135                         data->idd_uid, data->idd_err);
136                 return -1;
137         }
138
139         /* Do not place user's group ID in to the resulting groups list */
140         for (i = 0; i < ngroups_tmp; i++)
141                 if (pw->pw_gid != groups_tmp[i])
142                         groups[ngroups++] = groups_tmp[i];
143
144         if (ngroups > 0)
145                 qsort(groups, ngroups, sizeof(*groups), compare_u32);
146         data->idd_ngroups = ngroups;
147
148         free(groups_tmp);
149         return 0;
150 }
151
152 static inline int comment_line(char *line)
153 {
154         char *p = line;
155
156         while (*p && (*p == ' ' || *p == '\t')) p++;
157
158         if (!*p || *p == '\n' || *p == '#')
159                 return 1;
160         return 0;
161 }
162
163 static inline int match_uid(uid_t uid, const char *str)
164 {
165         char *end;
166         uid_t uid2;
167
168         if(!strcmp(str, "*"))
169                 return -1;
170
171         uid2 = strtoul(str, &end, 0);
172         if (*end)
173                 return 0;
174         return (uid == uid2);
175 }
176
177 typedef struct {
178         char   *name;
179         __u32   bit;
180 } perm_type_t;
181
182 static perm_type_t perm_types[] = {
183         { "setuid", CFS_SETUID_PERM },
184         { "setgid", CFS_SETGID_PERM },
185         { "setgrp", CFS_SETGRP_PERM },
186         { "rmtacl", 0 },
187         { "rmtown", 0 },
188         { 0 }
189 };
190
191 static perm_type_t noperm_types[] = {
192         { "nosetuid", CFS_SETUID_PERM },
193         { "nosetgid", CFS_SETGID_PERM },
194         { "nosetgrp", CFS_SETGRP_PERM },
195         { "normtacl", 0 },
196         { "normtown", 0 },
197         { 0 }
198 };
199
200 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
201 {
202         char *start, *end;
203         char name[64];
204         perm_type_t *pt;
205
206         *perm = 0;
207         *noperm = 0;
208         start = str;
209         while (1) {
210                 size_t len;
211                 memset(name, 0, sizeof(name));
212                 end = strchr(start, ',');
213                 if (end == NULL)
214                         end = str + strlen(str);
215                 if (start >= end)
216                         break;
217                 len = end - start;
218                 if (len >= sizeof(name))
219                         return -E2BIG;
220                 strncpy(name, start, len);
221                 name[len] = '\0';
222                 for (pt = perm_types; pt->name; pt++) {
223                         if (!strcasecmp(name, pt->name)) {
224                                 *perm |= pt->bit;
225                                 break;
226                         }
227                 }
228
229                 if (!pt->name) {
230                         for (pt = noperm_types; pt->name; pt++) {
231                                 if (!strcasecmp(name, pt->name)) {
232                                         *noperm |= pt->bit;
233                                         break;
234                                 }
235                         }
236
237                         if (!pt->name) {
238                                 printf("unkown type: %s\n", name);
239                                 return -1;
240                         }
241                 }
242
243                 start = end + 1;
244         }
245         return 0;
246 }
247
248 static int
249 parse_perm_line(struct identity_downcall_data *data, char *line, size_t size)
250 {
251         char uid_str[size];
252         char nid_str[size];
253         char perm_str[size];
254         lnet_nid_t nid;
255         __u32 perm, noperm;
256         int rc, i;
257
258         if (data->idd_nperms >= N_PERMS_MAX) {
259                 errlog("permission count %d > max %d\n",
260                         data->idd_nperms, N_PERMS_MAX);
261                 return -1;
262         }
263
264         rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
265         if (rc != 3) {
266                 errlog("can't parse line %s\n", line);
267                 return -1;
268         }
269
270         if (!match_uid(data->idd_uid, uid_str))
271                 return 0;
272
273         if (!strcmp(nid_str, "*")) {
274                 nid = LNET_NID_ANY;
275         } else {
276                 nid = libcfs_str2nid(nid_str);
277                 if (nid == LNET_NID_ANY) {
278                         errlog("can't parse nid %s\n", nid_str);
279                         return -1;
280                 }
281         }
282
283         if (parse_perm(&perm, &noperm, perm_str)) {
284                 errlog("invalid perm %s\n", perm_str);
285                 return -1;
286         }
287
288         /* merge the perms with the same nid.
289          *
290          * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
291          * it must be data->idd_perms[0].pdd_nid, and act as default perm.
292          */
293         if (nid != LNET_NID_ANY) {
294                 int found = 0;
295
296                 /* search for the same nid */
297                 for (i = data->idd_nperms - 1; i >= 0; i--) {
298                         if (data->idd_perms[i].pdd_nid == nid) {
299                                 data->idd_perms[i].pdd_perm =
300                                         (data->idd_perms[i].pdd_perm | perm) &
301                                         ~noperm;
302                                 found = 1;
303                                 break;
304                         }
305                 }
306
307                 /* NOT found, add to tail */
308                 if (!found) {
309                         data->idd_perms[data->idd_nperms].pdd_nid = nid;
310                         data->idd_perms[data->idd_nperms].pdd_perm =
311                                 perm & ~noperm;
312                         data->idd_nperms++;
313                 }
314         } else {
315                 if (data->idd_nperms > 0) {
316                         /* the first one isn't LNET_NID_ANY, need exchange */
317                         if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
318                                 data->idd_perms[data->idd_nperms].pdd_nid =
319                                         data->idd_perms[0].pdd_nid;
320                                 data->idd_perms[data->idd_nperms].pdd_perm =
321                                         data->idd_perms[0].pdd_perm;
322                                 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
323                                 data->idd_perms[0].pdd_perm = perm & ~noperm;
324                                 data->idd_nperms++;
325                         } else {
326                                 /* only fix LNET_NID_ANY item */
327                                 data->idd_perms[0].pdd_perm =
328                                         (data->idd_perms[0].pdd_perm | perm) &
329                                         ~noperm;
330                         }
331                 } else {
332                         /* it is the first one, only add to head */
333                         data->idd_perms[0].pdd_nid = LNET_NID_ANY;
334                         data->idd_perms[0].pdd_perm = perm & ~noperm;
335                         data->idd_nperms = 1;
336                 }
337         }
338
339         return 0;
340 }
341
342 int get_perms(struct identity_downcall_data *data)
343 {
344         FILE *fp;
345         char line[PATH_MAX];
346
347         fp = fopen(PERM_PATHNAME, "r");
348         if (fp == NULL) {
349                 if (errno == ENOENT) {
350                         return 0;
351                 } else {
352                         errlog("open %s failed: %s\n",
353                                PERM_PATHNAME, strerror(errno));
354                         data->idd_err = errno;
355                         return -1;
356                 }
357         }
358
359         while (fgets(line, sizeof(line), fp)) {
360                 if (comment_line(line))
361                         continue;
362
363                 if (parse_perm_line(data, line, sizeof(line))) {
364                         errlog("parse line %s failed!\n", line);
365                         data->idd_err = EINVAL;
366                         fclose(fp);
367                         return -1;
368                 }
369         }
370
371         fclose(fp);
372         return 0;
373 }
374
375 static void show_result(struct identity_downcall_data *data)
376 {
377         int i;
378
379         if (data->idd_err) {
380                 errlog("failed to get identity for uid %d: %s\n",
381                        data->idd_uid, strerror(data->idd_err));
382                 return;
383         }
384
385         printf("uid=%d gid=%d", data->idd_uid, data->idd_gid);
386         for (i = 0; i < data->idd_ngroups; i++)
387                 printf(",%u", data->idd_groups[i]);
388         printf("\n");
389         printf("permissions:\n"
390                "  nid\t\t\tperm\n");
391         for (i = 0; i < data->idd_nperms; i++) {
392                 struct perm_downcall_data *pdd;
393
394                 pdd = &data->idd_perms[i];
395
396                 printf("  %#jx\t0x%x\n", (uintmax_t)pdd->pdd_nid,
397                        pdd->pdd_perm);
398         }
399         printf("\n");
400 }
401
402 int main(int argc, char **argv)
403 {
404         char *end;
405         struct identity_downcall_data *data = NULL;
406         glob_t path;
407         unsigned long uid;
408         int fd, rc = -EINVAL, size, maxgroups;
409
410         progname = basename(argv[0]);
411         if (argc != 3) {
412                 usage();
413                 goto out;
414         }
415
416         uid = strtoul(argv[2], &end, 0);
417         if (*end) {
418                 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
419                 goto out;
420         }
421
422         maxgroups = sysconf(_SC_NGROUPS_MAX);
423         if (maxgroups > NGROUPS_MAX)
424                 maxgroups = NGROUPS_MAX;
425         if (maxgroups == -1) {
426                 rc = -EINVAL;
427                 goto out;
428         }
429
430         size = offsetof(struct identity_downcall_data, idd_groups[maxgroups]);
431         data = malloc(size);
432         if (!data) {
433                 errlog("malloc identity downcall data(%d) failed!\n", size);
434                 rc = -ENOMEM;
435                 goto out;
436         }
437
438         memset(data, 0, size);
439         data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
440         data->idd_uid = uid;
441         /* get groups for uid */
442         rc = get_groups_local(data, maxgroups);
443         if (rc)
444                 goto downcall;
445
446         size = offsetof(struct identity_downcall_data,
447                         idd_groups[data->idd_ngroups]);
448         /* read permission database */
449         rc = get_perms(data);
450
451 downcall:
452         if (strcmp(argv[1], "-d") == 0 || getenv("L_GETIDENTITY_TEST")) {
453                 show_result(data);
454                 rc = 0;
455                 goto out;
456         }
457
458         rc = cfs_get_param_paths(&path, "mdt/%s/identity_info", argv[1]);
459         if (rc != 0) {
460                 rc = -errno;
461                 goto out;
462         }
463
464         fd = open(path.gl_pathv[0], O_WRONLY);
465         if (fd < 0) {
466                 errlog("can't open file '%s':%s\n", path.gl_pathv[0],
467                        strerror(errno));
468                 rc = -errno;
469                 goto out_params;
470         }
471
472         rc = write(fd, data, size);
473         close(fd);
474         if (rc != size) {
475                 errlog("partial write ret %d: %s\n", rc, strerror(errno));
476                 rc = -1;
477         } else {
478                 rc = 0;
479         }
480
481 out_params:
482         cfs_free_param_data(&path);
483 out:
484         if (data != NULL)
485                 free(data);
486         return rc;
487 }