1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004-2006 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 #include <liblustre.h>
38 #include <lustre/lustre_user.h>
39 #include <lustre/lustre_idl.h>
40 #include <libcfs/kp30.h>
42 #define PERM_PATHNAME "/etc/lustre/perm.conf"
45 * permission file format is like this:
48 * '*' nid means any nid
49 * '*' uid means any uid
50 * the valid values for perms are:
51 * setuid/setgid/setgrp/rmtacl -- enable corresponding perm
52 * nosetuid/nosetgid/nosetgrp/normtacl -- disable corresponding perm
53 * they can be listed together, seperated by ',',
54 * when perm and noperm are in the same line (item), noperm is preferential,
55 * when they are in different lines (items), the latter is preferential,
56 * '*' nid is as default perm, and is not preferential.
59 static char *progname;
61 static void usage(void)
64 "\nusage: %s {mdtname} {uid}\n"
65 "Normally invoked as an upcall from Lustre, set via:\n"
66 " /proc/fs/lustre/mdt/{mdtname}/identity_upcall\n",
70 static int compare_u32(const void *v1, const void *v2)
72 return (*(__u32 *)v1 - *(__u32 *)v2);
75 static void errlog(const char *fmt, ...)
79 openlog(progname, LOG_PERROR, LOG_AUTHPRIV);
82 vsyslog(LOG_NOTICE, fmt, args);
83 fprintf(stderr, fmt, args);
89 int get_groups_local(struct identity_downcall_data *data)
93 unsigned int ngroups = 0;
100 pw = getpwuid(data->idd_uid);
102 errlog("no such user %u\n", data->idd_uid);
103 data->idd_err = errno ? errno : EIDRM;
106 data->idd_gid = pw->pw_gid;
108 namelen = sysconf(_SC_LOGIN_NAME_MAX);
109 if (namelen < _POSIX_LOGIN_NAME_MAX)
110 namelen = _POSIX_LOGIN_NAME_MAX;
111 pw_name = (char *)malloc(namelen);
113 errlog("malloc error\n");
114 data->idd_err = errno;
117 memset(pw_name, 0, namelen);
118 strncpy(pw_name, pw->pw_name, namelen - 1);
120 maxgroups = sysconf(_SC_NGROUPS_MAX);
121 if (maxgroups > NGROUPS_MAX)
122 maxgroups = NGROUPS_MAX;
123 groups = data->idd_groups;
125 groups[ngroups++] = pw->pw_gid;
126 while ((gr = getgrent())) {
127 if (gr->gr_gid == groups[0])
131 for (i = 0; gr->gr_mem[i]; i++) {
132 if (!strcmp(gr->gr_mem[i], pw_name)) {
133 groups[ngroups++] = gr->gr_gid;
137 if (ngroups == maxgroups)
141 qsort(groups, ngroups, sizeof(*groups), compare_u32);
142 data->idd_ngroups = ngroups;
148 static inline int comment_line(char *line)
152 while (*p && (*p == ' ' || *p == '\t')) p++;
154 if (!*p || *p == '\n' || *p == '#')
159 static inline int match_uid(uid_t uid, const char *str)
164 if(!strcmp(str, "*"))
167 uid2 = strtoul(str, &end, 0);
171 return (uid == uid2);
179 static perm_type_t perm_types[] = {
180 { "setuid", CFS_SETUID_PERM },
181 { "setgid", CFS_SETGID_PERM },
182 { "setgrp", CFS_SETGRP_PERM },
183 { "rmtacl", CFS_RMTACL_PERM },
187 static perm_type_t noperm_types[] = {
188 { "nosetuid", CFS_SETUID_PERM },
189 { "nosetgid", CFS_SETGID_PERM },
190 { "nosetgrp", CFS_SETGRP_PERM },
191 { "normtacl", CFS_RMTACL_PERM },
195 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
205 memset(name, 0, sizeof(name));
206 end = strchr(start, ',');
208 end = str + strlen(str);
211 strncpy(name, start, end - start);
212 for (pt = perm_types; pt->name; pt++) {
213 if (!strcasecmp(name, pt->name)) {
220 for (pt = noperm_types; pt->name; pt++) {
221 if (!strcasecmp(name, pt->name)) {
228 printf("unkown type: %s\n", name);
238 int parse_perm_line(struct identity_downcall_data *data, char *line)
240 char uid_str[256], nid_str[256], perm_str[256];
245 if (data->idd_nperms >= N_PERMS_MAX) {
246 errlog("permission count %d > max %d\n",
247 data->idd_nperms, N_PERMS_MAX);
251 rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
253 errlog("can't parse line %s\n", line);
257 if (!match_uid(data->idd_uid, uid_str))
260 if (!strcmp(nid_str, "*")) {
263 nid = libcfs_str2nid(nid_str);
264 if (nid == LNET_NID_ANY) {
265 errlog("can't parse nid %s\n", nid_str);
270 if (parse_perm(&perm, &noperm, perm_str)) {
271 errlog("invalid perm %s\n", perm_str);
275 /* merge the perms with the same nid.
277 * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
278 * it must be data->idd_perms[0].pdd_nid, and act as default perm.
280 if (nid != LNET_NID_ANY) {
283 /* search for the same nid */
284 for (i = data->idd_nperms - 1; i >= 0; i--) {
285 if (data->idd_perms[i].pdd_nid == nid) {
286 data->idd_perms[i].pdd_perm =
287 (data->idd_perms[i].pdd_perm | perm) &
294 /* NOT found, add to tail */
296 data->idd_perms[data->idd_nperms].pdd_nid = nid;
297 data->idd_perms[data->idd_nperms].pdd_perm =
302 if (data->idd_nperms > 0) {
303 /* the first one isn't LNET_NID_ANY, need exchange */
304 if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
305 data->idd_perms[data->idd_nperms].pdd_nid =
306 data->idd_perms[0].pdd_nid;
307 data->idd_perms[data->idd_nperms].pdd_perm =
308 data->idd_perms[0].pdd_perm;
309 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
310 data->idd_perms[0].pdd_perm = perm & ~noperm;
313 /* only fix LNET_NID_ANY item */
314 data->idd_perms[0].pdd_perm =
315 (data->idd_perms[0].pdd_perm | perm) &
319 /* it is the first one, only add to head */
320 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
321 data->idd_perms[0].pdd_perm = perm & ~noperm;
322 data->idd_nperms = 1;
329 int get_perms(FILE *fp, struct identity_downcall_data *data)
333 while (fgets(line, 1024, fp)) {
334 if (comment_line(line))
337 if (parse_perm_line(data, line)) {
338 errlog("parse line %s failed!\n", line);
346 static void show_result(struct identity_downcall_data *data)
351 errlog("failed to get identity for uid %d: %s\n",
352 data->idd_uid, strerror(data->idd_err));
356 printf("uid=%d gid=", data->idd_uid);
357 for (i = 0; i < data->idd_ngroups; i++)
358 printf("%s%u", i > 0 ? "," : "", data->idd_groups[i]);
360 printf("permissions:\n"
362 for (i = 0; i < data->idd_nperms; i++) {
363 struct perm_downcall_data *pdd;
365 pdd = &data->idd_perms[i];
367 printf(" %#llx\t0x%x\n", pdd->pdd_nid, pdd->pdd_perm);
372 int main(int argc, char **argv)
376 struct identity_downcall_data *data;
381 progname = basename(argv[0]);
388 uid = strtoul(argv[2], &end, 0);
390 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
395 data = malloc(sizeof(*data));
397 errlog("malloc identity downcall data(%d) failed!\n",
401 memset(data, 0, sizeof(*data));
402 data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
405 /* get groups for uid */
406 rc = get_groups_local(data);
410 /* read permission database */
411 perms_fp = fopen(PERM_PATHNAME, "r");
413 get_perms(perms_fp, data);
415 } else if (errno != ENOENT) {
416 errlog("open %s failed: %s\n",
417 PERM_PATHNAME, strerror(errno));
421 if (getenv("L_GETIDENTITY_TEST")) {
426 snprintf(procname, sizeof(procname),
427 "/proc/fs/lustre/mdt/%s/identity_info", argv[1]);
428 fd = open(procname, O_WRONLY);
430 errlog("can't open file %s: %s\n", procname, strerror(errno));
434 rc = write(fd, data, sizeof(*data));
436 if (rc != sizeof(*data)) {
437 errlog("partial write ret %d: %s\n", rc, strerror(errno));