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>
41 #define PERM_PATHNAME "/etc/lustre/perm.conf"
44 * permission file format is like this:
47 * '*' nid means any nid
48 * '*' uid means any uid
49 * the valid values for perms are:
50 * setuid/setgid/setgrp/rmtacl -- enable corresponding perm
51 * nosetuid/nosetgid/nosetgrp/normtacl -- disable corresponding perm
52 * they can be listed together, seperated by ',',
53 * when perm and noperm are in the same line (item), noperm is preferential,
54 * when they are in different lines (items), the latter is preferential,
55 * '*' nid is as default perm, and is not preferential.
58 static char *progname;
60 static void usage(void)
63 "\nusage: %s {mdtname} {uid}\n"
64 "Normally invoked as an upcall from Lustre, set via:\n"
65 " /proc/fs/lustre/mdt/{mdtname}/identity_upcall\n",
69 static int compare_u32(const void *v1, const void *v2)
71 return (*(__u32 *)v1 - *(__u32 *)v2);
74 static void errlog(const char *fmt, ...)
78 openlog(progname, LOG_PERROR, LOG_AUTHPRIV);
81 vsyslog(LOG_NOTICE, fmt, args);
82 fprintf(stderr, fmt, args);
88 int get_groups_local(struct identity_downcall_data *data)
92 unsigned int ngroups = 0;
99 pw = getpwuid(data->idd_uid);
101 errlog("no such user %u\n", data->idd_uid);
102 data->idd_err = errno ? errno : EIDRM;
105 data->idd_gid = pw->pw_gid;
107 namelen = sysconf(_SC_LOGIN_NAME_MAX);
108 if (namelen < _POSIX_LOGIN_NAME_MAX)
109 namelen = _POSIX_LOGIN_NAME_MAX;
110 pw_name = (char *)malloc(namelen);
112 errlog("malloc error\n");
113 data->idd_err = errno;
116 memset(pw_name, 0, namelen);
117 strncpy(pw_name, pw->pw_name, namelen - 1);
119 maxgroups = sysconf(_SC_NGROUPS_MAX);
120 if (maxgroups > NGROUPS_MAX)
121 maxgroups = NGROUPS_MAX;
122 groups = data->idd_groups;
124 groups[ngroups++] = pw->pw_gid;
125 while ((gr = getgrent())) {
126 if (gr->gr_gid == groups[0])
130 for (i = 0; gr->gr_mem[i]; i++) {
131 if (!strcmp(gr->gr_mem[i], pw_name)) {
132 groups[ngroups++] = gr->gr_gid;
136 if (ngroups == maxgroups)
140 qsort(groups, ngroups, sizeof(*groups), compare_u32);
141 data->idd_ngroups = ngroups;
147 static inline int comment_line(char *line)
151 while (*p && (*p == ' ' || *p == '\t')) p++;
153 if (!*p || *p == '\n' || *p == '#')
158 static inline int match_uid(uid_t uid, const char *str)
163 if(!strcmp(str, "*"))
166 uid2 = strtoul(str, &end, 0);
170 return (uid == uid2);
178 static perm_type_t perm_types[] = {
179 { "setuid", CFS_SETUID_PERM },
180 { "setgid", CFS_SETGID_PERM },
181 { "setgrp", CFS_SETGRP_PERM },
182 { "rmtacl", CFS_RMTACL_PERM },
186 static perm_type_t noperm_types[] = {
187 { "nosetuid", CFS_SETUID_PERM },
188 { "nosetgid", CFS_SETGID_PERM },
189 { "nosetgrp", CFS_SETGRP_PERM },
190 { "normtacl", CFS_RMTACL_PERM },
194 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
204 memset(name, 0, sizeof(name));
205 end = strchr(start, ',');
207 end = str + strlen(str);
210 strncpy(name, start, end - start);
211 for (pt = perm_types; pt->name; pt++) {
212 if (!strcasecmp(name, pt->name)) {
219 for (pt = noperm_types; pt->name; pt++) {
220 if (!strcasecmp(name, pt->name)) {
227 printf("unkown type: %s\n", name);
237 int parse_perm_line(struct identity_downcall_data *data, char *line)
239 char uid_str[256], nid_str[256], perm_str[256];
244 if (data->idd_nperms >= N_PERMS_MAX) {
245 errlog("permission count %d > max %d\n",
246 data->idd_nperms, N_PERMS_MAX);
250 rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
252 errlog("can't parse line %s\n", line);
256 if (!match_uid(data->idd_uid, uid_str))
259 if (!strcmp(nid_str, "*")) {
262 nid = libcfs_str2nid(nid_str);
263 if (nid == LNET_NID_ANY) {
264 errlog("can't parse nid %s\n", nid_str);
269 if (parse_perm(&perm, &noperm, perm_str)) {
270 errlog("invalid perm %s\n", perm_str);
274 /* merge the perms with the same nid.
276 * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
277 * it must be data->idd_perms[0].pdd_nid, and act as default perm.
279 if (nid != LNET_NID_ANY) {
282 /* search for the same nid */
283 for (i = data->idd_nperms - 1; i >= 0; i--) {
284 if (data->idd_perms[i].pdd_nid == nid) {
285 data->idd_perms[i].pdd_perm =
286 (data->idd_perms[i].pdd_perm | perm) &
293 /* NOT found, add to tail */
295 data->idd_perms[data->idd_nperms].pdd_nid = nid;
296 data->idd_perms[data->idd_nperms].pdd_perm =
301 if (data->idd_nperms > 0) {
302 /* the first one isn't LNET_NID_ANY, need exchange */
303 if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
304 data->idd_perms[data->idd_nperms].pdd_nid =
305 data->idd_perms[0].pdd_nid;
306 data->idd_perms[data->idd_nperms].pdd_perm =
307 data->idd_perms[0].pdd_perm;
308 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
309 data->idd_perms[0].pdd_perm = perm & ~noperm;
312 /* only fix LNET_NID_ANY item */
313 data->idd_perms[0].pdd_perm =
314 (data->idd_perms[0].pdd_perm | perm) &
318 /* it is the first one, only add to head */
319 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
320 data->idd_perms[0].pdd_perm = perm & ~noperm;
321 data->idd_nperms = 1;
328 int get_perms(FILE *fp, struct identity_downcall_data *data)
332 while (fgets(line, 1024, fp)) {
333 if (comment_line(line))
336 if (parse_perm_line(data, line)) {
337 errlog("parse line %s failed!\n", line);
345 static void show_result(struct identity_downcall_data *data)
350 errlog("failed to get identity for uid %d: %s\n",
351 data->idd_uid, strerror(data->idd_err));
355 printf("uid=%d gid=", data->idd_uid);
356 for (i = 0; i < data->idd_ngroups; i++)
357 printf("%s%u", i > 0 ? "," : "", data->idd_groups[i]);
359 printf("permissions:\n"
361 for (i = 0; i < data->idd_nperms; i++) {
362 struct perm_downcall_data *pdd;
364 pdd = &data->idd_perms[i];
366 printf(" %#llx\t0x%x\n", pdd->pdd_nid, pdd->pdd_perm);
371 int main(int argc, char **argv)
375 struct identity_downcall_data *data;
380 progname = basename(argv[0]);
387 uid = strtoul(argv[2], &end, 0);
389 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
394 data = malloc(sizeof(*data));
396 errlog("malloc identity downcall data(%d) failed!\n",
400 memset(data, 0, sizeof(*data));
401 data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
404 /* get groups for uid */
405 rc = get_groups_local(data);
409 /* read permission database */
410 perms_fp = fopen(PERM_PATHNAME, "r");
412 get_perms(perms_fp, data);
414 } else if (errno != ENOENT) {
415 errlog("open %s failed: %s\n",
416 PERM_PATHNAME, strerror(errno));
420 if (getenv("L_GETIDENTITY_TEST")) {
425 snprintf(procname, sizeof(procname),
426 "/proc/fs/lustre/mdt/%s/identity_info", argv[1]);
427 fd = open(procname, O_WRONLY);
429 errlog("can't open file %s: %s\n", procname, strerror(errno));
433 rc = write(fd, data, sizeof(*data));
435 if (rc != sizeof(*data)) {
436 errlog("partial write ret %d: %s\n", rc, strerror(errno));