2 * Copyright (c) 1995, 1996, 1997 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
41 RCSID("$Id: krb4.c,v 1.19 1997/05/11 09:00:07 assar Exp $");
44 #ifdef HAVE_SYS_TYPES_H
45 #include <sys/types.h>
47 #ifdef HAVE_SYS_PARAM_H
48 #include <sys/param.h>
50 #ifdef HAVE_NETINET_IN_h
51 #include <netinet/in.h>
67 static AUTH_DAT auth_dat;
68 static des_key_schedule schedule;
70 int krb4_auth(char *auth)
73 reply(334, "Using authentication type %s; ADAT must follow", auth);
77 int krb4_adat(char *auth)
83 char msg[35]; /* size of encrypted block */
88 memset(&tkt, 0, sizeof(tkt));
89 len = base64_decode(auth, tkt.dat);
92 reply(501, "Failed to decode base64 data.");
97 k_getsockinst(0, inst, sizeof(inst));
98 kerror = krb_rd_req(&tkt, "ftp", inst, 0, &auth_dat, "");
99 if(kerror == RD_AP_UNDEC){
100 k_getsockinst(0, inst, sizeof(inst));
101 kerror = krb_rd_req(&tkt, "rcmd", inst, 0, &auth_dat, "");
105 reply(535, "Error reading request: %s.", krb_get_err_text(kerror));
109 des_set_key(&auth_dat.session, schedule);
111 cs = auth_dat.checksum + 1;
113 unsigned char tmp[4];
114 tmp[0] = (cs >> 24) & 0xff;
115 tmp[1] = (cs >> 16) & 0xff;
116 tmp[2] = (cs >> 8) & 0xff;
118 len = krb_mk_safe(tmp, msg, 4, &auth_dat.session,
119 &ctrl_addr, &his_addr);
122 reply(535, "Error creating reply: %s.", strerror(errno));
125 base64_encode(msg, len, &p);
126 reply(235, "ADAT=%s", p);
132 int krb4_pbsz(int size)
134 if(size > 1048576) /* XXX arbitrary number */
137 reply(200, "OK PBSZ=%d", buffer_size);
141 int krb4_prot(int level)
143 if(level == prot_confidential)
150 reply(534, "Don't event think about it.");
154 int krb4_mic(char *msg)
163 len = base64_decode(msg, cmd);
165 reply(501, "Failed to decode base 64 data.");
169 kerror = krb_rd_safe(cmd, len, &auth_dat.session,
170 &his_addr, &ctrl_addr, &m_data);
173 reply(535, "Error reading request: %s.", krb_get_err_text(kerror));
178 tmp = malloc(strlen(msg) + 1);
179 snprintf(tmp, strlen(msg) + 1, "%.*s", (int)m_data.app_length, m_data.app_data);
180 if(!strstr(tmp, "\r\n"))
182 new_ftp_command(tmp);
187 int krb4_conf(char *msg)
189 prot_level = prot_safe;
191 reply(537, "Protection level not supported.");
195 int krb4_enc(char *msg)
204 len = base64_decode(msg, cmd);
206 reply(501, "Failed to decode base 64 data.");
210 kerror = krb_rd_priv(cmd, len, schedule, &auth_dat.session,
211 &his_addr, &ctrl_addr, &m_data);
214 reply(535, "Error reading request: %s.", krb_get_err_text(kerror));
220 snprintf(tmp, strlen(msg) + 1, "%.*s", (int)m_data.app_length, m_data.app_data);
221 if(!strstr(tmp, "\r\n"))
223 new_ftp_command(tmp);
228 int krb4_read(int fd, void *data, int length)
233 int len, bytes, tx = 0;
238 if(eof){ /* if we haven't reported an end-of-file, do so */
248 memmove(data, extra, bytes);
251 memmove(extra, extra + bytes, left);
259 unsigned char tmp[4];
260 if(krb_net_read(fd, tmp, 4) < 4){
261 reply(400, "Unexpected end of file.\n");
264 len = (tmp[0] << 24) | (tmp[1] << 16) | (tmp[2] << 8) | tmp[3];
265 krb_net_read(fd, data_buffer, len);
266 if(data_protection == prot_safe)
267 kerror = krb_rd_safe(data_buffer, len, &auth_dat.session,
268 &his_addr, &ctrl_addr, &m_data);
270 kerror = krb_rd_priv(data_buffer, len, schedule, &auth_dat.session,
271 &his_addr, &ctrl_addr, &m_data);
274 reply(400, "Failed to read data: %s.", krb_get_err_text(kerror));
278 bytes = m_data.app_length;
284 left = bytes - length;
286 extra = malloc(left);
287 memmove(extra, m_data.app_data + bytes, left);
289 memmove((unsigned char*)data + tx, m_data.app_data, bytes);
296 int krb4_write(int fd, void *data, int length)
298 int len, bytes, tx = 0;
301 if(data_protection == prot_safe)
302 len -= 31; /* always 31 bytes overhead */
304 len -= 26; /* at most 26 bytes */
309 if(data_protection == prot_safe)
310 bytes = krb_mk_safe(data, data_buffer+4, len, &auth_dat.session,
311 &ctrl_addr, &his_addr);
313 bytes = krb_mk_priv(data, data_buffer+4, len, schedule,
315 &ctrl_addr, &his_addr);
317 reply(535, "Failed to make packet: %s.", strerror(errno));
320 data_buffer[0] = (bytes >> 24) & 0xff;
321 data_buffer[1] = (bytes >> 16) & 0xff;
322 data_buffer[2] = (bytes >> 8) & 0xff;
323 data_buffer[3] = bytes & 0xff;
324 if(krb_net_write(fd, data_buffer, bytes+4) < 0)
327 data = (unsigned char*)data + len;
333 int krb4_userok(char *name)
335 if(!kuserok(&auth_dat, name)){
338 reply(530, "User %s access denied.", name);
345 krb4_vprintf(const char *fmt, va_list ap)
353 vsnprintf (buf, sizeof(buf), fmt, ap);
354 enc = malloc(strlen(buf) + 31);
355 if(prot_level == prot_safe){
356 len = krb_mk_safe((u_char*)buf, (u_char*)enc, strlen(buf), &auth_dat.session,
357 &ctrl_addr, &his_addr);
359 }else if(prot_level == prot_private){
360 len = krb_mk_priv((u_char*)buf, (u_char*)enc, strlen(buf), schedule,
361 &auth_dat.session, &ctrl_addr, &his_addr);
367 base64_encode(enc, len, &p);
368 fprintf(stdout, "%d %s\r\n", code, p);