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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
48 #include <libcfs/util/param.h>
49 #include <libcfs/util/string.h>
50 #include <linux/lnet/nidstr.h>
51 #include <linux/lustre/lustre_user.h>
52 #include <linux/lustre/lustre_idl.h>
54 #define PERM_PATHNAME "/etc/lustre/perm.conf"
57 * permission file format is like this:
60 * '*' nid means any nid
61 * '*' uid means any uid
62 * the valid values for perms are:
63 * setuid/setgid/setgrp -- enable corresponding perm
64 * nosetuid/nosetgid/nosetgrp -- disable corresponding perm
65 * they can be listed together, separated by ',',
66 * when perm and noperm are in the same line (item), noperm is preferential,
67 * when they are in different lines (items), the latter is preferential,
68 * '*' nid is as default perm, and is not preferential.
71 static char *progname;
73 static void usage(void)
76 "\nusage: %s {-d|mdtname} {uid}\n"
77 "Normally invoked as an upcall from Lustre, set via:\n"
78 "lctl set_param mdt.${mdtname}.identity_upcall={path to upcall}\n"
79 "\t-d: debug, print values to stdout instead of Lustre\n",
83 static int compare_u32(const void *v1, const void *v2)
85 return (*(__u32 *)v1 - *(__u32 *)v2);
88 static void errlog(const char *fmt, ...)
92 openlog(progname, LOG_PERROR | LOG_PID, LOG_AUTHPRIV);
95 vsyslog(LOG_WARNING, fmt, args);
101 int get_groups_local(struct identity_downcall_data *data,
102 unsigned int maxgroups)
104 gid_t *groups, *groups_tmp = NULL;
105 unsigned int ngroups = 0;
112 pw = getpwuid(data->idd_uid);
114 errlog("no such user %u\n", data->idd_uid);
115 data->idd_err = errno ? errno : EIDRM;
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;
124 pw_name = malloc(namelen);
126 errlog("malloc error\n");
127 data->idd_err = errno;
131 strlcpy(pw_name, pw->pw_name, namelen);
132 groups = data->idd_groups;
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) {
139 data->idd_err = errno ? errno : ENOMEM;
140 errlog("malloc error=%u\n",data->idd_err);
144 ngroups_tmp = maxgroups;
145 if (getgrouplist(pw->pw_name, pw->pw_gid, groups_tmp, &ngroups_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);
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];
161 qsort(groups, ngroups, sizeof(*groups), compare_u32);
162 data->idd_ngroups = ngroups;
169 static inline int comment_line(char *line)
173 while (*p && (*p == ' ' || *p == '\t')) p++;
175 if (!*p || *p == '\n' || *p == '#')
180 static inline int match_uid(uid_t uid, const char *str)
185 if(!strcmp(str, "*"))
188 uid2 = strtoul(str, &end, 0);
191 return (uid == uid2);
199 static perm_type_t perm_types[] = {
200 { "setuid", CFS_SETUID_PERM },
201 { "setgid", CFS_SETGID_PERM },
202 { "setgrp", CFS_SETGRP_PERM },
208 static perm_type_t noperm_types[] = {
209 { "nosetuid", CFS_SETUID_PERM },
210 { "nosetgid", CFS_SETGID_PERM },
211 { "nosetgrp", CFS_SETGRP_PERM },
217 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
228 memset(name, 0, sizeof(name));
229 end = strchr(start, ',');
231 end = str + strlen(str);
235 if (len >= sizeof(name))
237 strncpy(name, start, len);
239 for (pt = perm_types; pt->name; pt++) {
240 if (!strcasecmp(name, pt->name)) {
247 for (pt = noperm_types; pt->name; pt++) {
248 if (!strcasecmp(name, pt->name)) {
255 printf("unkown type: %s\n", name);
266 parse_perm_line(struct identity_downcall_data *data, char *line, size_t size)
275 if (data->idd_nperms >= N_PERMS_MAX) {
276 errlog("permission count %d > max %d\n",
277 data->idd_nperms, N_PERMS_MAX);
281 rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
283 errlog("can't parse line %s\n", line);
287 if (!match_uid(data->idd_uid, uid_str))
290 if (!strcmp(nid_str, "*")) {
293 nid = libcfs_str2nid(nid_str);
294 if (nid == LNET_NID_ANY) {
295 errlog("can't parse nid %s\n", nid_str);
300 if (parse_perm(&perm, &noperm, perm_str)) {
301 errlog("invalid perm %s\n", perm_str);
305 /* merge the perms with the same nid.
307 * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
308 * it must be data->idd_perms[0].pdd_nid, and act as default perm.
310 if (nid != LNET_NID_ANY) {
313 /* search for the same nid */
314 for (i = data->idd_nperms - 1; i >= 0; i--) {
315 if (data->idd_perms[i].pdd_nid == nid) {
316 data->idd_perms[i].pdd_perm =
317 (data->idd_perms[i].pdd_perm | perm) &
324 /* NOT found, add to tail */
326 data->idd_perms[data->idd_nperms].pdd_nid = nid;
327 data->idd_perms[data->idd_nperms].pdd_perm =
332 if (data->idd_nperms > 0) {
333 /* the first one isn't LNET_NID_ANY, need exchange */
334 if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
335 data->idd_perms[data->idd_nperms].pdd_nid =
336 data->idd_perms[0].pdd_nid;
337 data->idd_perms[data->idd_nperms].pdd_perm =
338 data->idd_perms[0].pdd_perm;
339 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
340 data->idd_perms[0].pdd_perm = perm & ~noperm;
343 /* only fix LNET_NID_ANY item */
344 data->idd_perms[0].pdd_perm =
345 (data->idd_perms[0].pdd_perm | perm) &
349 /* it is the first one, only add to head */
350 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
351 data->idd_perms[0].pdd_perm = perm & ~noperm;
352 data->idd_nperms = 1;
359 int get_perms(struct identity_downcall_data *data)
364 fp = fopen(PERM_PATHNAME, "r");
366 if (errno == ENOENT) {
369 errlog("open %s failed: %s\n",
370 PERM_PATHNAME, strerror(errno));
371 data->idd_err = errno;
376 while (fgets(line, sizeof(line), fp)) {
377 if (comment_line(line))
380 if (parse_perm_line(data, line, sizeof(line))) {
381 errlog("parse line %s failed!\n", line);
382 data->idd_err = EINVAL;
392 static void show_result(struct identity_downcall_data *data)
397 errlog("failed to get identity for uid %d: %s\n",
398 data->idd_uid, strerror(data->idd_err));
402 printf("uid=%d gid=%d", data->idd_uid, data->idd_gid);
403 for (i = 0; i < data->idd_ngroups; i++)
404 printf(",%u", data->idd_groups[i]);
406 printf("permissions:\n"
408 for (i = 0; i < data->idd_nperms; i++) {
409 struct perm_downcall_data *pdd;
411 pdd = &data->idd_perms[i];
413 printf(" %#jx\t0x%x\n", (uintmax_t)pdd->pdd_nid,
419 int main(int argc, char **argv)
422 struct identity_downcall_data *data = NULL;
425 int fd, rc = -EINVAL, size, maxgroups;
427 progname = basename(argv[0]);
433 uid = strtoul(argv[2], &end, 0);
435 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
439 maxgroups = sysconf(_SC_NGROUPS_MAX);
440 if (maxgroups > NGROUPS_MAX)
441 maxgroups = NGROUPS_MAX;
442 if (maxgroups == -1) {
447 size = offsetof(struct identity_downcall_data, idd_groups[maxgroups]);
450 errlog("malloc identity downcall data(%d) failed!\n", size);
455 memset(data, 0, size);
456 data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
458 /* get groups for uid */
459 rc = get_groups_local(data, maxgroups);
463 size = offsetof(struct identity_downcall_data,
464 idd_groups[data->idd_ngroups]);
465 /* read permission database */
466 rc = get_perms(data);
469 if (strcmp(argv[1], "-d") == 0 || getenv("L_GETIDENTITY_TEST")) {
475 rc = cfs_get_param_paths(&path, "mdt/%s/identity_info", argv[1]);
481 fd = open(path.gl_pathv[0], O_WRONLY);
483 errlog("can't open file '%s':%s\n", path.gl_pathv[0],
489 rc = write(fd, data, size);
492 errlog("partial write ret %d: %s\n", rc, strerror(errno));
499 cfs_free_param_data(&path);