Sync usage with man page.
[netbsd-mini2440.git] / crypto / dist / heimdal / appl / afsutil / afslog.c
blobaaba8493c5c8eb8227acb1c1a2ff855c2d51bc93
1 /*
2 * Copyright (c) 1997-2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 __RCSID("$Heimdal: afslog.c 16438 2006-01-03 09:27:54Z lha $"
37 "$NetBSD$");
38 #endif
39 #include <ctype.h>
40 #ifdef KRB5
41 #include <krb5.h>
42 #endif
43 #ifdef KRB4
44 #include <krb.h>
45 #endif
46 #include <kafs.h>
47 #include <roken.h>
48 #include <getarg.h>
49 #include <err.h>
51 static int help_flag;
52 static int version_flag;
53 static getarg_strings cells;
54 static char *realm;
55 static getarg_strings files;
56 static int unlog_flag;
57 static int verbose;
58 #ifdef KRB4
59 static int use_krb4 = 1;
60 #endif
61 #ifdef KRB5
62 static char *client_string;
63 static char *cache_string;
64 static int use_krb5 = 1;
65 #endif
67 struct getargs args[] = {
68 { "cell", 'c', arg_strings, &cells, "cells to get tokens for", "cell" },
69 { "file", 'p', arg_strings, &files, "files to get tokens for", "path" },
70 { "realm", 'k', arg_string, &realm, "realm for afs cell", "realm" },
71 { "unlog", 'u', arg_flag, &unlog_flag, "remove tokens" },
72 #ifdef KRB4
73 { "v4", 0, arg_negative_flag, &use_krb4, "don't use Kerberos 4" },
74 #endif
75 #ifdef KRB5
76 { "principal",'P',arg_string,&client_string,"principal to use","principal"},
77 { "cache", 0, arg_string, &cache_string, "ccache to use", "cache"},
78 { "v5", 0, arg_negative_flag, &use_krb5, "don't use Kerberos 5" },
79 #endif
80 { "verbose",'v', arg_flag, &verbose },
81 { "version", 0, arg_flag, &version_flag },
82 { "help", 'h', arg_flag, &help_flag },
85 static int num_args = sizeof(args) / sizeof(args[0]);
87 #ifdef KRB5
88 krb5_context context;
89 krb5_ccache id;
90 #endif
92 static const char *
93 expand_one_file(FILE *f, const char *cell)
95 static char buf[1024];
96 char *p;
98 while (fgets (buf, sizeof(buf), f) != NULL) {
99 if(buf[0] == '>') {
100 for(p = buf; *p && !isspace((unsigned char)*p) && *p != '#'; p++)
102 *p = '\0';
103 if(strncmp(buf + 1, cell, strlen(cell)) == 0)
104 return buf + 1;
106 buf[0] = '\0';
108 return NULL;
111 static const char *
112 expand_cell_name(const char *cell)
114 FILE *f;
115 const char *c;
116 const char **fn, *files[] = { _PATH_CELLSERVDB,
117 _PATH_ARLA_CELLSERVDB,
118 _PATH_OPENAFS_DEBIAN_CELLSERVDB,
119 _PATH_ARLA_DEBIAN_CELLSERVDB,
120 NULL };
121 for(fn = files; *fn; fn++) {
122 f = fopen(*fn, "r");
123 if(f == NULL)
124 continue;
125 c = expand_one_file(f, cell);
126 fclose(f);
127 if(c)
128 return c;
130 return cell;
133 static void
134 usage(int ecode)
136 arg_printusage(args, num_args, NULL, "[cell|path]...");
137 exit(ecode);
140 struct cell_list {
141 char *cell;
142 struct cell_list *next;
143 } *cell_list;
145 static int
146 afslog_cell(const char *cell, int expand)
148 struct cell_list *p, **q;
149 const char *c = cell;
150 if(expand){
151 c = expand_cell_name(cell);
152 if(c == NULL){
153 warnx("No cell matching \"%s\" found.", cell);
154 return -1;
156 if(verbose && strcmp(c, cell) != 0)
157 warnx("Cell \"%s\" expanded to \"%s\"", cell, c);
159 /* add to list of cells to get tokens for, and also remove
160 duplicates; the actual afslog takes place later */
161 for(p = cell_list, q = &cell_list; p; q = &p->next, p = p->next)
162 if(strcmp(p->cell, c) == 0)
163 return 0;
164 p = malloc(sizeof(*p));
165 if(p == NULL)
166 return -1;
167 p->cell = strdup(c);
168 if(p->cell == NULL) {
169 free(p);
170 return -1;
172 p->next = NULL;
173 *q = p;
174 return 0;
177 static int
178 afslog_file(const char *path)
180 char cell[64];
181 if(k_afs_cell_of_file(path, cell, sizeof(cell))){
182 warnx("No cell found for file \"%s\".", path);
183 return -1;
185 if(verbose)
186 warnx("File \"%s\" lives in cell \"%s\"", path, cell);
187 return afslog_cell(cell, 0);
190 static int
191 do_afslog(const char *cell)
193 int k5ret, k4ret;
195 k5ret = k4ret = 0;
197 #ifdef KRB5
198 if(context != NULL && id != NULL && use_krb5) {
199 k5ret = krb5_afslog(context, id, cell, realm);
200 if(k5ret == 0)
201 return 0;
203 #endif
204 #if KRB4
205 if (use_krb4) {
206 k4ret = krb_afslog(cell, realm);
207 if(k4ret == 0)
208 return 0;
210 #endif
211 if (cell == NULL)
212 cell = "<default cell>";
213 #ifdef KRB5
214 if (k5ret)
215 warnx("krb5_afslog(%s): %s", cell, krb5_get_err_text(context, k5ret));
216 #endif
217 #ifdef KRB4
218 if (k4ret)
219 warnx("krb_afslog(%s): %s", cell, krb_get_err_text(k4ret));
220 #endif
221 if (k5ret || k4ret)
222 return 1;
223 return 0;
226 static void
227 log_func(void *ctx, const char *str)
229 fprintf(stderr, "%s\n", str);
233 main(int argc, char **argv)
235 int optind = 0;
236 int i;
237 int num;
238 int ret = 0;
239 int failed = 0;
240 struct cell_list *p;
242 setprogname(argv[0]);
244 if(getarg(args, num_args, argc, argv, &optind))
245 usage(1);
246 if(help_flag)
247 usage(0);
248 if(version_flag) {
249 print_version(NULL);
250 exit(0);
253 if(!k_hasafs())
254 errx(1, "AFS does not seem to be present on this machine");
256 if(unlog_flag){
257 k_unlog();
258 exit(0);
260 #ifdef KRB5
261 ret = krb5_init_context(&context);
262 if (ret) {
263 context = NULL;
264 } else {
265 if (client_string) {
266 krb5_principal client;
268 ret = krb5_parse_name(context, client_string, &client);
269 if (ret == 0)
270 ret = krb5_cc_cache_match(context, client, NULL, &id);
271 if (ret)
272 id = NULL;
274 if (id == NULL && cache_string) {
275 if(krb5_cc_resolve(context, cache_string, &id) != 0) {
276 krb5_warnx(context, "failed to open kerberos 5 cache '%s'",
277 cache_string);
278 id = NULL;
281 if (id == NULL)
282 if(krb5_cc_default(context, &id) != 0)
283 id = NULL;
285 #endif
287 if (verbose)
288 kafs_set_verbose(log_func, NULL);
290 num = 0;
291 for(i = 0; i < files.num_strings; i++){
292 afslog_file(files.strings[i]);
293 num++;
295 free_getarg_strings (&files);
296 for(i = 0; i < cells.num_strings; i++){
297 afslog_cell(cells.strings[i], 1);
298 num++;
300 free_getarg_strings (&cells);
301 for(i = optind; i < argc; i++){
302 num++;
303 if(strcmp(argv[i], ".") == 0 ||
304 strcmp(argv[i], "..") == 0 ||
305 strchr(argv[i], '/') ||
306 access(argv[i], F_OK) == 0)
307 afslog_file(argv[i]);
308 else
309 afslog_cell(argv[i], 1);
311 if(num == 0) {
312 if(do_afslog(NULL))
313 failed++;
314 } else
315 for(p = cell_list; p; p = p->next) {
316 if(verbose)
317 warnx("Getting tokens for cell \"%s\"", p->cell);
318 if(do_afslog(p->cell))
319 failed++;
322 return failed;