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, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
51 #include <liblustre.h>
52 #include <lustre/lustre_user.h>
53 #include <lustre/lustre_idl.h>
55 #define PERM_PATHNAME "/etc/lustre/perm.conf"
58 * permission file format is like this:
61 * '*' nid means any nid
62 * '*' uid means any uid
63 * the valid values for perms are:
64 * setuid/setgid/setgrp/rmtacl -- enable corresponding perm
65 * nosetuid/nosetgid/nosetgrp/normtacl -- disable corresponding perm
66 * they can be listed together, seperated by ',',
67 * when perm and noperm are in the same line (item), noperm is preferential,
68 * when they are in different lines (items), the latter is preferential,
69 * '*' nid is as default perm, and is not preferential.
72 static char *progname;
74 static void usage(void)
77 "\nusage: %s {mdtname} {uid}\n"
78 "Normally invoked as an upcall from Lustre, set via:\n"
79 "/proc/fs/lustre/mdt/${mdtname}/identity_upcall\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_NOTICE, 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 memset(pw_name, 0, namelen);
132 strncpy(pw_name, pw->pw_name, namelen - 1);
133 groups = data->idd_groups;
135 /* Allocate array of size maxgroups instead of handling two
136 * consecutive and potentially racy getgrouplist() calls. */
137 groups_tmp = malloc(maxgroups * sizeof(gid_t));
138 if (groups_tmp == NULL) {
140 data->idd_err = errno ? errno : ENOMEM;
141 errlog("malloc error=%u\n",data->idd_err);
145 ngroups_tmp = maxgroups;
146 if (getgrouplist(pw->pw_name, pw->pw_gid, groups_tmp, &ngroups_tmp) <
150 data->idd_err = errno ? errno : EIDRM;
151 errlog("getgrouplist() error for uid %u: error=%u\n",
152 data->idd_uid, data->idd_err);
156 /* Do not place user's group ID in to the resulting groups list */
157 for (i = 0; i < ngroups_tmp; i++)
158 if (pw->pw_gid != groups_tmp[i])
159 groups[ngroups++] = groups_tmp[i];
162 qsort(groups, ngroups, sizeof(*groups), compare_u32);
163 data->idd_ngroups = ngroups;
170 static inline int comment_line(char *line)
174 while (*p && (*p == ' ' || *p == '\t')) p++;
176 if (!*p || *p == '\n' || *p == '#')
181 static inline int match_uid(uid_t uid, const char *str)
186 if(!strcmp(str, "*"))
189 uid2 = strtoul(str, &end, 0);
192 return (uid == uid2);
200 static perm_type_t perm_types[] = {
201 { "setuid", CFS_SETUID_PERM },
202 { "setgid", CFS_SETGID_PERM },
203 { "setgrp", CFS_SETGRP_PERM },
204 { "rmtacl", CFS_RMTACL_PERM },
205 { "rmtown", CFS_RMTOWN_PERM },
209 static perm_type_t noperm_types[] = {
210 { "nosetuid", CFS_SETUID_PERM },
211 { "nosetgid", CFS_SETGID_PERM },
212 { "nosetgrp", CFS_SETGRP_PERM },
213 { "normtacl", CFS_RMTACL_PERM },
214 { "normtown", CFS_RMTOWN_PERM },
218 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
228 memset(name, 0, sizeof(name));
229 end = strchr(start, ',');
231 end = str + strlen(str);
234 strncpy(name, start, end - start);
235 for (pt = perm_types; pt->name; pt++) {
236 if (!strcasecmp(name, pt->name)) {
243 for (pt = noperm_types; pt->name; pt++) {
244 if (!strcasecmp(name, pt->name)) {
251 printf("unkown type: %s\n", name);
261 int parse_perm_line(struct identity_downcall_data *data, char *line)
263 char uid_str[256], nid_str[256], perm_str[256];
268 if (data->idd_nperms >= N_PERMS_MAX) {
269 errlog("permission count %d > max %d\n",
270 data->idd_nperms, N_PERMS_MAX);
274 rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
276 errlog("can't parse line %s\n", line);
280 if (!match_uid(data->idd_uid, uid_str))
283 if (!strcmp(nid_str, "*")) {
286 nid = libcfs_str2nid(nid_str);
287 if (nid == LNET_NID_ANY) {
288 errlog("can't parse nid %s\n", nid_str);
293 if (parse_perm(&perm, &noperm, perm_str)) {
294 errlog("invalid perm %s\n", perm_str);
298 /* merge the perms with the same nid.
300 * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
301 * it must be data->idd_perms[0].pdd_nid, and act as default perm.
303 if (nid != LNET_NID_ANY) {
306 /* search for the same nid */
307 for (i = data->idd_nperms - 1; i >= 0; i--) {
308 if (data->idd_perms[i].pdd_nid == nid) {
309 data->idd_perms[i].pdd_perm =
310 (data->idd_perms[i].pdd_perm | perm) &
317 /* NOT found, add to tail */
319 data->idd_perms[data->idd_nperms].pdd_nid = nid;
320 data->idd_perms[data->idd_nperms].pdd_perm =
325 if (data->idd_nperms > 0) {
326 /* the first one isn't LNET_NID_ANY, need exchange */
327 if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
328 data->idd_perms[data->idd_nperms].pdd_nid =
329 data->idd_perms[0].pdd_nid;
330 data->idd_perms[data->idd_nperms].pdd_perm =
331 data->idd_perms[0].pdd_perm;
332 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
333 data->idd_perms[0].pdd_perm = perm & ~noperm;
336 /* only fix LNET_NID_ANY item */
337 data->idd_perms[0].pdd_perm =
338 (data->idd_perms[0].pdd_perm | perm) &
342 /* it is the first one, only add to head */
343 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
344 data->idd_perms[0].pdd_perm = perm & ~noperm;
345 data->idd_nperms = 1;
352 int get_perms(struct identity_downcall_data *data)
357 fp = fopen(PERM_PATHNAME, "r");
359 if (errno == ENOENT) {
362 errlog("open %s failed: %s\n",
363 PERM_PATHNAME, strerror(errno));
364 data->idd_err = errno;
369 while (fgets(line, 1024, fp)) {
370 if (comment_line(line))
373 if (parse_perm_line(data, line)) {
374 errlog("parse line %s failed!\n", line);
375 data->idd_err = EINVAL;
385 static void show_result(struct identity_downcall_data *data)
390 errlog("failed to get identity for uid %d: %s\n",
391 data->idd_uid, strerror(data->idd_err));
395 printf("uid=%d gid=%d", data->idd_uid, data->idd_gid);
396 for (i = 0; i < data->idd_ngroups; i++)
397 printf(",%u", data->idd_groups[i]);
399 printf("permissions:\n"
401 for (i = 0; i < data->idd_nperms; i++) {
402 struct perm_downcall_data *pdd;
404 pdd = &data->idd_perms[i];
406 printf(" "LPX64"\t0x%x\n", pdd->pdd_nid, pdd->pdd_perm);
411 int main(int argc, char **argv)
414 struct identity_downcall_data *data = NULL;
417 int fd, rc = -EINVAL, size, maxgroups;
419 progname = basename(argv[0]);
425 uid = strtoul(argv[2], &end, 0);
427 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
431 maxgroups = sysconf(_SC_NGROUPS_MAX);
432 if (maxgroups > NGROUPS_MAX)
433 maxgroups = NGROUPS_MAX;
434 if (maxgroups == -1) {
439 size = offsetof(struct identity_downcall_data, idd_groups[maxgroups]);
442 errlog("malloc identity downcall data(%d) failed!\n", size);
447 memset(data, 0, size);
448 data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
450 /* get groups for uid */
451 rc = get_groups_local(data, maxgroups);
455 size = offsetof(struct identity_downcall_data,
456 idd_groups[data->idd_ngroups]);
457 /* read permission database */
458 rc = get_perms(data);
461 if (getenv("L_GETIDENTITY_TEST")) {
467 snprintf(procname, sizeof(procname),
468 "/proc/fs/lustre/mdt/%s/identity_info", argv[1]);
469 fd = open(procname, O_WRONLY);
471 errlog("can't open file %s: %s\n", procname, strerror(errno));
476 rc = write(fd, data, size);
479 errlog("partial write ret %d: %s\n", rc, strerror(errno));