nfsrevoke.c revision 192811
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/cdefs.h>
29__FBSDID("$FreeBSD: head/usr.sbin/nfsrevoke/nfsrevoke.c 192811 2009-05-26 15:19:04Z rmacklem $");
30
31#include <sys/param.h>
32#include <sys/linker.h>
33#include <sys/module.h>
34#include <sys/mount.h>
35#include <sys/socket.h>
36#include <sys/sysctl.h>
37#include <sys/vnode.h>
38
39#include <netinet/in.h>
40
41#include <nfs/nfssvc.h>
42
43#include <fs/nfs/rpcv2.h>
44#include <fs/nfs/nfsproto.h>
45#include <fs/nfs/nfskpiport.h>
46#include <fs/nfs/nfs.h>
47
48#include <ctype.h>
49#include <err.h>
50#include <errno.h>
51#include <fcntl.h>
52#include <limits.h>
53#include <paths.h>
54#include <signal.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <unistd.h>
59
60void usage(void);
61extern int errno;
62
63int
64main(int argc, char **argv)
65{
66	char *cp;
67	u_char val;
68	int cnt, even;
69	struct nfsd_clid revoke;
70
71	if (modfind("nfsd") < 0)
72		errx(1, "nfsd not loaded - self terminating");
73	if (argc != 2)
74		usage();
75	cnt = 0;
76	cp = argv[1];
77	if (strlen(cp) % 2)
78		even = 0;
79	else
80		even = 1;
81	val = 0;
82	while (*cp) {
83		if (*cp >= '0' & *cp <= '9')
84			val += (u_char)(*cp - '0');
85		else if (*cp >= 'A' && *cp <= 'F')
86			val += ((u_char)(*cp - 'A')) + 0xa;
87		else if (*cp >= 'a' && *cp <= 'f')
88			val += ((u_char)(*cp - 'a')) + 0xa;
89		else
90			errx(1, "Non hexadecimal digit in %s", argv[1]);
91		if (even) {
92			val <<= 4;
93			even = 0;
94		} else {
95			revoke.nclid_id[cnt++] = val;
96			if (cnt > NFSV4_OPAQUELIMIT)
97				errx(1, "Clientid %s, loo long", argv[1]);
98			val = 0;
99			even = 1;
100		}
101		cp++;
102	}
103
104	/*
105	 * Do the revocation system call.
106	 */
107	revoke.nclid_idlen = cnt;
108#ifdef DEBUG
109	printf("Idlen=%d\n", revoke.nclid_idlen);
110	for (cnt = 0; cnt < revoke.nclid_idlen; cnt++)
111		printf("%02x", revoke.nclid_id[cnt]);
112	printf("\n");
113#else
114	if (nfssvc(NFSSVC_ADMINREVOKE, &revoke) < 0)
115		err(1, "Admin revoke failed");
116#endif
117}
118
119void
120usage(void)
121{
122
123	errx(1, "Usage: nfsrevoke <ClientID>");
124}
125