xref: /freebsd/crypto/heimdal/appl/afsutil/afslog.c (revision 5e9cd1ae3e10592ed70e7575551cba1bbab04d84)
1b528cefcSMark Murray /*
25e9cd1aeSAssar Westerlund  * Copyright (c) 1997-2001 Kungliga Tekniska H�gskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #ifdef HAVE_CONFIG_H
35b528cefcSMark Murray #include <config.h>
365e9cd1aeSAssar Westerlund RCSID("$Id: afslog.c,v 1.14 2001/01/25 12:44:46 assar Exp $");
37b528cefcSMark Murray #endif
38b528cefcSMark Murray #include <ctype.h>
39b528cefcSMark Murray #include <krb5.h>
40b528cefcSMark Murray #include <kafs.h>
41b528cefcSMark Murray #include <roken.h>
42b528cefcSMark Murray #include <getarg.h>
435e9cd1aeSAssar Westerlund #include <err.h>
44b528cefcSMark Murray 
45b528cefcSMark Murray static int help_flag;
46b528cefcSMark Murray static int version_flag;
47b528cefcSMark Murray #if 0
48b528cefcSMark Murray static int create_user;
49b528cefcSMark Murray #endif
50b528cefcSMark Murray static getarg_strings cells;
51b528cefcSMark Murray static char *realm;
52b528cefcSMark Murray static getarg_strings files;
53b528cefcSMark Murray static int unlog_flag;
54b528cefcSMark Murray static int verbose;
55b528cefcSMark Murray 
56b528cefcSMark Murray struct getargs args[] = {
575e9cd1aeSAssar Westerlund     { "cell",	'c', arg_strings, &cells, "cells to get tokens for", "cells" },
585e9cd1aeSAssar Westerlund     { "file",	'p', arg_strings, &files, "files to get tokens for", "paths" },
59b528cefcSMark Murray     { "realm",	'k', arg_string, &realm, "realm for afs cell", "realm" },
60b528cefcSMark Murray     { "unlog",	'u', arg_flag, &unlog_flag, "remove tokens" },
61b528cefcSMark Murray #if 0
62b528cefcSMark Murray     { "create-user", 0, arg_flag, &create_user, "create user if not found" },
63b528cefcSMark Murray #endif
64b528cefcSMark Murray     { "verbose",'v', arg_flag, &verbose },
65b528cefcSMark Murray     { "version", 0,  arg_flag, &version_flag },
66b528cefcSMark Murray     { "help",	'h', arg_flag, &help_flag },
67b528cefcSMark Murray };
68b528cefcSMark Murray 
69b528cefcSMark Murray static int num_args = sizeof(args) / sizeof(args[0]);
70b528cefcSMark Murray 
71b528cefcSMark Murray static const char *
72b528cefcSMark Murray expand_cell_name(const char *cell)
73b528cefcSMark Murray {
74b528cefcSMark Murray     FILE *f;
75b528cefcSMark Murray     static char buf[128];
76b528cefcSMark Murray     char *p;
77b528cefcSMark Murray 
78b528cefcSMark Murray     f = fopen(_PATH_CELLSERVDB, "r");
79b528cefcSMark Murray     if(f == NULL)
80b528cefcSMark Murray 	return cell;
81b528cefcSMark Murray     while (fgets (buf, sizeof(buf), f) != NULL) {
82b528cefcSMark Murray 	if(buf[0] == '>'){
83b528cefcSMark Murray 	    for(p=buf; *p && !isspace((unsigned char)*p) && *p != '#'; p++)
84b528cefcSMark Murray 		;
85b528cefcSMark Murray 	    *p = '\0';
86b528cefcSMark Murray 	    if(strstr(buf, cell)){
87b528cefcSMark Murray 		fclose(f);
88b528cefcSMark Murray 		return buf + 1;
89b528cefcSMark Murray 	    }
90b528cefcSMark Murray 	}
91b528cefcSMark Murray 	buf[0] = 0;
92b528cefcSMark Murray     }
93b528cefcSMark Murray     fclose(f);
94b528cefcSMark Murray     return cell;
95b528cefcSMark Murray }
96b528cefcSMark Murray 
97b528cefcSMark Murray #if 0
98b528cefcSMark Murray static int
99b528cefcSMark Murray createuser (char *cell)
100b528cefcSMark Murray {
101b528cefcSMark Murray     char cellbuf[64];
102b528cefcSMark Murray     char name[ANAME_SZ];
103b528cefcSMark Murray     char instance[INST_SZ];
104b528cefcSMark Murray     char realm[REALM_SZ];
105b528cefcSMark Murray     char cmd[1024];
106b528cefcSMark Murray 
107b528cefcSMark Murray     if (cell == NULL) {
108b528cefcSMark Murray 	FILE *f;
109b528cefcSMark Murray 	int len;
110b528cefcSMark Murray 
111b528cefcSMark Murray 	f = fopen (_PATH_THISCELL, "r");
112b528cefcSMark Murray 	if (f == NULL)
113b528cefcSMark Murray 	    err (1, "open(%s)", _PATH_THISCELL);
114b528cefcSMark Murray 	if (fgets (cellbuf, sizeof(cellbuf), f) == NULL)
115b528cefcSMark Murray 	    err (1, "read cellname from %s", _PATH_THISCELL);
116b528cefcSMark Murray 	len = strlen(cellbuf);
117b528cefcSMark Murray 	if (cellbuf[len-1] == '\n')
118b528cefcSMark Murray 	    cellbuf[len-1] = '\0';
119b528cefcSMark Murray 	cell = cellbuf;
120b528cefcSMark Murray     }
121b528cefcSMark Murray 
122b528cefcSMark Murray     if(krb_get_default_principal(name, instance, realm))
123b528cefcSMark Murray 	errx (1, "Could not even figure out who you are");
124b528cefcSMark Murray 
125b528cefcSMark Murray     snprintf (cmd, sizeof(cmd),
126b528cefcSMark Murray 	      "pts createuser %s%s%s@%s -cell %s",
127b528cefcSMark Murray 	      name, *instance ? "." : "", instance, strlwr(realm),
128b528cefcSMark Murray 	      cell);
129b528cefcSMark Murray     DEBUG("Executing %s", cmd);
130b528cefcSMark Murray     return system(cmd);
131b528cefcSMark Murray }
132b528cefcSMark Murray #endif
133b528cefcSMark Murray 
134b528cefcSMark Murray static void
135b528cefcSMark Murray usage(int ecode)
136b528cefcSMark Murray {
137b528cefcSMark Murray     arg_printusage(args, num_args, NULL, "[cell]... [path]...");
138b528cefcSMark Murray     exit(ecode);
139b528cefcSMark Murray }
140b528cefcSMark Murray 
141b528cefcSMark Murray static int
142b528cefcSMark Murray afslog_cell(krb5_context context, krb5_ccache id,
143b528cefcSMark Murray 	    const char *cell, int expand)
144b528cefcSMark Murray {
145b528cefcSMark Murray     const char *c = cell;
146b528cefcSMark Murray     if(expand){
147b528cefcSMark Murray 	c = expand_cell_name(cell);
148b528cefcSMark Murray 	if(c == NULL){
149b528cefcSMark Murray 	    krb5_warnx(context, "No cell matching \"%s\" found.", cell);
150b528cefcSMark Murray 	    return -1;
151b528cefcSMark Murray 	}
152b528cefcSMark Murray 	if(verbose)
153b528cefcSMark Murray 	    krb5_warnx(context, "Cell \"%s\" expanded to \"%s\"", cell, c);
154b528cefcSMark Murray     }
155b528cefcSMark Murray     return krb5_afslog(context, id, c, realm);
156b528cefcSMark Murray }
157b528cefcSMark Murray 
158b528cefcSMark Murray static int
159b528cefcSMark Murray afslog_file(krb5_context context, krb5_ccache id,
160b528cefcSMark Murray 	    const char *path)
161b528cefcSMark Murray {
162b528cefcSMark Murray     char cell[64];
163b528cefcSMark Murray     if(k_afs_cell_of_file(path, cell, sizeof(cell))){
164b528cefcSMark Murray 	krb5_warnx(context, "No cell found for file \"%s\".", path);
165b528cefcSMark Murray 	return -1;
166b528cefcSMark Murray     }
167b528cefcSMark Murray     if(verbose)
168b528cefcSMark Murray 	krb5_warnx(context, "File \"%s\" lives in cell \"%s\"", path, cell);
169b528cefcSMark Murray     return afslog_cell(context, id, cell, 0);
170b528cefcSMark Murray }
171b528cefcSMark Murray 
172b528cefcSMark Murray int
173b528cefcSMark Murray main(int argc, char **argv)
174b528cefcSMark Murray {
175b528cefcSMark Murray     int optind = 0;
176b528cefcSMark Murray     krb5_context context;
177b528cefcSMark Murray     krb5_ccache id;
178b528cefcSMark Murray     int i;
179b528cefcSMark Murray     int num;
180b528cefcSMark Murray     int ret = 0;
181b528cefcSMark Murray 
182b528cefcSMark Murray     set_progname(argv[0]);
183b528cefcSMark Murray 
184b528cefcSMark Murray     if(getarg(args, num_args, argc, argv, &optind))
185b528cefcSMark Murray 	usage(1);
186b528cefcSMark Murray     if(help_flag)
187b528cefcSMark Murray 	usage(0);
188b528cefcSMark Murray     if(version_flag) {
189b528cefcSMark Murray 	print_version(NULL);
190b528cefcSMark Murray 	exit(0);
191b528cefcSMark Murray     }
192b528cefcSMark Murray 
1935e9cd1aeSAssar Westerlund     ret = krb5_init_context(&context);
1945e9cd1aeSAssar Westerlund     if (ret)
1955e9cd1aeSAssar Westerlund 	errx (1, "krb5_init_context failed: %d", ret);
196b528cefcSMark Murray     if(!k_hasafs())
197b528cefcSMark Murray 	krb5_errx(context, 1,
198b528cefcSMark Murray 		  "AFS doesn't seem to be present on this machine");
199b528cefcSMark Murray 
200b528cefcSMark Murray     if(unlog_flag){
201b528cefcSMark Murray 	k_unlog();
202b528cefcSMark Murray 	exit(0);
203b528cefcSMark Murray     }
204b528cefcSMark Murray     krb5_cc_default(context, &id);
205b528cefcSMark Murray     num = 0;
206b528cefcSMark Murray     for(i = 0; i < files.num_strings; i++){
207b528cefcSMark Murray 	afslog_file(context, id, files.strings[i]);
208b528cefcSMark Murray 	num++;
209b528cefcSMark Murray     }
210b528cefcSMark Murray     for(i = 0; i < cells.num_strings; i++){
211b528cefcSMark Murray 	afslog_cell(context, id, cells.strings[i], 1);
212b528cefcSMark Murray 	num++;
213b528cefcSMark Murray     }
214b528cefcSMark Murray     for(i = optind; i < argc; i++){
215b528cefcSMark Murray 	num++;
216b528cefcSMark Murray 	if(strcmp(argv[i], ".") == 0 ||
217b528cefcSMark Murray 	   strcmp(argv[i], "..") == 0 ||
218b528cefcSMark Murray 	   strchr(argv[i], '/') ||
219b528cefcSMark Murray 	   access(argv[i], F_OK) == 0)
220b528cefcSMark Murray 	    afslog_file(context, id, argv[i]);
221b528cefcSMark Murray 	else
222b528cefcSMark Murray 	    afslog_cell(context, id, argv[i], 1);
223b528cefcSMark Murray     }
224b528cefcSMark Murray     if(num == 0) {
225b528cefcSMark Murray 	krb5_afslog(context, id, NULL, NULL);
226b528cefcSMark Murray     }
227b528cefcSMark Murray 
228b528cefcSMark Murray     return ret;
229b528cefcSMark Murray }
230