17 char *usage="Usage: %s [-v] [-c || [-u|-g|-r key] || [-t timeout] key desc]";
20 #define IDMAP_NAMESZ 128
24 #define PROCKEYS "/proc/keys"
25 #ifndef DEFAULT_KEYRING
26 #define DEFAULT_KEYRING "id_resolver"
29 static int keyring_clear(char *keyring);
35 * Find either a user or group id based on the name@domain string
37 int id_lookup(char *name_at_domain, key_serial_t key, int type)
45 rc = nfs4_owner_to_uid(name_at_domain, &uid);
46 sprintf(id, "%u", uid);
48 rc = nfs4_group_owner_to_gid(name_at_domain, &gid);
49 sprintf(id, "%u", gid);
52 xlog_err("id_lookup: %s: failed: %m",
53 (type == USER ? "nfs4_owner_to_uid" : "nfs4_group_owner_to_gid"));
56 rc = keyctl_instantiate(key, id, strlen(id) + 1, 0);
63 * The keyring is full. Clear the keyring and try again
65 rc = keyring_clear(DEFAULT_KEYRING);
67 rc = keyctl_instantiate(key, id, strlen(id) + 1, 0);
74 xlog_err("id_lookup: keyctl_instantiate failed: %m");
81 * Find the name@domain string from either a user or group id
83 int name_lookup(char *id, key_serial_t key, int type)
85 char name[IDMAP_NAMESZ];
86 char domain[NFS4_MAX_DOMAIN_LEN];
91 rc = nfs4_get_default_domain(NULL, domain, NFS4_MAX_DOMAIN_LEN);
94 xlog_err("name_lookup: nfs4_get_default_domain failed: %m");
100 rc = nfs4_uid_to_name(uid, domain, name, IDMAP_NAMESZ);
103 rc = nfs4_gid_to_name(gid, domain, name, IDMAP_NAMESZ);
106 xlog_err("name_lookup: %s: failed: %m",
107 (type == USER ? "nfs4_uid_to_name" : "nfs4_gid_to_name"));
110 rc = keyctl_instantiate(key, &name, strlen(name), 0);
112 xlog_err("name_lookup: keyctl_instantiate failed: %m");
118 * Clear all the keys on the given keyring
120 static int keyring_clear(char *keyring)
127 keyring = DEFAULT_KEYRING;
129 if ((fp = fopen(PROCKEYS, "r")) == NULL) {
130 xlog_err("fopen(%s) failed: %m", PROCKEYS);
134 while(fgets(buf, BUFSIZ, fp) != NULL) {
135 if (strstr(buf, "keyring") == NULL)
137 if (strstr(buf, keyring) == NULL)
140 *(strchr(buf, '\n')) = '\0';
141 xlog_warn("clearing '%s'", buf);
144 * The key is the first arugment in the string
146 *(strchr(buf, ' ')) = '\0';
147 sscanf(buf, "%x", &key);
148 if (keyctl_clear(key) < 0) {
149 xlog_err("keyctl_clear(0x%x) failed: %m", key);
156 xlog_err("'%s' keyring was not found.", keyring);
163 static int key_revoke(char *keystr, int keymask)
166 char buf[BUFSIZ], *ptr;
172 if ((fp = fopen(PROCKEYS, "r")) == NULL) {
173 xlog_err("fopen(%s) failed: %m", PROCKEYS);
177 while(fgets(buf, BUFSIZ, fp) != NULL) {
178 if (strstr(buf, "keyring") != NULL)
182 if ((ptr = strstr(buf, "uid:")) != NULL)
184 else if ((ptr = strstr(buf, "gid:")) != NULL)
189 if ((keymask & mask) == 0)
192 if (strncmp(ptr+4, keystr, strlen(keystr)) != 0)
196 *(strchr(buf, '\n')) = '\0';
197 xlog_warn("revoking '%s'", buf);
200 * The key is the first arugment in the string
202 *(strchr(buf, ' ')) = '\0';
203 sscanf(buf, "%x", &key);
205 if (keyctl_revoke(key) < 0) {
206 xlog_err("keyctl_revoke(0x%x) failed: %m", key);
217 xlog_err("'%s' key was not found.", keystr);
222 int main(int argc, char **argv)
230 char *progname, *keystr = NULL;
231 int clearing = 0, keymask = 0;
233 /* Set the basename */
234 if ((progname = strrchr(argv[0], '/')) != NULL)
241 while ((opt = getopt(argc, argv, "u:g:r:ct:v")) != -1) {
245 keystr = strdup(optarg);
249 keystr = strdup(optarg);
252 keymask = GIDKEYS|UIDKEYS;
253 keystr = strdup(optarg);
262 timeout = atoi(optarg);
265 xlog_warn(usage, progname);
271 rc = key_revoke(keystr, keymask);
276 rc = keyring_clear(DEFAULT_KEYRING);
281 if ((argc - optind) != 2) {
282 xlog_err("Bad arg count. Check /etc/request-key.conf");
283 xlog_warn(usage, progname);
288 nfs4_set_debug(verbose, NULL);
290 key = strtol(argv[optind++], NULL, 10);
292 arg = strdup(argv[optind]);
294 xlog_err("strdup failed: %m");
297 type = strtok(arg, ":");
298 value = strtok(NULL, ":");
301 xlog_warn("key: 0x%lx type: %s value: %s timeout %ld",
302 key, type, value, timeout);
305 if (strcmp(type, "uid") == 0)
306 rc = id_lookup(value, key, USER);
307 else if (strcmp(type, "gid") == 0)
308 rc = id_lookup(value, key, GROUP);
309 else if (strcmp(type, "user") == 0)
310 rc = name_lookup(value, key, USER);
311 else if (strcmp(type, "group") == 0)
312 rc = name_lookup(value, key, GROUP);
314 /* Set timeout to 10 (600 seconds) minutes */
316 keyctl_set_timeout(key, timeout);