This commit was manufactured by cvs2svn to create tag
[heimdal.git] / appl / afsutil / afslog.c
blobfdb026f0e0db96486721128587e915c7c0f97748
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("$Id$");
37 #endif
38 #include <ctype.h>
39 #ifdef KRB5
40 #include <krb5.h>
41 #endif
42 #ifdef KRB4
43 #include <krb.h>
44 #endif
45 #include <kafs.h>
46 #include <roken.h>
47 #include <getarg.h>
48 #include <err.h>
50 static int help_flag;
51 static int version_flag;
52 #if 0
53 static int create_user;
54 #endif
55 static getarg_strings cells;
56 static char *realm;
57 static getarg_strings files;
58 static int unlog_flag;
59 static int verbose;
60 #ifdef KRB4
61 static int use_krb4 = 1;
62 #endif
63 #ifdef KRB5
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 { "v5", 0, arg_negative_flag, &use_krb5, "don't use Kerberos 5" },
77 #endif
78 #if 0
79 { "create-user", 0, arg_flag, &create_user, "create user if not found" },
80 #endif
81 { "verbose",'v', arg_flag, &verbose },
82 { "version", 0, arg_flag, &version_flag },
83 { "help", 'h', arg_flag, &help_flag },
86 static int num_args = sizeof(args) / sizeof(args[0]);
88 #ifdef KRB5
89 krb5_context context;
90 krb5_ccache id;
91 #endif
93 static const char *
94 expand_one_file(FILE *f, const char *cell)
96 static char buf[1024];
97 char *p;
99 while (fgets (buf, sizeof(buf), f) != NULL) {
100 if(buf[0] == '>') {
101 for(p = buf; *p && !isspace((unsigned char)*p) && *p != '#'; p++)
103 *p = '\0';
104 if(strncmp(buf + 1, cell, strlen(cell)) == 0)
105 return buf + 1;
107 buf[0] = '\0';
109 return NULL;
112 static const char *
113 expand_cell_name(const char *cell)
115 FILE *f;
116 const char *c;
117 const char **fn, *files[] = { _PATH_CELLSERVDB,
118 _PATH_ARLA_CELLSERVDB,
119 _PATH_OPENAFS_DEBIAN_CELLSERVDB,
120 _PATH_ARLA_DEBIAN_CELLSERVDB,
121 NULL };
122 for(fn = files; *fn; fn++) {
123 f = fopen(*fn, "r");
124 if(f == NULL)
125 continue;
126 c = expand_one_file(f, cell);
127 fclose(f);
128 if(c)
129 return c;
131 return cell;
134 #if 0
135 static int
136 createuser (char *cell)
138 char cellbuf[64];
139 char name[ANAME_SZ];
140 char instance[INST_SZ];
141 char realm[REALM_SZ];
142 char cmd[1024];
144 if (cell == NULL) {
145 FILE *f;
146 int len;
148 f = fopen (_PATH_THISCELL, "r");
149 if (f == NULL)
150 err (1, "open(%s)", _PATH_THISCELL);
151 if (fgets (cellbuf, sizeof(cellbuf), f) == NULL)
152 err (1, "read cellname from %s", _PATH_THISCELL);
153 len = strlen(cellbuf);
154 if (cellbuf[len-1] == '\n')
155 cellbuf[len-1] = '\0';
156 cell = cellbuf;
159 if(krb_get_default_principal(name, instance, realm))
160 errx (1, "Could not even figure out who you are");
162 snprintf (cmd, sizeof(cmd),
163 "pts createuser %s%s%s@%s -cell %s",
164 name, *instance ? "." : "", instance, strlwr(realm),
165 cell);
166 DEBUG("Executing %s", cmd);
167 return system(cmd);
169 #endif
171 static void
172 usage(int ecode)
174 arg_printusage(args, num_args, NULL, "[cell|path]...");
175 exit(ecode);
178 struct cell_list {
179 char *cell;
180 struct cell_list *next;
181 } *cell_list;
183 static int
184 afslog_cell(const char *cell, int expand)
186 struct cell_list *p, **q;
187 const char *c = cell;
188 if(expand){
189 c = expand_cell_name(cell);
190 if(c == NULL){
191 warnx("No cell matching \"%s\" found.", cell);
192 return -1;
194 if(verbose && strcmp(c, cell) != 0)
195 warnx("Cell \"%s\" expanded to \"%s\"", cell, c);
197 /* add to list of cells to get tokens for, and also remove
198 duplicates; the actual afslog takes place later */
199 for(p = cell_list, q = &cell_list; p; q = &p->next, p = p->next)
200 if(strcmp(p->cell, c) == 0)
201 return 0;
202 p = malloc(sizeof(*p));
203 if(p == NULL)
204 return -1;
205 p->cell = strdup(c);
206 if(p->cell == NULL) {
207 free(p);
208 return -1;
210 p->next = NULL;
211 *q = p;
212 return 0;
215 static int
216 afslog_file(const char *path)
218 char cell[64];
219 if(k_afs_cell_of_file(path, cell, sizeof(cell))){
220 warnx("No cell found for file \"%s\".", path);
221 return -1;
223 if(verbose)
224 warnx("File \"%s\" lives in cell \"%s\"", path, cell);
225 return afslog_cell(cell, 0);
228 static int
229 do_afslog(const char *cell)
231 int k5ret, k4ret;
233 k5ret = k4ret = 0;
235 #ifdef KRB5
236 if(context != NULL && id != NULL && use_krb5) {
237 k5ret = krb5_afslog(context, id, cell, NULL);
238 if(k5ret == 0)
239 return 0;
241 #endif
242 #if KRB4
243 if (use_krb4) {
244 k4ret = krb_afslog(cell, NULL);
245 if(k4ret == 0)
246 return 0;
248 #endif
249 if (cell == NULL)
250 cell = "<default cell>";
251 #ifdef KRB5
252 if (k5ret)
253 warnx("krb5_afslog(%s): %s", cell, krb5_get_err_text(context, k5ret));
254 #endif
255 #ifdef KRB4
256 if (k4ret)
257 warnx("krb_afslog(%s): %s", cell, krb_get_err_text(k4ret));
258 #endif
259 if (k5ret || k4ret)
260 return 1;
261 return 0;
264 static void
265 log_func(void *ctx, const char *str)
267 fprintf(stderr, "%s\n", str);
271 main(int argc, char **argv)
273 int optind = 0;
274 int i;
275 int num;
276 int ret = 0;
277 int failed = 0;
278 struct cell_list *p;
280 setprogname(argv[0]);
282 if(getarg(args, num_args, argc, argv, &optind))
283 usage(1);
284 if(help_flag)
285 usage(0);
286 if(version_flag) {
287 print_version(NULL);
288 exit(0);
291 if(!k_hasafs())
292 errx(1, "AFS does not seem to be present on this machine");
294 if(unlog_flag){
295 k_unlog();
296 exit(0);
298 #ifdef KRB5
299 ret = krb5_init_context(&context);
300 if (ret)
301 context = NULL;
302 else
303 if(krb5_cc_default(context, &id) != 0)
304 id = NULL;
305 #endif
307 if (verbose)
308 kafs_set_verbose(log_func, NULL);
310 num = 0;
311 for(i = 0; i < files.num_strings; i++){
312 afslog_file(files.strings[i]);
313 num++;
315 free_getarg_strings (&files);
316 for(i = 0; i < cells.num_strings; i++){
317 afslog_cell(cells.strings[i], 1);
318 num++;
320 free_getarg_strings (&cells);
321 for(i = optind; i < argc; i++){
322 num++;
323 if(strcmp(argv[i], ".") == 0 ||
324 strcmp(argv[i], "..") == 0 ||
325 strchr(argv[i], '/') ||
326 access(argv[i], F_OK) == 0)
327 afslog_file(argv[i]);
328 else
329 afslog_cell(argv[i], 1);
331 if(num == 0) {
332 if(do_afslog(NULL))
333 failed++;
334 } else
335 for(p = cell_list; p; p = p->next) {
336 if(verbose)
337 warnx("Getting tokens for cell \"%s\"", p->cell);
338 if(do_afslog(p->cell))
339 failed++;
342 return failed;