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.sun.com/software/products/lustre/docs/GPLv2.pdf
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
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
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>
58 #define PERM_PATHNAME "/etc/lustre/perm.conf"
61 * permission file format is like this:
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.
75 static char *progname;
77 static void usage(void)
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",
86 static int compare_u32(const void *v1, const void *v2)
88 return (*(__u32 *)v1 - *(__u32 *)v2);
91 static void errlog(const char *fmt, ...)
95 openlog(progname, LOG_PERROR | LOG_PID, LOG_AUTHPRIV);
98 vsyslog(LOG_NOTICE, fmt, args);
104 int get_groups_local(struct identity_downcall_data *data,
105 unsigned int maxgroups)
107 gid_t *groups, *groups_tmp = NULL;
108 unsigned int ngroups = 0;
115 pw = getpwuid(data->idd_uid);
117 errlog("no such user %u\n", data->idd_uid);
118 data->idd_err = errno ? errno : EIDRM;
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;
127 pw_name = malloc(namelen);
129 errlog("malloc error\n");
130 data->idd_err = errno;
134 strlcpy(pw_name, pw->pw_name, namelen);
135 groups = data->idd_groups;
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) {
142 data->idd_err = errno ? errno : ENOMEM;
143 errlog("malloc error=%u\n",data->idd_err);
147 ngroups_tmp = maxgroups;
148 if (getgrouplist(pw->pw_name, pw->pw_gid, groups_tmp, &ngroups_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);
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];
164 qsort(groups, ngroups, sizeof(*groups), compare_u32);
165 data->idd_ngroups = ngroups;
172 static inline int comment_line(char *line)
176 while (*p && (*p == ' ' || *p == '\t')) p++;
178 if (!*p || *p == '\n' || *p == '#')
183 static inline int match_uid(uid_t uid, const char *str)
188 if(!strcmp(str, "*"))
191 uid2 = strtoul(str, &end, 0);
194 return (uid == uid2);
202 static perm_type_t perm_types[] = {
203 { "setuid", CFS_SETUID_PERM },
204 { "setgid", CFS_SETGID_PERM },
205 { "setgrp", CFS_SETGRP_PERM },
209 static perm_type_t noperm_types[] = {
210 { "nosetuid", CFS_SETUID_PERM },
211 { "nosetgid", CFS_SETGID_PERM },
212 { "nosetgrp", CFS_SETGRP_PERM },
216 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
227 memset(name, 0, sizeof(name));
228 end = strchr(start, ',');
230 end = str + strlen(str);
234 if (len >= sizeof(name))
236 strncpy(name, start, len);
238 for (pt = perm_types; pt->name; pt++) {
239 if (!strcasecmp(name, pt->name)) {
246 for (pt = noperm_types; pt->name; pt++) {
247 if (!strcasecmp(name, pt->name)) {
254 printf("unkown type: %s\n", name);
265 parse_perm_line(struct identity_downcall_data *data, char *line, size_t size)
274 if (data->idd_nperms >= N_PERMS_MAX) {
275 errlog("permission count %d > max %d\n",
276 data->idd_nperms, N_PERMS_MAX);
280 rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
282 errlog("can't parse line %s\n", line);
286 if (!match_uid(data->idd_uid, uid_str))
289 if (!strcmp(nid_str, "*")) {
292 nid = libcfs_str2nid(nid_str);
293 if (nid == LNET_NID_ANY) {
294 errlog("can't parse nid %s\n", nid_str);
299 if (parse_perm(&perm, &noperm, perm_str)) {
300 errlog("invalid perm %s\n", perm_str);
304 /* merge the perms with the same nid.
306 * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
307 * it must be data->idd_perms[0].pdd_nid, and act as default perm.
309 if (nid != LNET_NID_ANY) {
312 /* search for the same nid */
313 for (i = data->idd_nperms - 1; i >= 0; i--) {
314 if (data->idd_perms[i].pdd_nid == nid) {
315 data->idd_perms[i].pdd_perm =
316 (data->idd_perms[i].pdd_perm | perm) &
323 /* NOT found, add to tail */
325 data->idd_perms[data->idd_nperms].pdd_nid = nid;
326 data->idd_perms[data->idd_nperms].pdd_perm =
331 if (data->idd_nperms > 0) {
332 /* the first one isn't LNET_NID_ANY, need exchange */
333 if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
334 data->idd_perms[data->idd_nperms].pdd_nid =
335 data->idd_perms[0].pdd_nid;
336 data->idd_perms[data->idd_nperms].pdd_perm =
337 data->idd_perms[0].pdd_perm;
338 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
339 data->idd_perms[0].pdd_perm = perm & ~noperm;
342 /* only fix LNET_NID_ANY item */
343 data->idd_perms[0].pdd_perm =
344 (data->idd_perms[0].pdd_perm | perm) &
348 /* it is the first one, only add to head */
349 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
350 data->idd_perms[0].pdd_perm = perm & ~noperm;
351 data->idd_nperms = 1;
358 int get_perms(struct identity_downcall_data *data)
363 fp = fopen(PERM_PATHNAME, "r");
365 if (errno == ENOENT) {
368 errlog("open %s failed: %s\n",
369 PERM_PATHNAME, strerror(errno));
370 data->idd_err = errno;
375 while (fgets(line, sizeof(line), fp)) {
376 if (comment_line(line))
379 if (parse_perm_line(data, line, sizeof(line))) {
380 errlog("parse line %s failed!\n", line);
381 data->idd_err = EINVAL;
391 static void show_result(struct identity_downcall_data *data)
396 errlog("failed to get identity for uid %d: %s\n",
397 data->idd_uid, strerror(data->idd_err));
401 printf("uid=%d gid=%d", data->idd_uid, data->idd_gid);
402 for (i = 0; i < data->idd_ngroups; i++)
403 printf(",%u", data->idd_groups[i]);
405 printf("permissions:\n"
407 for (i = 0; i < data->idd_nperms; i++) {
408 struct perm_downcall_data *pdd;
410 pdd = &data->idd_perms[i];
412 printf(" %#jx\t0x%x\n", (uintmax_t)pdd->pdd_nid,
418 int main(int argc, char **argv)
421 struct identity_downcall_data *data = NULL;
424 int fd, rc = -EINVAL, size, maxgroups;
426 progname = basename(argv[0]);
432 uid = strtoul(argv[2], &end, 0);
434 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
438 maxgroups = sysconf(_SC_NGROUPS_MAX);
439 if (maxgroups > NGROUPS_MAX)
440 maxgroups = NGROUPS_MAX;
441 if (maxgroups == -1) {
446 size = offsetof(struct identity_downcall_data, idd_groups[maxgroups]);
449 errlog("malloc identity downcall data(%d) failed!\n", size);
454 memset(data, 0, size);
455 data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
457 /* get groups for uid */
458 rc = get_groups_local(data, maxgroups);
462 size = offsetof(struct identity_downcall_data,
463 idd_groups[data->idd_ngroups]);
464 /* read permission database */
465 rc = get_perms(data);
468 if (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);
491 errlog("partial write ret %d: %s\n", rc, strerror(errno));
498 cfs_free_param_data(&path);