4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
48 #include <libcfs/util/param.h>
49 #include <linux/lnet/nidstr.h>
50 #include <linux/lustre/lustre_user.h>
51 #include <linux/lustre/lustre_idl.h>
53 #define PERM_PATHNAME "/etc/lustre/perm.conf"
56 * permission file format is like this:
59 * '*' nid means any nid
60 * '*' uid means any uid
61 * the valid values for perms are:
62 * setuid/setgid/setgrp -- enable corresponding perm
63 * nosetuid/nosetgid/nosetgrp -- disable corresponding perm
64 * they can be listed together, separated by ',',
65 * when perm and noperm are in the same line (item), noperm is preferential,
66 * when they are in different lines (items), the latter is preferential,
67 * '*' nid is as default perm, and is not preferential.
70 static char *progname;
72 static void usage(void)
75 "\nusage: %s {-d|mdtname} {uid}\n"
76 "Normally invoked as an upcall from Lustre, set via:\n"
77 "lctl set_param mdt.${mdtname}.identity_upcall={path to upcall}\n"
78 "\t-d: debug, print values to stdout instead of Lustre\n",
82 static int compare_u32(const void *v1, const void *v2)
84 return *(__u32 *)v1 - *(__u32 *)v2;
87 static void errlog(const char *fmt, ...)
91 openlog(progname, LOG_PERROR | LOG_PID, LOG_AUTHPRIV);
94 vsyslog(LOG_WARNING, fmt, args);
100 int get_groups_local(struct identity_downcall_data *data,
101 unsigned int maxgroups)
103 gid_t *groups, *groups_tmp = NULL;
104 unsigned int ngroups = 0;
109 pw = getpwuid(data->idd_uid);
111 errlog("no such user %u\n", data->idd_uid);
112 data->idd_err = errno ? errno : EIDRM;
116 data->idd_gid = pw->pw_gid;
118 groups = data->idd_groups;
121 * Allocate array of size maxgroups instead of handling two
122 * consecutive and potentially racy getgrouplist() calls.
124 groups_tmp = malloc(maxgroups * sizeof(gid_t));
126 data->idd_err = errno ? errno : ENOMEM;
127 errlog("malloc error=%u\n", data->idd_err);
131 ngroups_tmp = maxgroups;
132 if (getgrouplist(pw->pw_name, pw->pw_gid, groups_tmp, &ngroups_tmp) <
135 data->idd_err = errno ? errno : EIDRM;
136 errlog("getgrouplist() error for uid %u: error=%u\n",
137 data->idd_uid, data->idd_err);
141 /* Do not place user's group ID in to the resulting groups list */
142 for (i = 0; i < ngroups_tmp; i++)
143 if (pw->pw_gid != groups_tmp[i])
144 groups[ngroups++] = groups_tmp[i];
147 qsort(groups, ngroups, sizeof(*groups), compare_u32);
148 data->idd_ngroups = ngroups;
154 static inline int comment_line(char *line)
158 while (*p && (*p == ' ' || *p == '\t'))
161 if (!*p || *p == '\n' || *p == '#')
166 static inline int match_uid(uid_t uid, const char *str)
171 if (!strcmp(str, "*"))
174 uid2 = strtoul(str, &end, 0);
177 return (uid == uid2);
185 static perm_type_t perm_types[] = {
186 { "setuid", CFS_SETUID_PERM },
187 { "setgid", CFS_SETGID_PERM },
188 { "setgrp", CFS_SETGRP_PERM },
194 static perm_type_t noperm_types[] = {
195 { "nosetuid", CFS_SETUID_PERM },
196 { "nosetgid", CFS_SETGID_PERM },
197 { "nosetgrp", CFS_SETGRP_PERM },
203 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
215 memset(name, 0, sizeof(name));
216 end = strchr(start, ',');
218 end = str + strlen(str);
222 if (len >= sizeof(name))
224 strncpy(name, start, len);
226 for (pt = perm_types; pt->name; pt++) {
227 if (!strcasecmp(name, pt->name)) {
234 for (pt = noperm_types; pt->name; pt++) {
235 if (!strcasecmp(name, pt->name)) {
242 printf("unkown type: %s\n", name);
253 parse_perm_line(struct identity_downcall_data *data, char *line, size_t size)
262 if (data->idd_nperms >= N_PERMS_MAX) {
263 errlog("permission count %d > max %d\n",
264 data->idd_nperms, N_PERMS_MAX);
268 rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
270 errlog("can't parse line %s\n", line);
274 if (!match_uid(data->idd_uid, uid_str))
277 if (!strcmp(nid_str, "*")) {
280 nid = libcfs_str2nid(nid_str);
281 if (nid == LNET_NID_ANY) {
282 errlog("can't parse nid %s\n", nid_str);
287 if (parse_perm(&perm, &noperm, perm_str)) {
288 errlog("invalid perm %s\n", perm_str);
293 * merge the perms with the same nid.
295 * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
296 * it must be data->idd_perms[0].pdd_nid, and act as default perm.
298 if (nid != LNET_NID_ANY) {
301 /* search for the same nid */
302 for (i = data->idd_nperms - 1; i >= 0; i--) {
303 if (data->idd_perms[i].pdd_nid == nid) {
304 data->idd_perms[i].pdd_perm =
305 (data->idd_perms[i].pdd_perm | perm) &
312 /* NOT found, add to tail */
314 data->idd_perms[data->idd_nperms].pdd_nid = nid;
315 data->idd_perms[data->idd_nperms].pdd_perm =
320 if (data->idd_nperms > 0) {
321 /* the first one isn't LNET_NID_ANY, need exchange */
322 if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
323 data->idd_perms[data->idd_nperms].pdd_nid =
324 data->idd_perms[0].pdd_nid;
325 data->idd_perms[data->idd_nperms].pdd_perm =
326 data->idd_perms[0].pdd_perm;
327 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
328 data->idd_perms[0].pdd_perm = perm & ~noperm;
331 /* only fix LNET_NID_ANY item */
332 data->idd_perms[0].pdd_perm =
333 (data->idd_perms[0].pdd_perm | perm) &
337 /* it is the first one, only add to head */
338 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
339 data->idd_perms[0].pdd_perm = perm & ~noperm;
340 data->idd_nperms = 1;
347 int get_perms(struct identity_downcall_data *data)
352 fp = fopen(PERM_PATHNAME, "r");
356 errlog("open %s failed: %s\n",
357 PERM_PATHNAME, strerror(errno));
358 data->idd_err = errno;
362 while (fgets(line, sizeof(line), fp)) {
363 if (comment_line(line))
366 if (parse_perm_line(data, line, sizeof(line))) {
367 errlog("parse line %s failed!\n", line);
368 data->idd_err = EINVAL;
378 static void show_result(struct identity_downcall_data *data)
383 errlog("failed to get identity for uid %d: %s\n",
384 data->idd_uid, strerror(data->idd_err));
388 printf("uid=%d gid=%d", data->idd_uid, data->idd_gid);
389 for (i = 0; i < data->idd_ngroups; i++)
390 printf(",%u", data->idd_groups[i]);
392 printf("permissions:\n"
394 for (i = 0; i < data->idd_nperms; i++) {
395 struct perm_downcall_data *pdd;
397 pdd = &data->idd_perms[i];
399 printf(" %#jx\t0x%x\n", (uintmax_t)pdd->pdd_nid,
405 #define difftime(a, b) \
406 ((a).tv_sec - (b).tv_sec + \
407 ((a).tv_usec - (b).tv_usec) / 1000000.0)
409 int main(int argc, char **argv)
412 struct identity_downcall_data *data = NULL;
415 struct timeval start, idgot, fini;
416 int fd, rc = -EINVAL, size, maxgroups;
417 bool alreadyfailed = false;
419 progname = basename(argv[0]);
425 uid = strtoul(argv[2], &end, 0);
427 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
430 gettimeofday(&start, NULL);
432 maxgroups = sysconf(_SC_NGROUPS_MAX);
433 if (maxgroups > NGROUPS_MAX)
434 maxgroups = NGROUPS_MAX;
435 if (maxgroups == -1) {
441 size = offsetof(struct identity_downcall_data, idd_groups[maxgroups]);
444 errlog("malloc identity downcall data(%d) failed!\n", size);
445 if (!alreadyfailed) {
446 alreadyfailed = true;
453 memset(data, 0, size);
454 data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
456 /* get groups for uid */
457 rc = get_groups_local(data, maxgroups);
461 size = offsetof(struct identity_downcall_data,
462 idd_groups[data->idd_ngroups]);
463 /* read permission database */
464 rc = get_perms(data);
466 gettimeofday(&idgot, NULL);
468 if (strcmp(argv[1], "-d") == 0 || getenv("L_GETIDENTITY_TEST")) {
474 rc = cfs_get_param_paths(&path, "mdt/%s/identity_info", argv[1]);
480 fd = open(path.gl_pathv[0], O_WRONLY);
482 errlog("can't open file '%s':%s\n", path.gl_pathv[0],
488 rc = write(fd, data, size);
489 gettimeofday(&fini, NULL);
492 errlog("partial write ret %d: %s\n", rc, strerror(errno));
493 if (!alreadyfailed) {
494 alreadyfailed = true;
495 cfs_free_param_data(&path);
504 /* log if it takes more than 20 second to avoid rate limite */
505 if (rc || difftime(fini, start) > 20)
506 errlog("get identity for uid %lu start time %ld.%06ld got time %ld.%06ld end time %ld.%06ld: rc = %d\n",
507 uid, start.tv_sec, start.tv_usec, idgot.tv_sec,
508 idgot.tv_usec, fini.tv_sec, fini.tv_usec, rc);
511 cfs_free_param_data(&path);