Whamcloud - gitweb
LU-14199 sec: find policy version in use for sepol
[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         /*
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 int main(int argc, char **argv)
406 {
407         char *end;
408         struct identity_downcall_data *data = NULL;
409         glob_t path;
410         unsigned long uid;
411         int fd, rc = -EINVAL, size, maxgroups;
412
413         progname = basename(argv[0]);
414         if (argc != 3) {
415                 usage();
416                 goto out;
417         }
418
419         uid = strtoul(argv[2], &end, 0);
420         if (*end) {
421                 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
422                 goto out;
423         }
424
425         maxgroups = sysconf(_SC_NGROUPS_MAX);
426         if (maxgroups > NGROUPS_MAX)
427                 maxgroups = NGROUPS_MAX;
428         if (maxgroups == -1) {
429                 rc = -EINVAL;
430                 goto out;
431         }
432
433         size = offsetof(struct identity_downcall_data, idd_groups[maxgroups]);
434         data = malloc(size);
435         if (!data) {
436                 errlog("malloc identity downcall data(%d) failed!\n", size);
437                 rc = -ENOMEM;
438                 goto out;
439         }
440
441         memset(data, 0, size);
442         data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
443         data->idd_uid = uid;
444         /* get groups for uid */
445         rc = get_groups_local(data, maxgroups);
446         if (rc)
447                 goto downcall;
448
449         size = offsetof(struct identity_downcall_data,
450                         idd_groups[data->idd_ngroups]);
451         /* read permission database */
452         rc = get_perms(data);
453
454 downcall:
455         if (strcmp(argv[1], "-d") == 0 || getenv("L_GETIDENTITY_TEST")) {
456                 show_result(data);
457                 rc = 0;
458                 goto out;
459         }
460
461         rc = cfs_get_param_paths(&path, "mdt/%s/identity_info", argv[1]);
462         if (rc != 0) {
463                 rc = -errno;
464                 goto out;
465         }
466
467         fd = open(path.gl_pathv[0], O_WRONLY);
468         if (fd < 0) {
469                 errlog("can't open file '%s':%s\n", path.gl_pathv[0],
470                        strerror(errno));
471                 rc = -errno;
472                 goto out_params;
473         }
474
475         rc = write(fd, data, size);
476         close(fd);
477         if (rc != size) {
478                 errlog("partial write ret %d: %s\n", rc, strerror(errno));
479                 rc = -1;
480         } else {
481                 rc = 0;
482         }
483
484 out_params:
485         cfs_free_param_data(&path);
486 out:
487         if (data)
488                 free(data);
489         return rc;
490 }