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