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