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