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