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.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
53 #include <liblustre.h>
54 #include <lustre/lustre_user.h>
55 #include <lustre/lustre_idl.h>
57 #define PERM_PATHNAME "/etc/lustre/perm.conf"
60 * permission file format is like this:
63 * '*' nid means any nid
64 * '*' uid means any uid
65 * the valid values for perms are:
66 * setuid/setgid/setgrp/rmtacl -- enable corresponding perm
67 * nosetuid/nosetgid/nosetgrp/normtacl -- disable corresponding perm
68 * they can be listed together, seperated by ',',
69 * when perm and noperm are in the same line (item), noperm is preferential,
70 * when they are in different lines (items), the latter is preferential,
71 * '*' nid is as default perm, and is not preferential.
74 static char *progname;
76 static void usage(void)
79 "\nusage: %s {mdtname} {uid}\n"
80 "Normally invoked as an upcall from Lustre, set via:\n"
81 "/proc/fs/lustre/mdt/${mdtname}/identity_upcall\n",
85 static int compare_u32(const void *v1, const void *v2)
87 return (*(__u32 *)v1 - *(__u32 *)v2);
90 static void errlog(const char *fmt, ...)
94 openlog(progname, LOG_PERROR | LOG_PID, LOG_AUTHPRIV);
97 vsyslog(LOG_NOTICE, fmt, args);
103 int get_groups_local(struct identity_downcall_data *data,
104 unsigned int maxgroups)
107 unsigned int ngroups = 0;
114 pw = getpwuid(data->idd_uid);
116 errlog("no such user %u\n", data->idd_uid);
117 data->idd_err = errno ? errno : EIDRM;
121 data->idd_gid = pw->pw_gid;
122 namelen = sysconf(_SC_LOGIN_NAME_MAX);
123 if (namelen < _POSIX_LOGIN_NAME_MAX)
124 namelen = _POSIX_LOGIN_NAME_MAX;
126 pw_name = (char *)malloc(namelen);
128 errlog("malloc error\n");
129 data->idd_err = errno;
133 memset(pw_name, 0, namelen);
134 strncpy(pw_name, pw->pw_name, namelen - 1);
135 groups = data->idd_groups;
137 while ((gr = getgrent()) && ngroups < maxgroups) {
138 if (gr->gr_gid == groups[0])
142 for (i = 0; gr->gr_mem[i]; i++) {
143 if (!strcmp(gr->gr_mem[i], pw_name)) {
144 groups[ngroups++] = gr->gr_gid;
151 qsort(groups, ngroups, sizeof(*groups), compare_u32);
152 data->idd_ngroups = ngroups;
158 static inline int comment_line(char *line)
162 while (*p && (*p == ' ' || *p == '\t')) p++;
164 if (!*p || *p == '\n' || *p == '#')
169 static inline int match_uid(uid_t uid, const char *str)
174 if(!strcmp(str, "*"))
177 uid2 = strtoul(str, &end, 0);
180 return (uid == uid2);
188 static perm_type_t perm_types[] = {
189 { "setuid", CFS_SETUID_PERM },
190 { "setgid", CFS_SETGID_PERM },
191 { "setgrp", CFS_SETGRP_PERM },
192 { "rmtacl", CFS_RMTACL_PERM },
193 { "rmtown", CFS_RMTOWN_PERM },
197 static perm_type_t noperm_types[] = {
198 { "nosetuid", CFS_SETUID_PERM },
199 { "nosetgid", CFS_SETGID_PERM },
200 { "nosetgrp", CFS_SETGRP_PERM },
201 { "normtacl", CFS_RMTACL_PERM },
202 { "normtown", CFS_RMTOWN_PERM },
206 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
216 memset(name, 0, sizeof(name));
217 end = strchr(start, ',');
219 end = str + strlen(str);
222 strncpy(name, start, end - start);
223 for (pt = perm_types; pt->name; pt++) {
224 if (!strcasecmp(name, pt->name)) {
231 for (pt = noperm_types; pt->name; pt++) {
232 if (!strcasecmp(name, pt->name)) {
239 printf("unkown type: %s\n", name);
249 int parse_perm_line(struct identity_downcall_data *data, char *line)
251 char uid_str[256], nid_str[256], perm_str[256];
256 if (data->idd_nperms >= N_PERMS_MAX) {
257 errlog("permission count %d > max %d\n",
258 data->idd_nperms, N_PERMS_MAX);
262 rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
264 errlog("can't parse line %s\n", line);
268 if (!match_uid(data->idd_uid, uid_str))
271 if (!strcmp(nid_str, "*")) {
274 nid = libcfs_str2nid(nid_str);
275 if (nid == LNET_NID_ANY) {
276 errlog("can't parse nid %s\n", nid_str);
281 if (parse_perm(&perm, &noperm, perm_str)) {
282 errlog("invalid perm %s\n", perm_str);
286 /* merge the perms with the same nid.
288 * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
289 * it must be data->idd_perms[0].pdd_nid, and act as default perm.
291 if (nid != LNET_NID_ANY) {
294 /* search for the same nid */
295 for (i = data->idd_nperms - 1; i >= 0; i--) {
296 if (data->idd_perms[i].pdd_nid == nid) {
297 data->idd_perms[i].pdd_perm =
298 (data->idd_perms[i].pdd_perm | perm) &
305 /* NOT found, add to tail */
307 data->idd_perms[data->idd_nperms].pdd_nid = nid;
308 data->idd_perms[data->idd_nperms].pdd_perm =
313 if (data->idd_nperms > 0) {
314 /* the first one isn't LNET_NID_ANY, need exchange */
315 if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
316 data->idd_perms[data->idd_nperms].pdd_nid =
317 data->idd_perms[0].pdd_nid;
318 data->idd_perms[data->idd_nperms].pdd_perm =
319 data->idd_perms[0].pdd_perm;
320 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
321 data->idd_perms[0].pdd_perm = perm & ~noperm;
324 /* only fix LNET_NID_ANY item */
325 data->idd_perms[0].pdd_perm =
326 (data->idd_perms[0].pdd_perm | perm) &
330 /* it is the first one, only add to head */
331 data->idd_perms[0].pdd_nid = LNET_NID_ANY;
332 data->idd_perms[0].pdd_perm = perm & ~noperm;
333 data->idd_nperms = 1;
340 int get_perms(struct identity_downcall_data *data)
345 fp = fopen(PERM_PATHNAME, "r");
347 if (errno == ENOENT) {
350 errlog("open %s failed: %s\n",
351 PERM_PATHNAME, strerror(errno));
352 data->idd_err = errno;
357 while (fgets(line, 1024, fp)) {
358 if (comment_line(line))
361 if (parse_perm_line(data, line)) {
362 errlog("parse line %s failed!\n", line);
363 data->idd_err = EINVAL;
373 static void show_result(struct identity_downcall_data *data)
378 errlog("failed to get identity for uid %d: %s\n",
379 data->idd_uid, strerror(data->idd_err));
383 printf("uid=%d gid=%d", data->idd_uid, data->idd_gid);
384 for (i = 0; i < data->idd_ngroups; i++)
385 printf(",%u", data->idd_groups[i]);
387 printf("permissions:\n"
389 for (i = 0; i < data->idd_nperms; i++) {
390 struct perm_downcall_data *pdd;
392 pdd = &data->idd_perms[i];
394 printf(" "LPX64"\t0x%x\n", pdd->pdd_nid, pdd->pdd_perm);
399 int main(int argc, char **argv)
402 struct identity_downcall_data *data = NULL;
405 int fd, rc = -EINVAL, size, maxgroups;
407 progname = basename(argv[0]);
413 uid = strtoul(argv[2], &end, 0);
415 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
419 maxgroups = sysconf(_SC_NGROUPS_MAX);
420 if (maxgroups > NGROUPS_MAX)
421 maxgroups = NGROUPS_MAX;
423 size = offsetof(struct identity_downcall_data, idd_groups[maxgroups]);
426 errlog("malloc identity downcall data(%d) failed!\n", size);
431 memset(data, 0, size);
432 data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
434 /* get groups for uid */
435 rc = get_groups_local(data, maxgroups);
439 size = offsetof(struct identity_downcall_data,
440 idd_groups[data->idd_ngroups]);
441 /* read permission database */
442 rc = get_perms(data);
445 if (getenv("L_GETIDENTITY_TEST")) {
451 snprintf(procname, sizeof(procname),
452 "/proc/fs/lustre/mdt/%s/identity_info", argv[1]);
453 fd = open(procname, O_WRONLY);
455 errlog("can't open file %s: %s\n", procname, strerror(errno));
460 rc = write(fd, data, size);
463 errlog("partial write ret %d: %s\n", rc, strerror(errno));