Whamcloud - gitweb
LU-1581 utils: introduce osd_init() wrapper
[fs/lustre-release.git] / lustre / utils / l_getidentity.c
index 65e9f0b..f384566 100644 (file)
@@ -1,23 +1,37 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
+/*
+ * GPL HEADER START
  *
- *  Copyright (C) 2004-2006 Cluster File Systems, Inc.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   Lustre is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
  *
+ * GPL HEADER END
+ */
+/*
+ * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, Whamcloud, Inc.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
  */
 
 #include <stdlib.h>
@@ -37,7 +51,6 @@
 #include <liblustre.h>
 #include <lustre/lustre_user.h>
 #include <lustre/lustre_idl.h>
-#include <libcfs/kp30.h>
 
 #define PERM_PATHNAME "/etc/lustre/perm.conf"
 
@@ -63,7 +76,7 @@ static void usage(void)
         fprintf(stderr,
                 "\nusage: %s {mdtname} {uid}\n"
                 "Normally invoked as an upcall from Lustre, set via:\n"
-                "  /proc/fs/lustre/mdt/{mdtname}/identity_upcall\n",
+                "/proc/fs/lustre/mdt/${mdtname}/identity_upcall\n",
                 progname);
 }
 
@@ -76,19 +89,18 @@ static void errlog(const char *fmt, ...)
 {
         va_list args;
 
-        openlog(progname, LOG_PERROR, LOG_AUTHPRIV);
+        openlog(progname, LOG_PERROR | LOG_PID, LOG_AUTHPRIV);
 
         va_start(args, fmt);
         vsyslog(LOG_NOTICE, fmt, args);
-        fprintf(stderr, fmt, args);
         va_end(args);
 
         closelog();
 }
 
-int get_groups_local(struct identity_downcall_data *data)
+int get_groups_local(struct identity_downcall_data *data,
+                     unsigned int maxgroups)
 {
-        int maxgroups;
         gid_t *groups;
         unsigned int ngroups = 0;
         struct passwd *pw;
@@ -103,27 +115,24 @@ int get_groups_local(struct identity_downcall_data *data)
                 data->idd_err = errno ? errno : EIDRM;
                 return -1;
         }
-        data->idd_gid = pw->pw_gid;
 
+        data->idd_gid = pw->pw_gid;
         namelen = sysconf(_SC_LOGIN_NAME_MAX);
         if (namelen < _POSIX_LOGIN_NAME_MAX)
                 namelen = _POSIX_LOGIN_NAME_MAX;
+
         pw_name = (char *)malloc(namelen);
         if (!pw_name) {
                 errlog("malloc error\n");
                 data->idd_err = errno;
                 return -1;
         }
+
         memset(pw_name, 0, namelen);
         strncpy(pw_name, pw->pw_name, namelen - 1);
-
-        maxgroups = sysconf(_SC_NGROUPS_MAX);
-        if (maxgroups > NGROUPS_MAX)
-                maxgroups = NGROUPS_MAX;
         groups = data->idd_groups;
 
-        groups[ngroups++] = pw->pw_gid;
-        while ((gr = getgrent())) {
+        while ((gr = getgrent()) && ngroups < maxgroups) {
                 if (gr->gr_gid == groups[0])
                         continue;
                 if (!gr->gr_mem)
@@ -134,11 +143,10 @@ int get_groups_local(struct identity_downcall_data *data)
                                 break;
                         }
                 }
-                if (ngroups == maxgroups)
-                        break;
         }
         endgrent();
-        qsort(groups, ngroups, sizeof(*groups), compare_u32);
+        if (ngroups > 0)
+                qsort(groups, ngroups, sizeof(*groups), compare_u32);
         data->idd_ngroups = ngroups;
 
         free(pw_name);
@@ -167,7 +175,6 @@ static inline int match_uid(uid_t uid, const char *str)
         uid2 = strtoul(str, &end, 0);
         if (*end)
                 return 0;
-
         return (uid == uid2);
 }
 
@@ -181,6 +188,7 @@ static perm_type_t perm_types[] = {
         { "setgid", CFS_SETGID_PERM },
         { "setgrp", CFS_SETGRP_PERM },
         { "rmtacl", CFS_RMTACL_PERM },
+        { "rmtown", CFS_RMTOWN_PERM },
         { 0 }
 };
 
@@ -189,6 +197,7 @@ static perm_type_t noperm_types[] = {
         { "nosetgid", CFS_SETGID_PERM },
         { "nosetgrp", CFS_SETGRP_PERM },
         { "normtacl", CFS_RMTACL_PERM },
+        { "normtown", CFS_RMTOWN_PERM },
         { 0 }
 };
 
@@ -326,20 +335,36 @@ int parse_perm_line(struct identity_downcall_data *data, char *line)
         return 0;
 }
 
-int get_perms(FILE *fp, struct identity_downcall_data *data)
+int get_perms(struct identity_downcall_data *data)
 {
+        FILE *fp;
         char line[1024];
 
+        fp = fopen(PERM_PATHNAME, "r");
+        if (fp == NULL) {
+                if (errno == ENOENT) {
+                        return 0;
+                } else {
+                        errlog("open %s failed: %s\n",
+                               PERM_PATHNAME, strerror(errno));
+                        data->idd_err = errno;
+                        return -1;
+                }
+        }
+
         while (fgets(line, 1024, fp)) {
                 if (comment_line(line))
                         continue;
 
                 if (parse_perm_line(data, line)) {
                         errlog("parse line %s failed!\n", line);
+                        data->idd_err = EINVAL;
+                        fclose(fp);
                         return -1;
                 }
         }
 
+        fclose(fp);
         return 0;
 }
 
@@ -353,9 +378,9 @@ static void show_result(struct identity_downcall_data *data)
                 return;
         }
 
-        printf("uid=%d gid=", data->idd_uid);
+        printf("uid=%d gid=%d", data->idd_uid, data->idd_gid);
         for (i = 0; i < data->idd_ngroups; i++)
-                printf("%s%u", i > 0 ? "," : "", data->idd_groups[i]);
+                printf(",%u", data->idd_groups[i]);
         printf("\n");
         printf("permissions:\n"
                "  nid\t\t\tperm\n");
@@ -364,63 +389,61 @@ static void show_result(struct identity_downcall_data *data)
 
                 pdd = &data->idd_perms[i];
 
-                printf("  %#llx\t0x%x\n", pdd->pdd_nid, pdd->pdd_perm);
+                printf("  "LPX64"\t0x%x\n", pdd->pdd_nid, pdd->pdd_perm);
         }
         printf("\n");
 }
 
 int main(int argc, char **argv)
 {
-        FILE *perms_fp;
         char *end;
-        struct identity_downcall_data *data;
+        struct identity_downcall_data *data = NULL;
         char procname[1024];
         unsigned long uid;
-        int fd, rc;
+        int fd, rc = -EINVAL, size, maxgroups;
 
         progname = basename(argv[0]);
-
         if (argc != 3) {
                 usage();
-                return 1;
+                goto out;
         }
 
         uid = strtoul(argv[2], &end, 0);
         if (*end) {
                 errlog("%s: invalid uid '%s'\n", progname, argv[2]);
-                usage();
-                return 1;
+                goto out;
         }
 
-        data = malloc(sizeof(*data));
+        maxgroups = sysconf(_SC_NGROUPS_MAX);
+        if (maxgroups > NGROUPS_MAX)
+                maxgroups = NGROUPS_MAX;
+
+        size = offsetof(struct identity_downcall_data, idd_groups[maxgroups]);
+        data = malloc(size);
         if (!data) {
-                errlog("malloc identity downcall data(%d) failed!\n",
-                       sizeof(*data));
-                return 1;
+                errlog("malloc identity downcall data(%d) failed!\n", size);
+                rc = -ENOMEM;
+                goto out;
         }
-        memset(data, 0, sizeof(*data));
+
+        memset(data, 0, size);
         data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
         data->idd_uid = uid;
-
         /* get groups for uid */
-        rc = get_groups_local(data);
+        rc = get_groups_local(data, maxgroups);
         if (rc)
                 goto downcall;
 
+        size = offsetof(struct identity_downcall_data,
+                        idd_groups[data->idd_ngroups]);
         /* read permission database */
-        perms_fp = fopen(PERM_PATHNAME, "r");
-        if (perms_fp) {
-                get_perms(perms_fp, data);
-                fclose(perms_fp);
-        } else if (errno != ENOENT) {
-                errlog("open %s failed: %s\n",
-                       PERM_PATHNAME, strerror(errno));
-        }
+        rc = get_perms(data);
 
 downcall:
         if (getenv("L_GETIDENTITY_TEST")) {
                 show_result(data);
-                return 0;
+                rc = 0;
+                goto out;
         }
 
         snprintf(procname, sizeof(procname),
@@ -428,15 +451,21 @@ downcall:
         fd = open(procname, O_WRONLY);
         if (fd < 0) {
                 errlog("can't open file %s: %s\n", procname, strerror(errno));
-                return 1;
+                rc = -1;
+                goto out;
         }
 
-        rc = write(fd, data, sizeof(*data));
+        rc = write(fd, data, size);
         close(fd);
-        if (rc != sizeof(*data)) {
+        if (rc != size) {
                 errlog("partial write ret %d: %s\n", rc, strerror(errno));
-                return 1;
+                rc = -1;
+        } else {
+                rc = 0;
         }
 
-        return 0;
+out:
+        if (data != NULL)
+                free(data);
+        return rc;
 }