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.
51 #include <libcfs/util/string.h>
52 #include <libcfs/libcfs.h>
53 #include <lnet/nidstr.h>
54 #include <lustre/lustre_user.h>
55 #include <lustre/lustre_idl.h>
57 #define PERM_PATHNAME "/etc/lustre/perm.conf"
60 * permission file format is like this:
63 * '*' nid means any nid
64 * '*' uid means any uid
65 * the valid values for perms are:
66 * setuid/setgid/setgrp/rmtacl -- enable corresponding perm
67 * nosetuid/nosetgid/nosetgrp/normtacl -- disable corresponding perm
68 * they can be listed together, seperated by ',',
69 * when perm and noperm are in the same line (item), noperm is preferential,
70 * when they are in different lines (items), the latter is preferential,
71 * '*' nid is as default perm, and is not preferential.
74 static char *progname;
76 static void usage(void)
79 "\nusage: %s {mdtname} {uid}\n"
80 "Normally invoked as an upcall from Lustre, set via:\n"
81 "/proc/fs/lustre/mdt/${mdtname}/identity_upcall\n",
85 static int compare_u32(const void *v1, const void *v2)
87 return (*(__u32 *)v1 - *(__u32 *)v2);
90 static void errlog(const char *fmt, ...)
94 openlog(progname, LOG_PERROR | LOG_PID, LOG_AUTHPRIV);
97 vsyslog(LOG_NOTICE, fmt, args);
103 int get_groups_local(struct identity_downcall_data *data,
104 unsigned int maxgroups)
106 gid_t *groups, *groups_tmp = NULL;
107 unsigned int ngroups = 0;
114 pw = getpwuid(data->idd_uid);
116 errlog("no such user %u\n", data->idd_uid);
117 data->idd_err = errno ? errno : EIDRM;
121 data->idd_gid = pw->pw_gid;
122 namelen = sysconf(_SC_LOGIN_NAME_MAX);
123 if (namelen < _POSIX_LOGIN_NAME_MAX)
124 namelen = _POSIX_LOGIN_NAME_MAX;
126 pw_name = malloc(namelen);
128 errlog("malloc error\n");
129 data->idd_err = errno;
133 strlcpy(pw_name, pw->pw_name, namelen);
134 groups = data->idd_groups;
136 /* Allocate array of size maxgroups instead of handling two
137 * consecutive and potentially racy getgrouplist() calls. */
138 groups_tmp = malloc(maxgroups * sizeof(gid_t));
139 if (groups_tmp == NULL) {
141 data->idd_err = errno ? errno : ENOMEM;
142 errlog("malloc error=%u\n",data->idd_err);
146 ngroups_tmp = maxgroups;
147 if (getgrouplist(pw->pw_name, pw->pw_gid, groups_tmp, &ngroups_tmp) <
151 data->idd_err = errno ? errno : EIDRM;
152 errlog("getgrouplist() error for uid %u: error=%u\n",
153 data->idd_uid, data->idd_err);
157 /* Do not place user's group ID in to the resulting groups list */
158 for (i = 0; i < ngroups_tmp; i++)
159 if (pw->pw_gid != groups_tmp[i])
160 groups[ngroups++] = groups_tmp[i];
163 qsort(groups, ngroups, sizeof(*groups), compare_u32);
164 data->idd_ngroups = ngroups;
171 static inline int comment_line(char *line)
175 while (*p && (*p == ' ' || *p == '\t')) p++;
177 if (!*p || *p == '\n' || *p == '#')
182 static inline int match_uid(uid_t uid, const char *str)
187 if(!strcmp(str, "*"))
190 uid2 = strtoul(str, &end, 0);
193 return (uid == uid2);
201 static perm_type_t perm_types[] = {
202 { "setuid", CFS_SETUID_PERM },
203 { "setgid", CFS_SETGID_PERM },
204 { "setgrp", CFS_SETGRP_PERM },
205 { "rmtacl", CFS_RMTACL_PERM },
206 { "rmtown", CFS_RMTOWN_PERM },
210 static perm_type_t noperm_types[] = {
211 { "nosetuid", CFS_SETUID_PERM },
212 { "nosetgid", CFS_SETGID_PERM },
213 { "nosetgrp", CFS_SETGRP_PERM },
214 { "normtacl", CFS_RMTACL_PERM },
215 { "normtown", CFS_RMTOWN_PERM },
219 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
230 memset(name, 0, sizeof(name));
231 end = strchr(start, ',');
233 end = str + strlen(str);
237 if (len >= sizeof(name))
239 strncpy(name, start, len);
241 for (pt = perm_types; pt->name; pt++) {
242 if (!strcasecmp(name, pt->name)) {
249 for (pt = noperm_types; pt->name; pt++) {
250 if (!strcasecmp(name, pt->name)) {
257 printf("unkown type: %s\n", name);
268 parse_perm_line(struct identity_downcall_data *data, char *line, size_t size)
277 if (data->idd_nperms >= N_PERMS_MAX) {
278 errlog("permission count %d > max %d\n",
279 data->idd_nperms, N_PERMS_MAX);
283 rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
285 errlog("can't parse line %s\n", line);
289 if (!match_uid(data->idd_uid, uid_str))
292 if (!strcmp(nid_str, "*")) {
295 nid = libcfs_str2nid(nid_str);
296 if (nid == LNET_NID_ANY) {
297 errlog("can't parse nid %s\n", nid_str);
302 if (parse_perm(&perm, &noperm, perm_str)) {
303 errlog("invalid perm %s\n", perm_str);
307 /* merge the perms with the same nid.
309 * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
310 * it must be data->idd_perms[0].pdd_nid, and act as default perm.
312 if (nid != LNET_NID_ANY) {
315 /* search for the same nid */
316 for (i = data->idd_nperms - 1; i >= 0; i--) {
317 if (data->idd_perms[i].pdd_nid == nid) {
318 data->idd_perms[i].pdd_perm =
319 (data->idd_perms[i].pdd_perm | perm) &
326 /* NOT found, add to tail */
328 data->idd_perms[data->idd_nperms].pdd_nid = nid;
329 data->idd_perms[data->idd_nperms].pdd_perm =
334 if (data->idd_nperms > 0) {
335 /* the first one isn't LNET_NID_ANY, need exchange */
336 if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
337 data->idd_perms[data->idd_nperms].pdd_nid =
338 data->idd_perms[0].pdd_nid;
339 data->idd_perms[data->idd_nperms].pdd_perm =
340 data->idd_perms[0].pdd_perm;
341 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
342 data->idd_perms[0].pdd_perm = perm & ~noperm;
345 /* only fix LNET_NID_ANY item */
346 data->idd_perms[0].pdd_perm =
347 (data->idd_perms[0].pdd_perm | perm) &
351 /* it is the first one, only add to head */
352 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
353 data->idd_perms[0].pdd_perm = perm & ~noperm;
354 data->idd_nperms = 1;
361 int get_perms(struct identity_downcall_data *data)
366 fp = fopen(PERM_PATHNAME, "r");
368 if (errno == ENOENT) {
371 errlog("open %s failed: %s\n",
372 PERM_PATHNAME, strerror(errno));
373 data->idd_err = errno;
378 while (fgets(line, sizeof(line), fp)) {
379 if (comment_line(line))
382 if (parse_perm_line(data, line, sizeof(line))) {
383 errlog("parse line %s failed!\n", line);
384 data->idd_err = EINVAL;
394 static void show_result(struct identity_downcall_data *data)
399 errlog("failed to get identity for uid %d: %s\n",
400 data->idd_uid, strerror(data->idd_err));
404 printf("uid=%d gid=%d", data->idd_uid, data->idd_gid);
405 for (i = 0; i < data->idd_ngroups; i++)
406 printf(",%u", data->idd_groups[i]);
408 printf("permissions:\n"
410 for (i = 0; i < data->idd_nperms; i++) {
411 struct perm_downcall_data *pdd;
413 pdd = &data->idd_perms[i];
415 printf(" "LPX64"\t0x%x\n", pdd->pdd_nid, pdd->pdd_perm);
420 int main(int argc, char **argv)
423 struct identity_downcall_data *data = NULL;
426 int fd, rc = -EINVAL, size, maxgroups;
428 progname = basename(argv[0]);
434 uid = strtoul(argv[2], &end, 0);
436 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
440 maxgroups = sysconf(_SC_NGROUPS_MAX);
441 if (maxgroups > NGROUPS_MAX)
442 maxgroups = NGROUPS_MAX;
443 if (maxgroups == -1) {
448 size = offsetof(struct identity_downcall_data, idd_groups[maxgroups]);
451 errlog("malloc identity downcall data(%d) failed!\n", size);
456 memset(data, 0, size);
457 data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
459 /* get groups for uid */
460 rc = get_groups_local(data, maxgroups);
464 size = offsetof(struct identity_downcall_data,
465 idd_groups[data->idd_ngroups]);
466 /* read permission database */
467 rc = get_perms(data);
470 if (getenv("L_GETIDENTITY_TEST")) {
476 snprintf(procname, sizeof(procname),
477 "/proc/fs/lustre/mdt/%s/identity_info", argv[1]);
478 fd = open(procname, O_WRONLY);
480 errlog("can't open file %s: %s\n", procname, strerror(errno));
485 rc = write(fd, data, size);
488 errlog("partial write ret %d: %s\n", rc, strerror(errno));