Whamcloud - gitweb
No bugzilla for this. One of the test failing was config-sanity.sh
[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_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 {
103         int maxgroups;
104         gid_t *groups;
105         unsigned int ngroups = 0;
106         struct passwd *pw;
107         struct group *gr;
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         data->idd_gid = pw->pw_gid;
119
120         namelen = sysconf(_SC_LOGIN_NAME_MAX);
121         if (namelen < _POSIX_LOGIN_NAME_MAX)
122                 namelen = _POSIX_LOGIN_NAME_MAX;
123         pw_name = (char *)malloc(namelen);
124         if (!pw_name) {
125                 errlog("malloc error\n");
126                 data->idd_err = errno;
127                 return -1;
128         }
129         memset(pw_name, 0, namelen);
130         strncpy(pw_name, pw->pw_name, namelen - 1);
131
132         maxgroups = sysconf(_SC_NGROUPS_MAX);
133         if (maxgroups > NGROUPS_MAX)
134                 maxgroups = NGROUPS_MAX;
135         groups = data->idd_groups;
136
137         groups[ngroups++] = pw->pw_gid;
138         while ((gr = getgrent())) {
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                 if (ngroups == maxgroups)
150                         break;
151         }
152         endgrent();
153         qsort(groups, ngroups, sizeof(*groups), compare_u32);
154         data->idd_ngroups = ngroups;
155
156         free(pw_name);
157         return 0;
158 }
159
160 static inline int comment_line(char *line)
161 {
162         char *p = line;
163
164         while (*p && (*p == ' ' || *p == '\t')) p++;
165
166         if (!*p || *p == '\n' || *p == '#')
167                 return 1;
168         return 0;
169 }
170
171 static inline int match_uid(uid_t uid, const char *str)
172 {
173         char *end;
174         uid_t uid2;
175
176         if(!strcmp(str, "*"))
177                 return -1;
178
179         uid2 = strtoul(str, &end, 0);
180         if (*end)
181                 return 0;
182
183         return (uid == uid2);
184 }
185
186 typedef struct {
187         char   *name;
188         __u32   bit;
189 } perm_type_t;
190
191 static perm_type_t perm_types[] = {
192         { "setuid", CFS_SETUID_PERM },
193         { "setgid", CFS_SETGID_PERM },
194         { "setgrp", CFS_SETGRP_PERM },
195         { "rmtacl", CFS_RMTACL_PERM },
196         { "rmtown", CFS_RMTOWN_PERM },
197         { 0 }
198 };
199
200 static perm_type_t noperm_types[] = {
201         { "nosetuid", CFS_SETUID_PERM },
202         { "nosetgid", CFS_SETGID_PERM },
203         { "nosetgrp", CFS_SETGRP_PERM },
204         { "normtacl", CFS_RMTACL_PERM },
205         { "normtown", CFS_RMTOWN_PERM },
206         { 0 }
207 };
208
209 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
210 {
211         char *start, *end;
212         char name[64];
213         perm_type_t *pt;
214
215         *perm = 0;
216         *noperm = 0;
217         start = str;
218         while (1) {
219                 memset(name, 0, sizeof(name));
220                 end = strchr(start, ',');
221                 if (!end)
222                         end = str + strlen(str);
223                 if (start >= end)
224                         break;
225                 strncpy(name, start, end - start);
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 int parse_perm_line(struct identity_downcall_data *data, char *line)
253 {
254         char uid_str[256], nid_str[256], perm_str[256];
255         lnet_nid_t nid;
256         __u32 perm, noperm;
257         int rc, i;
258
259         if (data->idd_nperms >= N_PERMS_MAX) {
260                 errlog("permission count %d > max %d\n",
261                         data->idd_nperms, N_PERMS_MAX);
262                 return -1;
263         }
264
265         rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
266         if (rc != 3) {
267                 errlog("can't parse line %s\n", line);
268                 return -1;
269         }
270
271         if (!match_uid(data->idd_uid, uid_str))
272                 return 0;
273
274         if (!strcmp(nid_str, "*")) {
275                 nid = LNET_NID_ANY;
276         } else {
277                 nid = libcfs_str2nid(nid_str);
278                 if (nid == LNET_NID_ANY) {
279                         errlog("can't parse nid %s\n", nid_str);
280                         return -1;
281                 }
282         }
283
284         if (parse_perm(&perm, &noperm, perm_str)) {
285                 errlog("invalid perm %s\n", perm_str);
286                 return -1;
287         }
288
289         /* merge the perms with the same nid.
290          *
291          * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
292          * it must be data->idd_perms[0].pdd_nid, and act as default perm.
293          */
294         if (nid != LNET_NID_ANY) {
295                 int found = 0;
296
297                 /* search for the same nid */
298                 for (i = data->idd_nperms - 1; i >= 0; i--) {
299                         if (data->idd_perms[i].pdd_nid == nid) {
300                                 data->idd_perms[i].pdd_perm =
301                                         (data->idd_perms[i].pdd_perm | perm) &
302                                         ~noperm;
303                                 found = 1;
304                                 break;
305                         }
306                 }
307
308                 /* NOT found, add to tail */
309                 if (!found) {
310                         data->idd_perms[data->idd_nperms].pdd_nid = nid;
311                         data->idd_perms[data->idd_nperms].pdd_perm =
312                                 perm & ~noperm;
313                         data->idd_nperms++;
314                 }
315         } else {
316                 if (data->idd_nperms > 0) {
317                         /* the first one isn't LNET_NID_ANY, need exchange */
318                         if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
319                                 data->idd_perms[data->idd_nperms].pdd_nid =
320                                         data->idd_perms[0].pdd_nid;
321                                 data->idd_perms[data->idd_nperms].pdd_perm =
322                                         data->idd_perms[0].pdd_perm;
323                                 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
324                                 data->idd_perms[0].pdd_perm = perm & ~noperm;
325                                 data->idd_nperms++;
326                         } else {
327                                 /* only fix LNET_NID_ANY item */
328                                 data->idd_perms[0].pdd_perm =
329                                         (data->idd_perms[0].pdd_perm | perm) &
330                                         ~noperm;
331                         }
332                 } else {
333                         /* it is the first one, only add to head */
334                         data->idd_perms[0].pdd_nid = LNET_NID_ANY;
335                         data->idd_perms[0].pdd_perm = perm & ~noperm;
336                         data->idd_nperms = 1;
337                 }
338         }
339
340         return 0;
341 }
342
343 int get_perms(FILE *fp, struct identity_downcall_data *data)
344 {
345         char line[1024];
346
347         while (fgets(line, 1024, fp)) {
348                 if (comment_line(line))
349                         continue;
350
351                 if (parse_perm_line(data, line)) {
352                         errlog("parse line %s failed!\n", line);
353                         return -1;
354                 }
355         }
356
357         return 0;
358 }
359
360 static void show_result(struct identity_downcall_data *data)
361 {
362         int i;
363
364         if (data->idd_err) {
365                 errlog("failed to get identity for uid %d: %s\n",
366                        data->idd_uid, strerror(data->idd_err));
367                 return;
368         }
369
370         printf("uid=%d gid=", data->idd_uid);
371         for (i = 0; i < data->idd_ngroups; i++)
372                 printf("%s%u", i > 0 ? "," : "", data->idd_groups[i]);
373         printf("\n");
374         printf("permissions:\n"
375                "  nid\t\t\tperm\n");
376         for (i = 0; i < data->idd_nperms; i++) {
377                 struct perm_downcall_data *pdd;
378
379                 pdd = &data->idd_perms[i];
380
381                 printf("  "LPX64"\t0x%x\n", pdd->pdd_nid, pdd->pdd_perm);
382         }
383         printf("\n");
384 }
385
386 int main(int argc, char **argv)
387 {
388         FILE *perms_fp;
389         char *end;
390         struct identity_downcall_data *data;
391         char procname[1024];
392         unsigned long uid;
393         int fd, rc;
394
395         progname = basename(argv[0]);
396
397         if (argc != 3) {
398                 usage();
399                 return 1;
400         }
401
402         uid = strtoul(argv[2], &end, 0);
403         if (*end) {
404                 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
405                 usage();
406                 return 1;
407         }
408
409         data = malloc(sizeof(*data));
410         if (!data) {
411                 errlog("malloc identity downcall data(%d) failed!\n",
412                        sizeof(*data));
413                 return 1;
414         }
415         memset(data, 0, sizeof(*data));
416         data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
417         data->idd_uid = uid;
418
419         /* get groups for uid */
420         rc = get_groups_local(data);
421         if (rc)
422                 goto downcall;
423
424         /* read permission database */
425         perms_fp = fopen(PERM_PATHNAME, "r");
426         if (perms_fp) {
427                 get_perms(perms_fp, data);
428                 fclose(perms_fp);
429         } else if (errno != ENOENT) {
430                 errlog("open %s failed: %s\n",
431                        PERM_PATHNAME, strerror(errno));
432         }
433
434 downcall:
435         if (getenv("L_GETIDENTITY_TEST")) {
436                 show_result(data);
437                 return 0;
438         }
439
440         snprintf(procname, sizeof(procname),
441                  "/proc/fs/lustre/mdt/%s/identity_info", argv[1]);
442         fd = open(procname, O_WRONLY);
443         if (fd < 0) {
444                 errlog("can't open file %s: %s\n", procname, strerror(errno));
445                 return 1;
446         }
447
448         rc = write(fd, data, sizeof(*data));
449         close(fd);
450         if (rc != sizeof(*data)) {
451                 errlog("partial write ret %d: %s\n", rc, strerror(errno));
452                 return 1;
453         }
454
455         return 0;
456 }