1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
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)
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;
118 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;
123 pw_name = (char *)malloc(namelen);
125 errlog("malloc error\n");
126 data->idd_err = errno;
129 memset(pw_name, 0, namelen);
130 strncpy(pw_name, pw->pw_name, namelen - 1);
132 maxgroups = sysconf(_SC_NGROUPS_MAX);
133 if (maxgroups > NGROUPS_MAX)
134 maxgroups = NGROUPS_MAX;
135 groups = data->idd_groups;
137 groups[ngroups++] = pw->pw_gid;
138 while ((gr = getgrent())) {
139 if (gr->gr_gid == groups[0])
143 for (i = 0; gr->gr_mem[i]; i++) {
144 if (!strcmp(gr->gr_mem[i], pw_name)) {
145 groups[ngroups++] = gr->gr_gid;
149 if (ngroups == maxgroups)
153 qsort(groups, ngroups, sizeof(*groups), compare_u32);
154 data->idd_ngroups = ngroups;
160 static inline int comment_line(char *line)
164 while (*p && (*p == ' ' || *p == '\t')) p++;
166 if (!*p || *p == '\n' || *p == '#')
171 static inline int match_uid(uid_t uid, const char *str)
176 if(!strcmp(str, "*"))
179 uid2 = strtoul(str, &end, 0);
183 return (uid == uid2);
191 static perm_type_t perm_types[] = {
192 { "setuid", CFS_SETUID_PERM },
193 { "setgid", CFS_SETGID_PERM },
194 { "setgrp", CFS_SETGRP_PERM },
195 { "rmtacl", CFS_RMTACL_PERM },
196 { "rmtown", CFS_RMTOWN_PERM },
200 static perm_type_t noperm_types[] = {
201 { "nosetuid", CFS_SETUID_PERM },
202 { "nosetgid", CFS_SETGID_PERM },
203 { "nosetgrp", CFS_SETGRP_PERM },
204 { "normtacl", CFS_RMTACL_PERM },
205 { "normtown", CFS_RMTOWN_PERM },
209 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
219 memset(name, 0, sizeof(name));
220 end = strchr(start, ',');
222 end = str + strlen(str);
225 strncpy(name, start, end - start);
226 for (pt = perm_types; pt->name; pt++) {
227 if (!strcasecmp(name, pt->name)) {
234 for (pt = noperm_types; pt->name; pt++) {
235 if (!strcasecmp(name, pt->name)) {
242 printf("unkown type: %s\n", name);
252 int parse_perm_line(struct identity_downcall_data *data, char *line)
254 char uid_str[256], nid_str[256], perm_str[256];
259 if (data->idd_nperms >= N_PERMS_MAX) {
260 errlog("permission count %d > max %d\n",
261 data->idd_nperms, N_PERMS_MAX);
265 rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
267 errlog("can't parse line %s\n", line);
271 if (!match_uid(data->idd_uid, uid_str))
274 if (!strcmp(nid_str, "*")) {
277 nid = libcfs_str2nid(nid_str);
278 if (nid == LNET_NID_ANY) {
279 errlog("can't parse nid %s\n", nid_str);
284 if (parse_perm(&perm, &noperm, perm_str)) {
285 errlog("invalid perm %s\n", perm_str);
289 /* merge the perms with the same nid.
291 * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
292 * it must be data->idd_perms[0].pdd_nid, and act as default perm.
294 if (nid != LNET_NID_ANY) {
297 /* search for the same nid */
298 for (i = data->idd_nperms - 1; i >= 0; i--) {
299 if (data->idd_perms[i].pdd_nid == nid) {
300 data->idd_perms[i].pdd_perm =
301 (data->idd_perms[i].pdd_perm | perm) &
308 /* NOT found, add to tail */
310 data->idd_perms[data->idd_nperms].pdd_nid = nid;
311 data->idd_perms[data->idd_nperms].pdd_perm =
316 if (data->idd_nperms > 0) {
317 /* the first one isn't LNET_NID_ANY, need exchange */
318 if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
319 data->idd_perms[data->idd_nperms].pdd_nid =
320 data->idd_perms[0].pdd_nid;
321 data->idd_perms[data->idd_nperms].pdd_perm =
322 data->idd_perms[0].pdd_perm;
323 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
324 data->idd_perms[0].pdd_perm = perm & ~noperm;
327 /* only fix LNET_NID_ANY item */
328 data->idd_perms[0].pdd_perm =
329 (data->idd_perms[0].pdd_perm | perm) &
333 /* it is the first one, only add to head */
334 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
335 data->idd_perms[0].pdd_perm = perm & ~noperm;
336 data->idd_nperms = 1;
343 int get_perms(FILE *fp, struct identity_downcall_data *data)
347 while (fgets(line, 1024, fp)) {
348 if (comment_line(line))
351 if (parse_perm_line(data, line)) {
352 errlog("parse line %s failed!\n", line);
360 static void show_result(struct identity_downcall_data *data)
365 errlog("failed to get identity for uid %d: %s\n",
366 data->idd_uid, strerror(data->idd_err));
370 printf("uid=%d gid=", data->idd_uid);
371 for (i = 0; i < data->idd_ngroups; i++)
372 printf("%s%u", i > 0 ? "," : "", data->idd_groups[i]);
374 printf("permissions:\n"
376 for (i = 0; i < data->idd_nperms; i++) {
377 struct perm_downcall_data *pdd;
379 pdd = &data->idd_perms[i];
381 printf(" "LPX64"\t0x%x\n", pdd->pdd_nid, pdd->pdd_perm);
386 int main(int argc, char **argv)
390 struct identity_downcall_data *data;
395 progname = basename(argv[0]);
402 uid = strtoul(argv[2], &end, 0);
404 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
409 data = malloc(sizeof(*data));
411 errlog("malloc identity downcall data(%d) failed!\n",
415 memset(data, 0, sizeof(*data));
416 data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
419 /* get groups for uid */
420 rc = get_groups_local(data);
424 /* read permission database */
425 perms_fp = fopen(PERM_PATHNAME, "r");
427 get_perms(perms_fp, data);
429 } else if (errno != ENOENT) {
430 errlog("open %s failed: %s\n",
431 PERM_PATHNAME, strerror(errno));
435 if (getenv("L_GETIDENTITY_TEST")) {
440 snprintf(procname, sizeof(procname),
441 "/proc/fs/lustre/mdt/%s/identity_info", argv[1]);
442 fd = open(procname, O_WRONLY);
444 errlog("can't open file %s: %s\n", procname, strerror(errno));
448 rc = write(fd, data, sizeof(*data));
450 if (rc != sizeof(*data)) {
451 errlog("partial write ret %d: %s\n", rc, strerror(errno));