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