Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / appl / afsutil / aklog.c
1 /*
2  * Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 
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.
16  * 
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.
20  * 
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.
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <stdarg.h>
40 #include <string.h>
41 #include <ctype.h>
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif
45 #ifdef HAVE_FCNTL_H
46 #include <fcntl.h>
47 #endif
48 #ifdef HAVE_SYS_TYPES_H
49 #include <sys/types.h>
50 #endif
51 #ifdef HAVE_SYS_SOCKET_H
52 #include <sys/socket.h>
53 #endif
54 #if defined(HAVE_SYS_IOCTL_H) && SunOS != 40
55 #include <sys/ioctl.h>
56 #endif
57 #ifdef HAVE_SYS_IOCCOM_H
58 #include <sys/ioccom.h>
59 #endif
60 #ifdef HAVE_PWD_H
61 #include <pwd.h>
62 #endif
63 #include <err.h>
64 #include <krb.h>
65 #include <kafs.h>
66
67 #include <roken.h>
68
69 RCSID("$Id: aklog.c,v 1.24.2.1 2000/06/23 02:31:15 assar Exp $");
70
71 static int debug = 0;
72
73 static void
74 DEBUG(const char *, ...)
75 #ifdef __GNUC__
76 __attribute__ ((format (printf, 1, 2)))
77 #endif
78 ;
79
80 static void
81 DEBUG(const char *fmt, ...)
82 {
83   va_list ap;
84   if (debug) {
85     va_start(ap, fmt);
86     vwarnx(fmt, ap);
87     va_end(ap);
88   }
89 }
90
91 static char *
92 expand_1 (const char *cell, const char *filename)
93 {
94   FILE *f;
95   static char buf[128];
96   char *p;
97
98   f = fopen(filename, "r");
99   if(f == NULL)
100     return NULL;
101   while(fgets(buf, sizeof(buf), f) != NULL) {
102     if(buf[0] == '>') {
103       for(p=buf; *p && !isspace(*p) && *p != '#'; p++)
104           ;
105       *p = '\0';
106       if(strstr(buf, cell)){
107         fclose(f);
108         return buf + 1;
109       }
110     }
111     buf[0] = 0;
112   }
113   fclose(f);
114   return NULL;
115 }
116
117 static const char *
118 expand_cell_name(const char *cell)
119 {
120   char *ret;
121
122   ret = expand_1(cell, _PATH_CELLSERVDB);
123   if (ret != NULL)
124     return ret;
125   ret = expand_1(cell, _PATH_ARLA_CELLSERVDB);
126   if (ret != NULL)
127     return ret;
128   return cell;
129 }
130
131 static int
132 createuser (const char *cell)
133 {
134   char cellbuf[64];
135   char name[ANAME_SZ];
136   char instance[INST_SZ];
137   char realm[REALM_SZ];
138   char cmd[1024];
139
140   if (cell == NULL) {
141     FILE *f;
142     int len;
143
144     f = fopen (_PATH_THISCELL, "r");
145     if (f == NULL)
146       f = fopen (_PATH_ARLA_THISCELL, "r");
147     if (f == NULL)
148       err (1, "open(%s, %s)", _PATH_THISCELL, _PATH_ARLA_THISCELL);
149     if (fgets (cellbuf, sizeof(cellbuf), f) == NULL)
150       err (1, "read cellname from %s %s", _PATH_THISCELL, _PATH_ARLA_THISCELL);
151     fclose (f);
152     len = strlen(cellbuf);
153     if (cellbuf[len-1] == '\n')
154       cellbuf[len-1] = '\0';
155     cell = cellbuf;
156   }
157
158   if(krb_get_default_principal(name, instance, realm))
159     errx (1, "Could not even figure out who you are");
160
161   snprintf (cmd, sizeof(cmd),
162             "pts createuser %s%s%s@%s -cell %s",
163             name, *instance ? "." : "", instance, strlwr(realm),
164             cell);
165   DEBUG("Executing %s", cmd);
166   return system(cmd);
167 }
168
169 int
170 main(int argc, char **argv)
171 {
172   int i;
173   int do_aklog = -1;
174   int do_createuser = -1;
175   const char *cell = NULL;
176   char *realm = NULL;
177   char cellbuf[64];
178   
179   set_progname (argv[0]);
180
181   if(!k_hasafs())
182     exit(1);
183
184   for(i = 1; i < argc; i++){
185     if(!strncmp(argv[i], "-createuser", 11)){
186       do_createuser = do_aklog = 1;
187
188     }else if(!strncmp(argv[i], "-c", 2) && i + 1 < argc){
189       cell = expand_cell_name(argv[++i]);
190       do_aklog = 1;
191
192     }else if(!strncmp(argv[i], "-k", 2) && i + 1 < argc){
193       realm = argv[++i];
194
195     }else if(!strncmp(argv[i], "-p", 2) && i + 1 < argc){
196       if(k_afs_cell_of_file(argv[++i], cellbuf, sizeof(cellbuf)))
197         errx (1, "No cell found for file \"%s\".", argv[i]);
198       else
199         cell = cellbuf;
200       do_aklog = 1;
201
202     }else if(!strncmp(argv[i], "-unlog", 6)){
203       exit(k_unlog());
204
205     }else if(!strncmp(argv[i], "-hosts", 6)){
206       warnx ("Argument -hosts is not implemented.");
207
208     }else if(!strncmp(argv[i], "-zsubs", 6)){
209       warnx("Argument -zsubs is not implemented.");
210
211     }else if(!strncmp(argv[i], "-noprdb", 6)){
212       warnx("Argument -noprdb is not implemented.");
213
214     }else if(!strncmp(argv[i], "-d", 6)){
215       debug = 1;
216
217     }else{
218       if(!strcmp(argv[i], ".") ||
219          !strcmp(argv[i], "..") ||
220          strchr(argv[i], '/')){
221         DEBUG("I guess that \"%s\" is a filename.", argv[i]);
222         if(k_afs_cell_of_file(argv[i], cellbuf, sizeof(cellbuf)))
223           errx (1, "No cell found for file \"%s\".", argv[i]);
224         else {
225           cell = cellbuf;
226           DEBUG("The file \"%s\" lives in cell \"%s\".", argv[i], cell);
227         }
228       }else{
229         cell = expand_cell_name(argv[i]);
230         DEBUG("I guess that %s is cell %s.", argv[i], cell);
231       }
232       do_aklog = 1;
233     }
234     if(do_aklog == 1){
235       do_aklog = 0;
236       if(krb_afslog(cell, realm))
237         errx (1, "Failed getting tokens for cell %s in realm %s.", 
238               cell?cell:"(local cell)", realm?realm:"(local realm)");
239     }
240     if(do_createuser == 1) {
241       do_createuser = 0;
242       if(createuser(cell))
243         errx (1, "Failed creating user in cell %s", cell?cell:"(local cell)");
244     }
245   }
246   if(do_aklog == -1 && do_createuser == -1 && krb_afslog(0, realm))
247     errx (1, "Failed getting tokens for cell %s in realm %s.", 
248           cell?cell:"(local cell)", realm?realm:"(local realm)");
249   return 0;
250 }