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/
47 #include <libcfs/util/param.h>
48 #include <linux/lnet/nidstr.h>
49 #include <linux/lustre/lustre_user.h>
50 #include <linux/lustre/lustre_idl.h>
52 #define PERM_PATHNAME "/etc/lustre/perm.conf"
55 * permission file format is like this:
58 * '*' nid means any nid
59 * '*' uid means any uid
60 * the valid values for perms are:
61 * setuid/setgid/setgrp -- enable corresponding perm
62 * nosetuid/nosetgid/nosetgrp -- disable corresponding perm
63 * they can be listed together, separated by ',',
64 * when perm and noperm are in the same line (item), noperm is preferential,
65 * when they are in different lines (items), the latter is preferential,
66 * '*' nid is as default perm, and is not preferential.
69 static char *progname;
71 static void usage(void)
74 "\nusage: %s {-d|mdtname} {uid}\n"
75 "Normally invoked as an upcall from Lustre, set via:\n"
76 "lctl set_param mdt.${mdtname}.identity_upcall={path to upcall}\n"
77 "\t-d: debug, print values to stdout instead of Lustre\n",
81 static int compare_u32(const void *v1, const void *v2)
83 return *(__u32 *)v1 - *(__u32 *)v2;
86 static void errlog(const char *fmt, ...)
90 openlog(progname, LOG_PERROR | LOG_PID, LOG_AUTHPRIV);
93 vsyslog(LOG_WARNING, fmt, args);
99 int get_groups_local(struct identity_downcall_data *data,
100 unsigned int maxgroups)
102 gid_t *groups, *groups_tmp = NULL;
103 unsigned int ngroups = 0;
108 pw = getpwuid(data->idd_uid);
110 errlog("no such user %u\n", data->idd_uid);
111 data->idd_err = errno ? errno : EIDRM;
115 data->idd_gid = pw->pw_gid;
117 groups = data->idd_groups;
120 * Allocate array of size maxgroups instead of handling two
121 * consecutive and potentially racy getgrouplist() calls.
123 groups_tmp = malloc(maxgroups * sizeof(gid_t));
125 data->idd_err = errno ? errno : ENOMEM;
126 errlog("malloc error=%u\n", data->idd_err);
130 ngroups_tmp = maxgroups;
131 if (getgrouplist(pw->pw_name, pw->pw_gid, groups_tmp, &ngroups_tmp) <
134 data->idd_err = errno ? errno : EIDRM;
135 errlog("getgrouplist() error for uid %u: error=%u\n",
136 data->idd_uid, data->idd_err);
140 /* Do not place user's group ID in to the resulting groups list */
141 for (i = 0; i < ngroups_tmp; i++)
142 if (pw->pw_gid != groups_tmp[i])
143 groups[ngroups++] = groups_tmp[i];
146 qsort(groups, ngroups, sizeof(*groups), compare_u32);
147 data->idd_ngroups = ngroups;
153 static inline int comment_line(char *line)
157 while (*p && (*p == ' ' || *p == '\t'))
160 if (!*p || *p == '\n' || *p == '#')
165 static inline int match_uid(uid_t uid, const char *str)
170 if (!strcmp(str, "*"))
173 uid2 = strtoul(str, &end, 0);
176 return (uid == uid2);
184 static perm_type_t perm_types[] = {
185 { "setuid", CFS_SETUID_PERM },
186 { "setgid", CFS_SETGID_PERM },
187 { "setgrp", CFS_SETGRP_PERM },
193 static perm_type_t noperm_types[] = {
194 { "nosetuid", CFS_SETUID_PERM },
195 { "nosetgid", CFS_SETGID_PERM },
196 { "nosetgrp", CFS_SETGRP_PERM },
202 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
214 memset(name, 0, sizeof(name));
215 end = strchr(start, ',');
217 end = str + strlen(str);
221 if (len >= sizeof(name))
223 strncpy(name, start, len);
225 for (pt = perm_types; pt->name; pt++) {
226 if (!strcasecmp(name, pt->name)) {
233 for (pt = noperm_types; pt->name; pt++) {
234 if (!strcasecmp(name, pt->name)) {
241 printf("unkown type: %s\n", name);
252 parse_perm_line(struct identity_downcall_data *data, char *line, size_t size)
261 if (data->idd_nperms >= N_PERMS_MAX) {
262 errlog("permission count %d > max %d\n",
263 data->idd_nperms, N_PERMS_MAX);
267 rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
269 errlog("can't parse line %s\n", line);
273 if (!match_uid(data->idd_uid, uid_str))
276 if (!strcmp(nid_str, "*")) {
279 nid = libcfs_str2nid(nid_str);
280 if (nid == LNET_NID_ANY) {
281 errlog("can't parse nid %s\n", nid_str);
286 if (parse_perm(&perm, &noperm, perm_str)) {
287 errlog("invalid perm %s\n", perm_str);
292 * merge the perms with the same nid.
294 * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
295 * it must be data->idd_perms[0].pdd_nid, and act as default perm.
297 if (nid != LNET_NID_ANY) {
300 /* search for the same nid */
301 for (i = data->idd_nperms - 1; i >= 0; i--) {
302 if (data->idd_perms[i].pdd_nid == nid) {
303 data->idd_perms[i].pdd_perm =
304 (data->idd_perms[i].pdd_perm | perm) &
311 /* NOT found, add to tail */
313 data->idd_perms[data->idd_nperms].pdd_nid = nid;
314 data->idd_perms[data->idd_nperms].pdd_perm =
319 if (data->idd_nperms > 0) {
320 /* the first one isn't LNET_NID_ANY, need exchange */
321 if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
322 data->idd_perms[data->idd_nperms].pdd_nid =
323 data->idd_perms[0].pdd_nid;
324 data->idd_perms[data->idd_nperms].pdd_perm =
325 data->idd_perms[0].pdd_perm;
326 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
327 data->idd_perms[0].pdd_perm = perm & ~noperm;
330 /* only fix LNET_NID_ANY item */
331 data->idd_perms[0].pdd_perm =
332 (data->idd_perms[0].pdd_perm | perm) &
336 /* it is the first one, only add to head */
337 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
338 data->idd_perms[0].pdd_perm = perm & ~noperm;
339 data->idd_nperms = 1;
346 int get_perms(struct identity_downcall_data *data)
351 fp = fopen(PERM_PATHNAME, "r");
355 errlog("open %s failed: %s\n",
356 PERM_PATHNAME, strerror(errno));
357 data->idd_err = errno;
361 while (fgets(line, sizeof(line), fp)) {
362 if (comment_line(line))
365 if (parse_perm_line(data, line, sizeof(line))) {
366 errlog("parse line %s failed!\n", line);
367 data->idd_err = EINVAL;
377 static void show_result(struct identity_downcall_data *data)
382 errlog("failed to get identity for uid %d: %s\n",
383 data->idd_uid, strerror(data->idd_err));
387 printf("uid=%d gid=%d", data->idd_uid, data->idd_gid);
388 for (i = 0; i < data->idd_ngroups; i++)
389 printf(",%u", data->idd_groups[i]);
391 printf("permissions:\n"
393 for (i = 0; i < data->idd_nperms; i++) {
394 struct perm_downcall_data *pdd;
396 pdd = &data->idd_perms[i];
398 printf(" %#jx\t0x%x\n", (uintmax_t)pdd->pdd_nid,
404 int main(int argc, char **argv)
407 struct identity_downcall_data *data = NULL;
410 int fd, rc = -EINVAL, size, maxgroups;
411 bool alreadyfailed = false;
413 progname = basename(argv[0]);
419 uid = strtoul(argv[2], &end, 0);
421 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
425 maxgroups = sysconf(_SC_NGROUPS_MAX);
426 if (maxgroups > NGROUPS_MAX)
427 maxgroups = NGROUPS_MAX;
428 if (maxgroups == -1) {
434 size = offsetof(struct identity_downcall_data, idd_groups[maxgroups]);
437 errlog("malloc identity downcall data(%d) failed!\n", size);
438 if (!alreadyfailed) {
439 alreadyfailed = true;
446 memset(data, 0, size);
447 data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
449 /* get groups for uid */
450 rc = get_groups_local(data, maxgroups);
454 size = offsetof(struct identity_downcall_data,
455 idd_groups[data->idd_ngroups]);
456 /* read permission database */
457 rc = get_perms(data);
460 if (strcmp(argv[1], "-d") == 0 || getenv("L_GETIDENTITY_TEST")) {
466 rc = cfs_get_param_paths(&path, "mdt/%s/identity_info", argv[1]);
472 fd = open(path.gl_pathv[0], O_WRONLY);
474 errlog("can't open file '%s':%s\n", path.gl_pathv[0],
480 rc = write(fd, data, size);
483 errlog("partial write ret %d: %s\n", rc, strerror(errno));
484 if (!alreadyfailed) {
485 alreadyfailed = true;
486 cfs_free_param_data(&path);
497 cfs_free_param_data(&path);