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