Whamcloud - gitweb
b=23428 Fix lustre built with --enable-lu_ref
[fs/lustre-release.git] / lustre / utils / l_getidentity.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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 <liblustre.h>
52 #include <lustre/lustre_user.h>
53 #include <lustre/lustre_idl.h>
54
55 #define PERM_PATHNAME "/etc/lustre/perm.conf"
56
57 /*
58  * permission file format is like this:
59  * {nid} {uid} {perms}
60  *
61  * '*' nid means any nid
62  * '*' uid means any uid
63  * the valid values for perms are:
64  * setuid/setgid/setgrp/rmtacl           -- enable corresponding perm
65  * nosetuid/nosetgid/nosetgrp/normtacl   -- disable corresponding perm
66  * they can be listed together, seperated by ',',
67  * when perm and noperm are in the same line (item), noperm is preferential,
68  * when they are in different lines (items), the latter is preferential,
69  * '*' nid is as default perm, and is not preferential.
70  */
71
72 static char *progname;
73
74 static void usage(void)
75 {
76         fprintf(stderr,
77                 "\nusage: %s {mdtname} {uid}\n"
78                 "Normally invoked as an upcall from Lustre, set via:\n"
79                 "  /proc/fs/lustre/mdt/{mdtname}/identity_upcall\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_AUTHPRIV);
93
94         va_start(args, fmt);
95         vsyslog(LOG_NOTICE, fmt, args);
96         fprintf(stderr, fmt, args);
97         va_end(args);
98
99         closelog();
100 }
101
102 int get_groups_local(struct identity_downcall_data *data)
103 {
104         int maxgroups;
105         gid_t *groups;
106         unsigned int ngroups = 0;
107         struct passwd *pw;
108         struct group *gr;
109         char *pw_name;
110         int namelen;
111         int i;
112
113         pw = getpwuid(data->idd_uid);
114         if (!pw) {
115                 errlog("no such user %u\n", data->idd_uid);
116                 data->idd_err = errno ? errno : EIDRM;
117                 return -1;
118         }
119         data->idd_gid = pw->pw_gid;
120
121         namelen = sysconf(_SC_LOGIN_NAME_MAX);
122         if (namelen < _POSIX_LOGIN_NAME_MAX)
123                 namelen = _POSIX_LOGIN_NAME_MAX;
124         pw_name = (char *)malloc(namelen);
125         if (!pw_name) {
126                 errlog("malloc error\n");
127                 data->idd_err = errno;
128                 return -1;
129         }
130         memset(pw_name, 0, namelen);
131         strncpy(pw_name, pw->pw_name, namelen - 1);
132
133         maxgroups = sysconf(_SC_NGROUPS_MAX);
134         if (maxgroups > NGROUPS_MAX)
135                 maxgroups = NGROUPS_MAX;
136         groups = data->idd_groups;
137
138         groups[ngroups++] = pw->pw_gid;
139         while ((gr = getgrent())) {
140                 if (gr->gr_gid == groups[0])
141                         continue;
142                 if (!gr->gr_mem)
143                         continue;
144                 for (i = 0; gr->gr_mem[i]; i++) {
145                         if (!strcmp(gr->gr_mem[i], pw_name)) {
146                                 groups[ngroups++] = gr->gr_gid;
147                                 break;
148                         }
149                 }
150                 if (ngroups == maxgroups)
151                         break;
152         }
153         endgrent();
154         qsort(groups, ngroups, sizeof(*groups), compare_u32);
155         data->idd_ngroups = ngroups;
156
157         free(pw_name);
158         return 0;
159 }
160
161 static inline int comment_line(char *line)
162 {
163         char *p = line;
164
165         while (*p && (*p == ' ' || *p == '\t')) p++;
166
167         if (!*p || *p == '\n' || *p == '#')
168                 return 1;
169         return 0;
170 }
171
172 static inline int match_uid(uid_t uid, const char *str)
173 {
174         char *end;
175         uid_t uid2;
176
177         if(!strcmp(str, "*"))
178                 return -1;
179
180         uid2 = strtoul(str, &end, 0);
181         if (*end)
182                 return 0;
183
184         return (uid == uid2);
185 }
186
187 typedef struct {
188         char   *name;
189         __u32   bit;
190 } perm_type_t;
191
192 static perm_type_t perm_types[] = {
193         { "setuid", CFS_SETUID_PERM },
194         { "setgid", CFS_SETGID_PERM },
195         { "setgrp", CFS_SETGRP_PERM },
196         { "rmtacl", CFS_RMTACL_PERM },
197         { "rmtown", CFS_RMTOWN_PERM },
198         { 0 }
199 };
200
201 static perm_type_t noperm_types[] = {
202         { "nosetuid", CFS_SETUID_PERM },
203         { "nosetgid", CFS_SETGID_PERM },
204         { "nosetgrp", CFS_SETGRP_PERM },
205         { "normtacl", CFS_RMTACL_PERM },
206         { "normtown", CFS_RMTOWN_PERM },
207         { 0 }
208 };
209
210 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
211 {
212         char *start, *end;
213         char name[64];
214         perm_type_t *pt;
215
216         *perm = 0;
217         *noperm = 0;
218         start = str;
219         while (1) {
220                 memset(name, 0, sizeof(name));
221                 end = strchr(start, ',');
222                 if (!end)
223                         end = str + strlen(str);
224                 if (start >= end)
225                         break;
226                 strncpy(name, start, end - start);
227                 for (pt = perm_types; pt->name; pt++) {
228                         if (!strcasecmp(name, pt->name)) {
229                                 *perm |= pt->bit;
230                                 break;
231                         }
232                 }
233
234                 if (!pt->name) {
235                         for (pt = noperm_types; pt->name; pt++) {
236                                 if (!strcasecmp(name, pt->name)) {
237                                         *noperm |= pt->bit;
238                                         break;
239                                 }
240                         }
241
242                         if (!pt->name) {
243                                 printf("unkown type: %s\n", name);
244                                 return -1;
245                         }
246                 }
247
248                 start = end + 1;
249         }
250         return 0;
251 }
252
253 int parse_perm_line(struct identity_downcall_data *data, char *line)
254 {
255         char uid_str[256], nid_str[256], perm_str[256];
256         lnet_nid_t nid;
257         __u32 perm, noperm;
258         int rc, i;
259
260         if (data->idd_nperms >= N_PERMS_MAX) {
261                 errlog("permission count %d > max %d\n",
262                         data->idd_nperms, N_PERMS_MAX);
263                 return -1;
264         }
265
266         rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
267         if (rc != 3) {
268                 errlog("can't parse line %s\n", line);
269                 return -1;
270         }
271
272         if (!match_uid(data->idd_uid, uid_str))
273                 return 0;
274
275         if (!strcmp(nid_str, "*")) {
276                 nid = LNET_NID_ANY;
277         } else {
278                 nid = libcfs_str2nid(nid_str);
279                 if (nid == LNET_NID_ANY) {
280                         errlog("can't parse nid %s\n", nid_str);
281                         return -1;
282                 }
283         }
284
285         if (parse_perm(&perm, &noperm, perm_str)) {
286                 errlog("invalid perm %s\n", perm_str);
287                 return -1;
288         }
289
290         /* merge the perms with the same nid.
291          *
292          * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
293          * it must be data->idd_perms[0].pdd_nid, and act as default perm.
294          */
295         if (nid != LNET_NID_ANY) {
296                 int found = 0;
297
298                 /* search for the same nid */
299                 for (i = data->idd_nperms - 1; i >= 0; i--) {
300                         if (data->idd_perms[i].pdd_nid == nid) {
301                                 data->idd_perms[i].pdd_perm =
302                                         (data->idd_perms[i].pdd_perm | perm) &
303                                         ~noperm;
304                                 found = 1;
305                                 break;
306                         }
307                 }
308
309                 /* NOT found, add to tail */
310                 if (!found) {
311                         data->idd_perms[data->idd_nperms].pdd_nid = nid;
312                         data->idd_perms[data->idd_nperms].pdd_perm =
313                                 perm & ~noperm;
314                         data->idd_nperms++;
315                 }
316         } else {
317                 if (data->idd_nperms > 0) {
318                         /* the first one isn't LNET_NID_ANY, need exchange */
319                         if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
320                                 data->idd_perms[data->idd_nperms].pdd_nid =
321                                         data->idd_perms[0].pdd_nid;
322                                 data->idd_perms[data->idd_nperms].pdd_perm =
323                                         data->idd_perms[0].pdd_perm;
324                                 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
325                                 data->idd_perms[0].pdd_perm = perm & ~noperm;
326                                 data->idd_nperms++;
327                         } else {
328                                 /* only fix LNET_NID_ANY item */
329                                 data->idd_perms[0].pdd_perm =
330                                         (data->idd_perms[0].pdd_perm | perm) &
331                                         ~noperm;
332                         }
333                 } else {
334                         /* it is the first one, only add to head */
335                         data->idd_perms[0].pdd_nid = LNET_NID_ANY;
336                         data->idd_perms[0].pdd_perm = perm & ~noperm;
337                         data->idd_nperms = 1;
338                 }
339         }
340
341         return 0;
342 }
343
344 int get_perms(FILE *fp, struct identity_downcall_data *data)
345 {
346         char line[1024];
347
348         while (fgets(line, 1024, fp)) {
349                 if (comment_line(line))
350                         continue;
351
352                 if (parse_perm_line(data, line)) {
353                         errlog("parse line %s failed!\n", line);
354                         return -1;
355                 }
356         }
357
358         return 0;
359 }
360
361 static void show_result(struct identity_downcall_data *data)
362 {
363         int i;
364
365         if (data->idd_err) {
366                 errlog("failed to get identity for uid %d: %s\n",
367                        data->idd_uid, strerror(data->idd_err));
368                 return;
369         }
370
371         printf("uid=%d gid=", data->idd_uid);
372         for (i = 0; i < data->idd_ngroups; i++)
373                 printf("%s%u", i > 0 ? "," : "", data->idd_groups[i]);
374         printf("\n");
375         printf("permissions:\n"
376                "  nid\t\t\tperm\n");
377         for (i = 0; i < data->idd_nperms; i++) {
378                 struct perm_downcall_data *pdd;
379
380                 pdd = &data->idd_perms[i];
381
382                 printf("  "LPX64"\t0x%x\n", pdd->pdd_nid, pdd->pdd_perm);
383         }
384         printf("\n");
385 }
386
387 int main(int argc, char **argv)
388 {
389         FILE *perms_fp;
390         char *end;
391         struct identity_downcall_data *data;
392         char procname[1024];
393         unsigned long uid;
394         int fd, rc;
395
396         progname = basename(argv[0]);
397
398         if (argc != 3) {
399                 usage();
400                 return 1;
401         }
402
403         uid = strtoul(argv[2], &end, 0);
404         if (*end) {
405                 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
406                 usage();
407                 return 1;
408         }
409
410         data = malloc(sizeof(*data));
411         if (!data) {
412                 errlog("malloc identity downcall data(%d) failed!\n",
413                        sizeof(*data));
414                 return 1;
415         }
416         memset(data, 0, sizeof(*data));
417         data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
418         data->idd_uid = uid;
419
420         /* get groups for uid */
421         rc = get_groups_local(data);
422         if (rc)
423                 goto downcall;
424
425         /* read permission database */
426         perms_fp = fopen(PERM_PATHNAME, "r");
427         if (perms_fp) {
428                 get_perms(perms_fp, data);
429                 fclose(perms_fp);
430         } else if (errno != ENOENT) {
431                 errlog("open %s failed: %s\n",
432                        PERM_PATHNAME, strerror(errno));
433         }
434
435 downcall:
436         if (getenv("L_GETIDENTITY_TEST")) {
437                 show_result(data);
438                 return 0;
439         }
440
441         snprintf(procname, sizeof(procname),
442                  "/proc/fs/lustre/mdt/%s/identity_info", argv[1]);
443         fd = open(procname, O_WRONLY);
444         if (fd < 0) {
445                 errlog("can't open file %s: %s\n", procname, strerror(errno));
446                 return 1;
447         }
448
449         rc = write(fd, data, sizeof(*data));
450         close(fd);
451         if (rc != sizeof(*data)) {
452                 errlog("partial write ret %d: %s\n", rc, strerror(errno));
453                 return 1;
454         }
455
456         return 0;
457 }