Whamcloud - gitweb
LU-5030 utils: migrate most lustre utils to use cfs_get_paths()
[fs/lustre-release.git] / lustre / utils / l_getidentity.c
index 936f494..10842a2 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, Intel Corporation.
+ * Copyright (c) 2011, 2014, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -48,7 +48,9 @@
 #include <libgen.h>
 #include <syslog.h>
 
-#include <liblustre.h>
+#include <libcfs/util/param.h>
+#include <libcfs/util/string.h>
+#include <lnet/nidstr.h>
 #include <lustre/lustre_user.h>
 #include <lustre/lustre_idl.h>
 
@@ -63,7 +65,7 @@
  * the valid values for perms are:
  * setuid/setgid/setgrp/rmtacl           -- enable corresponding perm
  * nosetuid/nosetgid/nosetgrp/normtacl   -- disable corresponding perm
- * they can be listed together, seperated by ',',
+ * they can be listed together, separated by ',',
  * when perm and noperm are in the same line (item), noperm is preferential,
  * when they are in different lines (items), the latter is preferential,
  * '*' nid is as default perm, and is not preferential.
@@ -73,11 +75,11 @@ static char *progname;
 
 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",
-                progname);
+       fprintf(stderr,
+               "\nusage: %s {mdtname} {uid}\n"
+               "Normally invoked as an upcall from Lustre, set via:\n"
+               "lctl set_param mdt.${mdtname}.identity_upcall={path to upcall}\n",
+               progname);
 }
 
 static int compare_u32(const void *v1, const void *v2)
@@ -128,8 +130,7 @@ int get_groups_local(struct identity_downcall_data *data,
                return -1;
        }
 
-       memset(pw_name, 0, namelen);
-       strncpy(pw_name, pw->pw_name, namelen - 1);
+       strlcpy(pw_name, pw->pw_name, namelen);
        groups = data->idd_groups;
 
        /* Allocate array of size maxgroups instead of handling two
@@ -137,7 +138,8 @@ int get_groups_local(struct identity_downcall_data *data,
        groups_tmp = malloc(maxgroups * sizeof(gid_t));
        if (groups_tmp == NULL) {
                free(pw_name);
-               errlog("malloc error\n");
+               data->idd_err = errno ? errno : ENOMEM;
+               errlog("malloc error=%u\n",data->idd_err);
                return -1;
        }
 
@@ -146,7 +148,9 @@ int get_groups_local(struct identity_downcall_data *data,
            0) {
                free(pw_name);
                free(groups_tmp);
-               errlog("getgrouplist() error\n");
+               data->idd_err = errno ? errno : EIDRM;
+               errlog("getgrouplist() error for uid %u: error=%u\n",
+                       data->idd_uid, data->idd_err);
                return -1;
        }
 
@@ -222,13 +226,18 @@ int parse_perm(__u32 *perm, __u32 *noperm, char *str)
         *noperm = 0;
         start = str;
         while (1) {
-                memset(name, 0, sizeof(name));
-                end = strchr(start, ',');
-                if (!end)
-                        end = str + strlen(str);
-                if (start >= end)
-                        break;
-                strncpy(name, start, end - start);
+               size_t len;
+               memset(name, 0, sizeof(name));
+               end = strchr(start, ',');
+               if (end == NULL)
+                       end = str + strlen(str);
+               if (start >= end)
+                       break;
+               len = end - start;
+               if (len >= sizeof(name))
+                       return -E2BIG;
+               strncpy(name, start, len);
+               name[len] = '\0';
                 for (pt = perm_types; pt->name; pt++) {
                         if (!strcasecmp(name, pt->name)) {
                                 *perm |= pt->bit;
@@ -255,9 +264,12 @@ int parse_perm(__u32 *perm, __u32 *noperm, char *str)
         return 0;
 }
 
-int parse_perm_line(struct identity_downcall_data *data, char *line)
+static int
+parse_perm_line(struct identity_downcall_data *data, char *line, size_t size)
 {
-        char uid_str[256], nid_str[256], perm_str[256];
+       char uid_str[size];
+       char nid_str[size];
+       char perm_str[size];
         lnet_nid_t nid;
         __u32 perm, noperm;
         int rc, i;
@@ -268,7 +280,7 @@ int parse_perm_line(struct identity_downcall_data *data, char *line)
                 return -1;
         }
 
-        rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
+       rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
         if (rc != 3) {
                 errlog("can't parse line %s\n", line);
                 return -1;
@@ -348,8 +360,8 @@ int parse_perm_line(struct identity_downcall_data *data, char *line)
 
 int get_perms(struct identity_downcall_data *data)
 {
-        FILE *fp;
-        char line[1024];
+       FILE *fp;
+       char line[PATH_MAX];
 
         fp = fopen(PERM_PATHNAME, "r");
         if (fp == NULL) {
@@ -363,11 +375,11 @@ int get_perms(struct identity_downcall_data *data)
                 }
         }
 
-        while (fgets(line, 1024, fp)) {
+       while (fgets(line, sizeof(line), fp)) {
                 if (comment_line(line))
                         continue;
 
-                if (parse_perm_line(data, line)) {
+               if (parse_perm_line(data, line, sizeof(line))) {
                         errlog("parse line %s failed!\n", line);
                         data->idd_err = EINVAL;
                         fclose(fp);
@@ -407,11 +419,11 @@ static void show_result(struct identity_downcall_data *data)
 
 int main(int argc, char **argv)
 {
-        char *end;
-        struct identity_downcall_data *data = NULL;
-        char procname[1024];
-        unsigned long uid;
-        int fd, rc = -EINVAL, size, maxgroups;
+       char *end;
+       struct identity_downcall_data *data = NULL;
+       glob_t path;
+       unsigned long uid;
+       int fd, rc = -EINVAL, size, maxgroups;
 
         progname = basename(argv[0]);
         if (argc != 3) {
@@ -461,26 +473,33 @@ downcall:
                 goto out;
         }
 
-        snprintf(procname, sizeof(procname),
-                 "/proc/fs/lustre/mdt/%s/identity_info", argv[1]);
-        fd = open(procname, O_WRONLY);
-        if (fd < 0) {
-                errlog("can't open file %s: %s\n", procname, strerror(errno));
-                rc = -1;
-                goto out;
-        }
+       rc = cfs_get_param_paths(&path, "mdt/%s/identity_info", argv[1]);
+       if (rc != 0) {
+               rc = -errno;
+               goto out;
+       }
 
-        rc = write(fd, data, size);
-        close(fd);
-        if (rc != size) {
-                errlog("partial write ret %d: %s\n", rc, strerror(errno));
-                rc = -1;
-        } else {
-                rc = 0;
-        }
+       fd = open(path.gl_pathv[0], O_WRONLY);
+       if (fd < 0) {
+               errlog("can't open file '%s':%s\n", path.gl_pathv[0],
+                      strerror(errno));
+               rc = -errno;
+               goto out_params;
+       }
+
+       rc = write(fd, data, size);
+       close(fd);
+       if (rc != size) {
+               errlog("partial write ret %d: %s\n", rc, strerror(errno));
+               rc = -1;
+       } else {
+               rc = 0;
+       }
 
+out_params:
+       cfs_free_param_data(&path);
 out:
-        if (data != NULL)
-                free(data);
-        return rc;
+       if (data != NULL)
+               free(data);
+       return rc;
 }