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 2008 Sun Microsystems, Inc. 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_AUTHPRIV);
95 vsyslog(LOG_NOTICE, fmt, args);
96 fprintf(stderr, fmt, args);
102 int get_groups_local(struct identity_downcall_data *data)
106 unsigned int ngroups = 0;
113 pw = getpwuid(data->idd_uid);
115 errlog("no such user %u\n", data->idd_uid);
116 data->idd_err = errno ? errno : EIDRM;
119 data->idd_gid = pw->pw_gid;
121 namelen = sysconf(_SC_LOGIN_NAME_MAX);
122 if (namelen < _POSIX_LOGIN_NAME_MAX)
123 namelen = _POSIX_LOGIN_NAME_MAX;
124 pw_name = (char *)malloc(namelen);
126 errlog("malloc error\n");
127 data->idd_err = errno;
130 memset(pw_name, 0, namelen);
131 strncpy(pw_name, pw->pw_name, namelen - 1);
133 maxgroups = sysconf(_SC_NGROUPS_MAX);
134 if (maxgroups > NGROUPS_MAX)
135 maxgroups = NGROUPS_MAX;
136 groups = data->idd_groups;
138 groups[ngroups++] = pw->pw_gid;
139 while ((gr = getgrent())) {
140 if (gr->gr_gid == groups[0])
144 for (i = 0; gr->gr_mem[i]; i++) {
145 if (!strcmp(gr->gr_mem[i], pw_name)) {
146 groups[ngroups++] = gr->gr_gid;
150 if (ngroups == maxgroups)
154 qsort(groups, ngroups, sizeof(*groups), compare_u32);
155 data->idd_ngroups = ngroups;
161 static inline int comment_line(char *line)
165 while (*p && (*p == ' ' || *p == '\t')) p++;
167 if (!*p || *p == '\n' || *p == '#')
172 static inline int match_uid(uid_t uid, const char *str)
177 if(!strcmp(str, "*"))
180 uid2 = strtoul(str, &end, 0);
184 return (uid == uid2);
192 static perm_type_t perm_types[] = {
193 { "setuid", CFS_SETUID_PERM },
194 { "setgid", CFS_SETGID_PERM },
195 { "setgrp", CFS_SETGRP_PERM },
196 { "rmtacl", CFS_RMTACL_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 },
208 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
218 memset(name, 0, sizeof(name));
219 end = strchr(start, ',');
221 end = str + strlen(str);
224 strncpy(name, start, end - start);
225 for (pt = perm_types; pt->name; pt++) {
226 if (!strcasecmp(name, pt->name)) {
233 for (pt = noperm_types; pt->name; pt++) {
234 if (!strcasecmp(name, pt->name)) {
241 printf("unkown type: %s\n", name);
251 int parse_perm_line(struct identity_downcall_data *data, char *line)
253 char uid_str[256], nid_str[256], perm_str[256];
258 if (data->idd_nperms >= N_PERMS_MAX) {
259 errlog("permission count %d > max %d\n",
260 data->idd_nperms, N_PERMS_MAX);
264 rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
266 errlog("can't parse line %s\n", line);
270 if (!match_uid(data->idd_uid, uid_str))
273 if (!strcmp(nid_str, "*")) {
276 nid = libcfs_str2nid(nid_str);
277 if (nid == LNET_NID_ANY) {
278 errlog("can't parse nid %s\n", nid_str);
283 if (parse_perm(&perm, &noperm, perm_str)) {
284 errlog("invalid perm %s\n", perm_str);
288 /* merge the perms with the same nid.
290 * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
291 * it must be data->idd_perms[0].pdd_nid, and act as default perm.
293 if (nid != LNET_NID_ANY) {
296 /* search for the same nid */
297 for (i = data->idd_nperms - 1; i >= 0; i--) {
298 if (data->idd_perms[i].pdd_nid == nid) {
299 data->idd_perms[i].pdd_perm =
300 (data->idd_perms[i].pdd_perm | perm) &
307 /* NOT found, add to tail */
309 data->idd_perms[data->idd_nperms].pdd_nid = nid;
310 data->idd_perms[data->idd_nperms].pdd_perm =
315 if (data->idd_nperms > 0) {
316 /* the first one isn't LNET_NID_ANY, need exchange */
317 if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
318 data->idd_perms[data->idd_nperms].pdd_nid =
319 data->idd_perms[0].pdd_nid;
320 data->idd_perms[data->idd_nperms].pdd_perm =
321 data->idd_perms[0].pdd_perm;
322 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
323 data->idd_perms[0].pdd_perm = perm & ~noperm;
326 /* only fix LNET_NID_ANY item */
327 data->idd_perms[0].pdd_perm =
328 (data->idd_perms[0].pdd_perm | perm) &
332 /* it is the first one, only add to head */
333 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
334 data->idd_perms[0].pdd_perm = perm & ~noperm;
335 data->idd_nperms = 1;
342 int get_perms(FILE *fp, struct identity_downcall_data *data)
346 while (fgets(line, 1024, fp)) {
347 if (comment_line(line))
350 if (parse_perm_line(data, line)) {
351 errlog("parse line %s failed!\n", line);
359 static void show_result(struct identity_downcall_data *data)
364 errlog("failed to get identity for uid %d: %s\n",
365 data->idd_uid, strerror(data->idd_err));
369 printf("uid=%d gid=", data->idd_uid);
370 for (i = 0; i < data->idd_ngroups; i++)
371 printf("%s%u", i > 0 ? "," : "", data->idd_groups[i]);
373 printf("permissions:\n"
375 for (i = 0; i < data->idd_nperms; i++) {
376 struct perm_downcall_data *pdd;
378 pdd = &data->idd_perms[i];
380 printf(" "LPX64"\t0x%x\n", pdd->pdd_nid, pdd->pdd_perm);
385 int main(int argc, char **argv)
389 struct identity_downcall_data *data;
394 progname = basename(argv[0]);
401 uid = strtoul(argv[2], &end, 0);
403 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
408 data = malloc(sizeof(*data));
410 errlog("malloc identity downcall data(%d) failed!\n",
414 memset(data, 0, sizeof(*data));
415 data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
418 /* get groups for uid */
419 rc = get_groups_local(data);
423 /* read permission database */
424 perms_fp = fopen(PERM_PATHNAME, "r");
426 get_perms(perms_fp, data);
428 } else if (errno != ENOENT) {
429 errlog("open %s failed: %s\n",
430 PERM_PATHNAME, strerror(errno));
434 if (getenv("L_GETIDENTITY_TEST")) {
439 snprintf(procname, sizeof(procname),
440 "/proc/fs/lustre/mdt/%s/identity_info", argv[1]);
441 fd = open(procname, O_WRONLY);
443 errlog("can't open file %s: %s\n", procname, strerror(errno));
447 rc = write(fd, data, sizeof(*data));
449 if (rc != sizeof(*data)) {
450 errlog("partial write ret %d: %s\n", rc, strerror(errno));