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