Whamcloud - gitweb
LU-657 test: limit the write size in run_dd
[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, Intel Corporation.
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, *groups_tmp = NULL;
105         unsigned int ngroups = 0;
106         int ngroups_tmp;
107         struct passwd *pw;
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 = 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         /* Allocate array of size maxgroups instead of handling two
136          * consecutive and potentially racy getgrouplist() calls. */
137         groups_tmp = malloc(maxgroups * sizeof(gid_t));
138         if (groups_tmp == NULL) {
139                 free(pw_name);
140                 errlog("malloc error\n");
141                 return -1;
142         }
143
144         ngroups_tmp = maxgroups;
145         if (getgrouplist(pw->pw_name, pw->pw_gid, groups_tmp, &ngroups_tmp) <
146             0) {
147                 free(pw_name);
148                 free(groups_tmp);
149                 errlog("getgrouplist() error\n");
150                 return -1;
151         }
152
153         /* Do not place user's group ID in to the resulting groups list */
154         for (i = 0; i < ngroups_tmp; i++)
155                 if (pw->pw_gid != groups_tmp[i])
156                         groups[ngroups++] = groups_tmp[i];
157
158         if (ngroups > 0)
159                 qsort(groups, ngroups, sizeof(*groups), compare_u32);
160         data->idd_ngroups = ngroups;
161
162         free(pw_name);
163         free(groups_tmp);
164         return 0;
165 }
166
167 static inline int comment_line(char *line)
168 {
169         char *p = line;
170
171         while (*p && (*p == ' ' || *p == '\t')) p++;
172
173         if (!*p || *p == '\n' || *p == '#')
174                 return 1;
175         return 0;
176 }
177
178 static inline int match_uid(uid_t uid, const char *str)
179 {
180         char *end;
181         uid_t uid2;
182
183         if(!strcmp(str, "*"))
184                 return -1;
185
186         uid2 = strtoul(str, &end, 0);
187         if (*end)
188                 return 0;
189         return (uid == uid2);
190 }
191
192 typedef struct {
193         char   *name;
194         __u32   bit;
195 } perm_type_t;
196
197 static perm_type_t perm_types[] = {
198         { "setuid", CFS_SETUID_PERM },
199         { "setgid", CFS_SETGID_PERM },
200         { "setgrp", CFS_SETGRP_PERM },
201         { "rmtacl", CFS_RMTACL_PERM },
202         { "rmtown", CFS_RMTOWN_PERM },
203         { 0 }
204 };
205
206 static perm_type_t noperm_types[] = {
207         { "nosetuid", CFS_SETUID_PERM },
208         { "nosetgid", CFS_SETGID_PERM },
209         { "nosetgrp", CFS_SETGRP_PERM },
210         { "normtacl", CFS_RMTACL_PERM },
211         { "normtown", CFS_RMTOWN_PERM },
212         { 0 }
213 };
214
215 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
216 {
217         char *start, *end;
218         char name[64];
219         perm_type_t *pt;
220
221         *perm = 0;
222         *noperm = 0;
223         start = str;
224         while (1) {
225                 memset(name, 0, sizeof(name));
226                 end = strchr(start, ',');
227                 if (!end)
228                         end = str + strlen(str);
229                 if (start >= end)
230                         break;
231                 strncpy(name, start, end - start);
232                 for (pt = perm_types; pt->name; pt++) {
233                         if (!strcasecmp(name, pt->name)) {
234                                 *perm |= pt->bit;
235                                 break;
236                         }
237                 }
238
239                 if (!pt->name) {
240                         for (pt = noperm_types; pt->name; pt++) {
241                                 if (!strcasecmp(name, pt->name)) {
242                                         *noperm |= pt->bit;
243                                         break;
244                                 }
245                         }
246
247                         if (!pt->name) {
248                                 printf("unkown type: %s\n", name);
249                                 return -1;
250                         }
251                 }
252
253                 start = end + 1;
254         }
255         return 0;
256 }
257
258 int parse_perm_line(struct identity_downcall_data *data, char *line)
259 {
260         char uid_str[256], nid_str[256], perm_str[256];
261         lnet_nid_t nid;
262         __u32 perm, noperm;
263         int rc, i;
264
265         if (data->idd_nperms >= N_PERMS_MAX) {
266                 errlog("permission count %d > max %d\n",
267                         data->idd_nperms, N_PERMS_MAX);
268                 return -1;
269         }
270
271         rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
272         if (rc != 3) {
273                 errlog("can't parse line %s\n", line);
274                 return -1;
275         }
276
277         if (!match_uid(data->idd_uid, uid_str))
278                 return 0;
279
280         if (!strcmp(nid_str, "*")) {
281                 nid = LNET_NID_ANY;
282         } else {
283                 nid = libcfs_str2nid(nid_str);
284                 if (nid == LNET_NID_ANY) {
285                         errlog("can't parse nid %s\n", nid_str);
286                         return -1;
287                 }
288         }
289
290         if (parse_perm(&perm, &noperm, perm_str)) {
291                 errlog("invalid perm %s\n", perm_str);
292                 return -1;
293         }
294
295         /* merge the perms with the same nid.
296          *
297          * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
298          * it must be data->idd_perms[0].pdd_nid, and act as default perm.
299          */
300         if (nid != LNET_NID_ANY) {
301                 int found = 0;
302
303                 /* search for the same nid */
304                 for (i = data->idd_nperms - 1; i >= 0; i--) {
305                         if (data->idd_perms[i].pdd_nid == nid) {
306                                 data->idd_perms[i].pdd_perm =
307                                         (data->idd_perms[i].pdd_perm | perm) &
308                                         ~noperm;
309                                 found = 1;
310                                 break;
311                         }
312                 }
313
314                 /* NOT found, add to tail */
315                 if (!found) {
316                         data->idd_perms[data->idd_nperms].pdd_nid = nid;
317                         data->idd_perms[data->idd_nperms].pdd_perm =
318                                 perm & ~noperm;
319                         data->idd_nperms++;
320                 }
321         } else {
322                 if (data->idd_nperms > 0) {
323                         /* the first one isn't LNET_NID_ANY, need exchange */
324                         if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
325                                 data->idd_perms[data->idd_nperms].pdd_nid =
326                                         data->idd_perms[0].pdd_nid;
327                                 data->idd_perms[data->idd_nperms].pdd_perm =
328                                         data->idd_perms[0].pdd_perm;
329                                 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
330                                 data->idd_perms[0].pdd_perm = perm & ~noperm;
331                                 data->idd_nperms++;
332                         } else {
333                                 /* only fix LNET_NID_ANY item */
334                                 data->idd_perms[0].pdd_perm =
335                                         (data->idd_perms[0].pdd_perm | perm) &
336                                         ~noperm;
337                         }
338                 } else {
339                         /* it is the first one, only add to head */
340                         data->idd_perms[0].pdd_nid = LNET_NID_ANY;
341                         data->idd_perms[0].pdd_perm = perm & ~noperm;
342                         data->idd_nperms = 1;
343                 }
344         }
345
346         return 0;
347 }
348
349 int get_perms(struct identity_downcall_data *data)
350 {
351         FILE *fp;
352         char line[1024];
353
354         fp = fopen(PERM_PATHNAME, "r");
355         if (fp == NULL) {
356                 if (errno == ENOENT) {
357                         return 0;
358                 } else {
359                         errlog("open %s failed: %s\n",
360                                PERM_PATHNAME, strerror(errno));
361                         data->idd_err = errno;
362                         return -1;
363                 }
364         }
365
366         while (fgets(line, 1024, fp)) {
367                 if (comment_line(line))
368                         continue;
369
370                 if (parse_perm_line(data, line)) {
371                         errlog("parse line %s failed!\n", line);
372                         data->idd_err = EINVAL;
373                         fclose(fp);
374                         return -1;
375                 }
376         }
377
378         fclose(fp);
379         return 0;
380 }
381
382 static void show_result(struct identity_downcall_data *data)
383 {
384         int i;
385
386         if (data->idd_err) {
387                 errlog("failed to get identity for uid %d: %s\n",
388                        data->idd_uid, strerror(data->idd_err));
389                 return;
390         }
391
392         printf("uid=%d gid=%d", data->idd_uid, data->idd_gid);
393         for (i = 0; i < data->idd_ngroups; i++)
394                 printf(",%u", data->idd_groups[i]);
395         printf("\n");
396         printf("permissions:\n"
397                "  nid\t\t\tperm\n");
398         for (i = 0; i < data->idd_nperms; i++) {
399                 struct perm_downcall_data *pdd;
400
401                 pdd = &data->idd_perms[i];
402
403                 printf("  "LPX64"\t0x%x\n", pdd->pdd_nid, pdd->pdd_perm);
404         }
405         printf("\n");
406 }
407
408 int main(int argc, char **argv)
409 {
410         char *end;
411         struct identity_downcall_data *data = NULL;
412         char procname[1024];
413         unsigned long uid;
414         int fd, rc = -EINVAL, size, maxgroups;
415
416         progname = basename(argv[0]);
417         if (argc != 3) {
418                 usage();
419                 goto out;
420         }
421
422         uid = strtoul(argv[2], &end, 0);
423         if (*end) {
424                 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
425                 goto out;
426         }
427
428         maxgroups = sysconf(_SC_NGROUPS_MAX);
429         if (maxgroups > NGROUPS_MAX)
430                 maxgroups = NGROUPS_MAX;
431
432         size = offsetof(struct identity_downcall_data, idd_groups[maxgroups]);
433         data = malloc(size);
434         if (!data) {
435                 errlog("malloc identity downcall data(%d) failed!\n", size);
436                 rc = -ENOMEM;
437                 goto out;
438         }
439
440         memset(data, 0, size);
441         data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
442         data->idd_uid = uid;
443         /* get groups for uid */
444         rc = get_groups_local(data, maxgroups);
445         if (rc)
446                 goto downcall;
447
448         size = offsetof(struct identity_downcall_data,
449                         idd_groups[data->idd_ngroups]);
450         /* read permission database */
451         rc = get_perms(data);
452
453 downcall:
454         if (getenv("L_GETIDENTITY_TEST")) {
455                 show_result(data);
456                 rc = 0;
457                 goto out;
458         }
459
460         snprintf(procname, sizeof(procname),
461                  "/proc/fs/lustre/mdt/%s/identity_info", argv[1]);
462         fd = open(procname, O_WRONLY);
463         if (fd < 0) {
464                 errlog("can't open file %s: %s\n", procname, strerror(errno));
465                 rc = -1;
466                 goto out;
467         }
468
469         rc = write(fd, data, size);
470         close(fd);
471         if (rc != size) {
472                 errlog("partial write ret %d: %s\n", rc, strerror(errno));
473                 rc = -1;
474         } else {
475                 rc = 0;
476         }
477
478 out:
479         if (data != NULL)
480                 free(data);
481         return rc;
482 }