]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - utils/nfsidmap/nfsidmap.c
nfsidmap: Added -v and -t flags
[nfs-utils.git] / utils / nfsidmap / nfsidmap.c
index 2d87381b7a9e646c5505c52052f3c2b997e9fbc6..6a09f38113b5adb6aab7ff481d4ec7873ec1e21d 100644 (file)
@@ -9,16 +9,17 @@
 #include <keyutils.h>
 #include <nfsidmap.h>
 
-#include <syslog.h>
+#include <unistd.h>
+#include "xlog.h"
 
-/* gcc nfsidmap.c -o nfsidmap -l nfsidmap -l keyutils */
+int verbose = 0;
+char *usage="Usage: %s [-v] [-t timeout] key desc";
 
 #define MAX_ID_LEN   11
 #define IDMAP_NAMESZ 128
 #define USER  1
 #define GROUP 0
 
-
 /*
  * Find either a user or group id based on the name@domain string
  */
@@ -36,9 +37,15 @@ int id_lookup(char *name_at_domain, key_serial_t key, int type)
                rc = nfs4_group_owner_to_gid(name_at_domain, &gid);
                sprintf(id, "%u", gid);
        }
+       if (rc < 0)
+               xlog_err("id_lookup: %s: failed: %m",
+                       (type == USER ? "nfs4_owner_to_uid" : "nfs4_group_owner_to_gid"));
 
-       if (rc == 0)
+       if (rc == 0) {
                rc = keyctl_instantiate(key, id, strlen(id) + 1, 0);
+               if (rc < 0)
+                       xlog_err("id_lookup: keyctl_instantiate failed: %m");
+       }
 
        return rc;
 }
@@ -57,6 +64,7 @@ int name_lookup(char *id, key_serial_t key, int type)
        rc = nfs4_get_default_domain(NULL, domain, NFS4_MAX_DOMAIN_LEN);
        if (rc != 0) {
                rc = -1;
+               xlog_err("name_lookup: nfs4_get_default_domain failed: %m");
                goto out;
        }
 
@@ -67,10 +75,15 @@ int name_lookup(char *id, key_serial_t key, int type)
                gid = atoi(id);
                rc = nfs4_gid_to_name(gid, domain, name, IDMAP_NAMESZ);
        }
+       if (rc < 0)
+               xlog_err("name_lookup: %s: failed: %m",
+                       (type == USER ? "nfs4_uid_to_name" : "nfs4_gid_to_name"));
 
-       if (rc == 0)
+       if (rc == 0) {
                rc = keyctl_instantiate(key, &name, strlen(name), 0);
-
+               if (rc < 0)
+                       xlog_err("name_lookup: keyctl_instantiate failed: %m");
+       }
 out:
        return rc;
 }
@@ -80,26 +93,59 @@ int main(int argc, char **argv)
        char *arg;
        char *value;
        char *type;
-       int rc = 1;
+       int rc = 1, opt;
        int timeout = 600;
        key_serial_t key;
+       char *progname;
+
+       /* Set the basename */
+       if ((progname = strrchr(argv[0], '/')) != NULL)
+               progname++;
+       else
+               progname = argv[0];
+
+       xlog_open(progname);
+       xlog_syslog(1);
+       xlog_stderr(0);
+
+       while ((opt = getopt(argc, argv, "t:v")) != -1) {
+               switch (opt) {
+               case 'v':
+                       verbose++;
+                       break;
+               case 't':
+                       timeout = atoi(optarg);
+                       break;
+               default:
+                       xlog_warn(usage, progname);
+                       break;
+               }
+       }
 
-       if (argc < 3)
+       if ((argc - optind) != 2) {
+               xlog_err("Bad arg count. Check /etc/request-key.conf");
+               xlog_warn(usage, progname);
                return 1;
+       }
+
+       if (verbose)
+               nfs4_set_debug(verbose, NULL);
+
+       key = strtol(argv[optind++], NULL, 10);
 
-       arg = malloc(sizeof(char) * strlen(argv[2]) + 1);
-       strcpy(arg, argv[2]);
+       arg = strdup(argv[optind]);
+       if (arg == NULL) {
+               xlog_err("strdup failed: %m");
+               return 1;
+       }
        type = strtok(arg, ":");
        value = strtok(NULL, ":");
 
-       if (argc == 4) {
-               timeout = atoi(argv[3]);
-               if (timeout < 0)
-                       timeout = 0;
+       if (verbose) {
+               xlog_warn("key: %ld type: %s value: %s timeout %ld",
+                       key, type, value, timeout);
        }
 
-       key = strtol(argv[1], NULL, 10);
-
        if (strcmp(type, "uid") == 0)
                rc = id_lookup(value, key, USER);
        else if (strcmp(type, "gid") == 0)
@@ -109,7 +155,7 @@ int main(int argc, char **argv)
        else if (strcmp(type, "group") == 0)
                rc = name_lookup(value, key, GROUP);
 
-       /* Set timeout to 5 (600 seconds) minutes */
+       /* Set timeout to 10 (600 seconds) minutes */
        if (rc == 0)
                keyctl_set_timeout(key, timeout);