]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/nfsidmap/nfsidmap.c
134d9bcc21904fcb0cebd4da9452507158678e49
[nfs-utils.git] / utils / nfsidmap / nfsidmap.c
1
2 #include <stdarg.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include <pwd.h>
8 #include <grp.h>
9 #include <keyutils.h>
10 #include <nfsidmap.h>
11
12 #include <syslog.h>
13 #include "xlog.h"
14
15 /* gcc nfsidmap.c -o nfsidmap -l nfsidmap -l keyutils */
16
17 #define MAX_ID_LEN   11
18 #define IDMAP_NAMESZ 128
19 #define USER  1
20 #define GROUP 0
21
22
23 /*
24  * Find either a user or group id based on the name@domain string
25  */
26 int id_lookup(char *name_at_domain, key_serial_t key, int type)
27 {
28         char id[MAX_ID_LEN];
29         uid_t uid = 0;
30         gid_t gid = 0;
31         int rc;
32
33         if (type == USER) {
34                 rc = nfs4_owner_to_uid(name_at_domain, &uid);
35                 sprintf(id, "%u", uid);
36         } else {
37                 rc = nfs4_group_owner_to_gid(name_at_domain, &gid);
38                 sprintf(id, "%u", gid);
39         }
40         if (rc < 0)
41                 xlog_err("id_lookup: %s: failed: %m",
42                         (type == USER ? "nfs4_owner_to_uid" : "nfs4_group_owner_to_gid"));
43
44         if (rc == 0) {
45                 rc = keyctl_instantiate(key, id, strlen(id) + 1, 0);
46                 if (rc < 0)
47                         xlog_err("id_lookup: keyctl_instantiate failed: %m");
48         }
49
50         return rc;
51 }
52
53 /*
54  * Find the name@domain string from either a user or group id
55  */
56 int name_lookup(char *id, key_serial_t key, int type)
57 {
58         char name[IDMAP_NAMESZ];
59         char domain[NFS4_MAX_DOMAIN_LEN];
60         uid_t uid;
61         gid_t gid;
62         int rc;
63
64         rc = nfs4_get_default_domain(NULL, domain, NFS4_MAX_DOMAIN_LEN);
65         if (rc != 0) {
66                 rc = -1;
67                 xlog_err("name_lookup: nfs4_get_default_domain failed: %m");
68                 goto out;
69         }
70
71         if (type == USER) {
72                 uid = atoi(id);
73                 rc = nfs4_uid_to_name(uid, domain, name, IDMAP_NAMESZ);
74         } else {
75                 gid = atoi(id);
76                 rc = nfs4_gid_to_name(gid, domain, name, IDMAP_NAMESZ);
77         }
78         if (rc < 0)
79                 xlog_err("name_lookup: %s: failed: %m",
80                         (type == USER ? "nfs4_uid_to_name" : "nfs4_gid_to_name"));
81
82         if (rc == 0) {
83                 rc = keyctl_instantiate(key, &name, strlen(name), 0);
84                 if (rc < 0)
85                         xlog_err("name_lookup: keyctl_instantiate failed: %m");
86         }
87 out:
88         return rc;
89 }
90
91 int main(int argc, char **argv)
92 {
93         char *arg;
94         char *value;
95         char *type;
96         int rc = 1;
97         int timeout = 600;
98         key_serial_t key;
99         char *progname;
100
101         /* Set the basename */
102         if ((progname = strrchr(argv[0], '/')) != NULL)
103                 progname++;
104         else
105                 progname = argv[0];
106
107         xlog_open(progname);
108         xlog_syslog(1);
109         xlog_stderr(0);
110
111         if (argc < 3) {
112                 xlog_err("Bad arg count. Check /etc/request-key.conf");
113                 return 1;
114         }
115
116         arg = malloc(sizeof(char) * strlen(argv[2]) + 1);
117         strcpy(arg, argv[2]);
118         type = strtok(arg, ":");
119         value = strtok(NULL, ":");
120
121         if (argc == 4) {
122                 timeout = atoi(argv[3]);
123                 if (timeout < 0)
124                         timeout = 0;
125         }
126
127         key = strtol(argv[1], NULL, 10);
128
129         if (strcmp(type, "uid") == 0)
130                 rc = id_lookup(value, key, USER);
131         else if (strcmp(type, "gid") == 0)
132                 rc = id_lookup(value, key, GROUP);
133         else if (strcmp(type, "user") == 0)
134                 rc = name_lookup(value, key, USER);
135         else if (strcmp(type, "group") == 0)
136                 rc = name_lookup(value, key, GROUP);
137
138         /* Set timeout to 5 (600 seconds) minutes */
139         if (rc == 0)
140                 keyctl_set_timeout(key, timeout);
141
142         free(arg);
143         return rc;
144 }