1/*
2 * Copyright (c) 2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23#define _DARWIN_FEATURE_64_BIT_INODE
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28#include <err.h>
29#include <sys/attr.h>
30#include <sys/errno.h>
31#include <sys/mount.h>
32#include <System/sys/fsctl.h>
33#include <nfs/nfs_ioctl.h>
34
35int
36nfs_cred_destroy(const char *path, uint32_t flags)
37{
38	int error;
39
40	if (path == NULL)
41		return (EINVAL);
42
43	error = fsctl(path, NFS_IOC_DESTROY_CRED, NULL, flags);
44	return (error);
45}
46
47
48int
49main(int argc, char *argv[])
50{
51	int count, i, opt;
52	int return_status = 0;
53	unsigned int flags = 0;
54	struct statfs *mounts;
55	struct statfs fsbuf;
56	char path_storage[PATH_MAX];
57	char *path;
58	int verbose = 0;
59	int error;
60
61	while ((opt = getopt(argc, argv, "Pv")) != -1) {
62		switch (opt) {
63		case 'P':
64			flags = FSOPT_NOFOLLOW;
65			break;
66		case 'v':
67			verbose = 1;
68			break;
69		}
70	}
71	argc -= optind;
72	argv += optind;
73
74	for (i = 0; i < argc; i++) {
75		path = (flags & FSOPT_NOFOLLOW) ? argv[i] : realpath(argv[i], path_storage);
76
77		error = statfs(argv[i], &fsbuf);
78		if (error && errno == ENOENT) {
79			warn("%s", argv[i]);
80			return_status = 1;
81			continue;
82		}
83		if (verbose) {
84			printf("Flushing credentials associated with %s ", path);
85			if (!error)
86				printf("Mounted on %s", fsbuf.f_mntonname);
87			printf("\n");
88		}
89		error = nfs_cred_destroy(argv[i], flags);
90		if (verbose && error )
91			warn("fsctl for %s", path);
92	}
93
94	if (i == 0) {
95		count = getmntinfo(&mounts, MNT_NOWAIT);
96		if (count == 0)
97			err(1, "getmntinfo failed");
98
99		for (i = 0; i < count; i++) {
100			if (strcmp(mounts[i].f_fstypename, "nfs") == 0) {
101				if (verbose)
102					printf("Flushing credentials from %s\n", mounts[i].f_mntonname);
103				error = nfs_cred_destroy(mounts[i].f_mntonname, 0);
104				if (error && verbose)
105					warn("fsctl %s", mounts[i].f_mntonname);
106			}
107		}
108	}
109
110	return (return_status);
111}
112