1/*-
2 * Copyright (c) 2009 Rick Macklem, University of Guelph
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28#include <sys/param.h>
29#include <sys/linker.h>
30#include <sys/module.h>
31#include <sys/mount.h>
32#include <sys/socket.h>
33#include <sys/sysctl.h>
34#include <sys/vnode.h>
35
36#include <netinet/in.h>
37
38#include <nfs/nfssvc.h>
39
40#include <fs/nfs/rpcv2.h>
41#include <fs/nfs/nfsproto.h>
42#include <fs/nfs/nfskpiport.h>
43#include <fs/nfs/nfs.h>
44
45#include <ctype.h>
46#include <err.h>
47#include <errno.h>
48#include <fcntl.h>
49#include <limits.h>
50#include <paths.h>
51#include <signal.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <unistd.h>
56
57static void usage(void) __dead2;
58
59int
60main(int argc, char **argv)
61{
62	char *cp;
63	u_char val;
64	int cnt, even;
65	struct nfsd_clid revoke_handle;
66
67	if (modfind("nfsd") < 0)
68		errx(1, "nfsd not loaded - self terminating");
69	if (argc != 2)
70		usage();
71	cnt = 0;
72	cp = argv[1];
73	if (strlen(cp) % 2)
74		even = 0;
75	else
76		even = 1;
77	val = 0;
78	while (*cp) {
79		if (*cp >= '0' && *cp <= '9')
80			val += (u_char)(*cp - '0');
81		else if (*cp >= 'A' && *cp <= 'F')
82			val += ((u_char)(*cp - 'A')) + 0xa;
83		else if (*cp >= 'a' && *cp <= 'f')
84			val += ((u_char)(*cp - 'a')) + 0xa;
85		else
86			errx(1, "Non hexadecimal digit in %s", argv[1]);
87		if (even) {
88			val <<= 4;
89			even = 0;
90		} else {
91			revoke_handle.nclid_id[cnt++] = val;
92			if (cnt > NFSV4_OPAQUELIMIT)
93				errx(1, "Clientid %s, loo long", argv[1]);
94			val = 0;
95			even = 1;
96		}
97		cp++;
98	}
99
100	/*
101	 * Do the revocation system call.
102	 */
103	revoke_handle.nclid_idlen = cnt;
104#ifdef DEBUG
105	printf("Idlen=%d\n", revoke_handle.nclid_idlen);
106	for (cnt = 0; cnt < revoke_handle.nclid_idlen; cnt++)
107		printf("%02x", revoke_handle.nclid_id[cnt]);
108	printf("\n");
109#else
110	if (nfssvc(NFSSVC_ADMINREVOKE, &revoke_handle) < 0)
111		err(1, "Admin revoke failed");
112#endif
113}
114
115static void
116usage(void)
117{
118
119	errx(1, "Usage: nfsrevoke <ClientID>");
120}
121