check for innetgr
[heimdal.git] / appl / ftp / ftp / kauth.c
blob1295d8cd1617e375d2104a1b9124ea8eedaf75ca
1 /*
2 * Copyright (c) 1995, 1996, 1997 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.
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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the Kungliga Tekniska
20 * Högskolan and its contributors.
22 * 4. Neither the name of the Institute nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
39 #include "ftp_locl.h"
40 RCSID("$Id$");
42 void
43 kauth(int argc, char **argv)
45 int ret;
46 char buf[1024];
47 des_cblock key;
48 des_key_schedule schedule;
49 KTEXT_ST tkt, tktcopy;
50 char *name;
51 char *p;
52 int overbose;
53 char passwd[100];
54 int tmp;
56 if(argc > 2){
57 printf("usage: %s [principal]\n", argv[0]);
58 code = -1;
59 return;
61 if(argc == 2)
62 name = argv[1];
63 else
64 name = username;
66 overbose = verbose;
67 verbose = 0;
69 ret = command("SITE KAUTH %s", name);
70 if(ret != CONTINUE){
71 verbose = overbose;
72 code = -1;
73 return;
75 verbose = overbose;
76 p = strstr(reply_string, "T=");
77 if(!p){
78 printf("Bad reply from server.\n");
79 code = -1;
80 return;
82 p += 2;
83 tmp = base64_decode(p, &tkt.dat);
84 if(tmp < 0){
85 printf("Failed to decode base64 in reply.\n");
86 code = -1;
87 return;
89 tkt.length = tmp;
90 tktcopy.length = tkt.length;
92 p = strstr(reply_string, "P=");
93 if(!p){
94 printf("Bad reply from server.\n");
95 verbose = overbose;
96 code = -1;
97 return;
99 name = p + 2;
100 for(; *p && *p != ' ' && *p != '\r' && *p != '\n'; p++);
101 *p = 0;
103 snprintf(buf, sizeof(buf), "Password for %s:", name);
104 if (des_read_pw_string (passwd, sizeof(passwd)-1, buf, 0))
105 *passwd = '\0';
106 des_string_to_key (passwd, &key);
108 des_key_sched(&key, schedule);
110 des_pcbc_encrypt((des_cblock*)tkt.dat, (des_cblock*)tktcopy.dat,
111 tkt.length,
112 schedule, &key, DES_DECRYPT);
113 if (strcmp ((char*)tktcopy.dat + 8,
114 KRB_TICKET_GRANTING_TICKET) != 0) {
115 afs_string_to_key (passwd, krb_realmofhost(hostname), &key);
116 des_key_sched (&key, schedule);
117 des_pcbc_encrypt((des_cblock*)tkt.dat, (des_cblock*)tktcopy.dat,
118 tkt.length,
119 schedule, &key, DES_DECRYPT);
121 memset(key, 0, sizeof(key));
122 memset(schedule, 0, sizeof(schedule));
123 memset(passwd, 0, sizeof(passwd));
124 if(base64_encode(tktcopy.dat, tktcopy.length, &p) < 0) {
125 printf("Out of memory base64-encoding.\n");
126 code = -1;
127 return;
129 memset (tktcopy.dat, 0, tktcopy.length);
130 ret = command("SITE KAUTH %s %s", name, p);
131 free(p);
132 if(ret != COMPLETE){
133 code = -1;
134 return;
136 code = 0;
139 void
140 klist(int argc, char **argv)
142 int ret;
143 if(argc != 1){
144 printf("usage: %s\n", argv[0]);
145 code = -1;
146 return;
149 ret = command("SITE KLIST");
150 code = (ret == COMPLETE);
153 void
154 kdestroy(int argc, char **argv)
156 int ret;
157 if (argc != 1) {
158 printf("usage: %s\n", argv[0]);
159 code = -1;
160 return;
162 ret = command("SITE KDESTROY");
163 code = (ret == COMPLETE);
166 void
167 krbtkfile(int argc, char **argv)
169 int ret;
170 if(argc != 2) {
171 printf("usage: %s tktfile\n", argv[0]);
172 code = -1;
173 return;
175 ret = command("SITE KRBTKFILE %s", argv[1]);
176 code = (ret == COMPLETE);
179 void
180 afslog(int argc, char **argv)
182 int ret;
183 if(argc > 2) {
184 printf("usage: %s [cell]\n", argv[0]);
185 code = -1;
186 return;
188 if(argc == 2)
189 ret = command("SITE AFSLOG %s", argv[1]);
190 else
191 ret = command("SITE AFSLOG");
192 code = (ret == COMPLETE);