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