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