summaryrefslogtreecommitdiffstats
path: root/crypto/kerberosIV/appl/ftp/ftpd/krb4.c
blob: 2457c61cc1fed07f2918ddc54575fe8a1fc523c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Copyright (c) 1995, 1996, 1997 Kungliga Tekniska Högskolan
 * (Royal Institute of Technology, Stockholm, Sweden).
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the Kungliga Tekniska
 *      Högskolan and its contributors.
 * 
 * 4. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
RCSID("$Id: krb4.c,v 1.19 1997/05/11 09:00:07 assar Exp $");
#endif

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_NETINET_IN_h
#include <netinet/in.h>
#endif

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <krb.h>

#include "base64.h"
#include "extern.h"
#include "auth.h"
#include "krb4.h"

#include <roken.h>

static AUTH_DAT auth_dat;
static des_key_schedule schedule;

int krb4_auth(char *auth)
{
    auth_complete = 0;
    reply(334, "Using authentication type %s; ADAT must follow", auth);
    return 0;
}

int krb4_adat(char *auth)
{
    KTEXT_ST tkt;
    char *p;
    int kerror;
    u_int32_t cs;
    char msg[35]; /* size of encrypted block */
    int len;

    char inst[INST_SZ];

    memset(&tkt, 0, sizeof(tkt));
    len = base64_decode(auth, tkt.dat);

    if(len < 0){
	reply(501, "Failed to decode base64 data.");
	return -1;
    }
    tkt.length = len;

    k_getsockinst(0, inst, sizeof(inst));
    kerror = krb_rd_req(&tkt, "ftp", inst, 0, &auth_dat, "");
    if(kerror == RD_AP_UNDEC){
	k_getsockinst(0, inst, sizeof(inst));
	kerror = krb_rd_req(&tkt, "rcmd", inst, 0, &auth_dat, "");
    }

    if(kerror){
	reply(535, "Error reading request: %s.", krb_get_err_text(kerror));
	return -1;
    }
  
    des_set_key(&auth_dat.session, schedule);

    cs = auth_dat.checksum + 1;
    {
	unsigned char tmp[4];
	tmp[0] = (cs >> 24) & 0xff;
	tmp[1] = (cs >> 16) & 0xff;
	tmp[2] = (cs >> 8) & 0xff;
	tmp[3] = cs & 0xff;
	len = krb_mk_safe(tmp, msg, 4, &auth_dat.session, 
			  &ctrl_addr, &his_addr);
    }
    if(len < 0){
	reply(535, "Error creating reply: %s.", strerror(errno));
	return -1;
    }
    base64_encode(msg, len, &p);
    reply(235, "ADAT=%s", p);
    auth_complete = 1;
    free(p);
    return 0;
}

int krb4_pbsz(int size)
{
    if(size > 1048576) /* XXX arbitrary number */
	size = 1048576;
    buffer_size = size;
    reply(200, "OK PBSZ=%d", buffer_size);
    return 0;
}

int krb4_prot(int level)
{
    if(level == prot_confidential)
	return -1;
    return 0;
}

int krb4_ccc(void)
{
    reply(534, "Don't event think about it.");
    return -1;
}

int krb4_mic(char *msg)
{
    int len;
    int kerror;
    MSG_DAT m_data;
    char *tmp, *cmd;
  
    cmd = strdup(msg);
    
    len = base64_decode(msg, cmd);
    if(len < 0){
	reply(501, "Failed to decode base 64 data.");
	free(cmd);
	return -1;
    }
    kerror = krb_rd_safe(cmd, len, &auth_dat.session, 
			 &his_addr, &ctrl_addr, &m_data);

    if(kerror){
	reply(535, "Error reading request: %s.", krb_get_err_text(kerror));
	free(cmd);
	return -1;
    }
    
    tmp = malloc(strlen(msg) + 1);
    snprintf(tmp, strlen(msg) + 1, "%.*s", (int)m_data.app_length, m_data.app_data);
    if(!strstr(tmp, "\r\n"))
	strcat(tmp, "\r\n");
    new_ftp_command(tmp);
    free(cmd);
    return 0;
}

int krb4_conf(char *msg)
{
    prot_level = prot_safe;

    reply(537, "Protection level not supported.");
    return -1;
}

int krb4_enc(char *msg)
{
    int len;
    int kerror;
    MSG_DAT m_data;
    char *tmp, *cmd;
  
    cmd = strdup(msg);
    
    len = base64_decode(msg, cmd);
    if(len < 0){
	reply(501, "Failed to decode base 64 data.");
	free(cmd);
	return -1;
    }
    kerror = krb_rd_priv(cmd, len, schedule, &auth_dat.session, 
			 &his_addr, &ctrl_addr, &m_data);

    if(kerror){
	reply(535, "Error reading request: %s.", krb_get_err_text(kerror));
	free(cmd);
	return -1;
    }
    
    tmp = strdup(msg);
    snprintf(tmp, strlen(msg) + 1, "%.*s", (int)m_data.app_length, m_data.app_data);
    if(!strstr(tmp, "\r\n"))
	strcat(tmp, "\r\n");
    new_ftp_command(tmp);
    free(cmd);
    return 0;
}

int krb4_read(int fd, void *data, int length)
{
    static int left;
    static char *extra;
    static int eof;
    int len, bytes, tx = 0;
    
    MSG_DAT m_data;
    int kerror;

    if(eof){ /* if we haven't reported an end-of-file, do so */
	eof = 0;
	return 0;
    }
    
    if(left){
	if(length > left)
	    bytes = left;
	else
	    bytes = length;
	memmove(data, extra, bytes);
	left -= bytes;
	if(left)
	    memmove(extra, extra + bytes, left);
	else
	    free(extra);
	length -= bytes;
	tx += bytes;
    }

    while(length){
	unsigned char tmp[4];
	if(krb_net_read(fd, tmp, 4) < 4){
	    reply(400, "Unexpected end of file.\n");
	    return -1;
	}
	len = (tmp[0] << 24) | (tmp[1] << 16) | (tmp[2] << 8) | tmp[3];
	krb_net_read(fd, data_buffer, len);
	if(data_protection == prot_safe)
	    kerror = krb_rd_safe(data_buffer, len, &auth_dat.session, 
				 &his_addr, &ctrl_addr, &m_data);
	else
	    kerror = krb_rd_priv(data_buffer, len, schedule, &auth_dat.session,
				 &his_addr, &ctrl_addr, &m_data);
	
	if(kerror){
	    reply(400, "Failed to read data: %s.", krb_get_err_text(kerror));
	    return -1;
	}
	
	bytes = m_data.app_length;
	if(bytes == 0){
	    if(tx) eof = 1;
	    return  tx;
	}
	if(bytes > length){
	    left = bytes - length;
	    bytes = length;
	    extra = malloc(left);
	    memmove(extra, m_data.app_data + bytes, left);
	}
	memmove((unsigned char*)data + tx, m_data.app_data, bytes);
	tx += bytes;
	length -= bytes;
    }
    return tx;
}

int krb4_write(int fd, void *data, int length)
{
    int len, bytes, tx = 0;

    len = buffer_size;
    if(data_protection == prot_safe)
	len -= 31; /* always 31 bytes overhead */
    else
	len -= 26; /* at most 26 bytes */
    
    do{
	if(length < len)
	    len = length;
	if(data_protection == prot_safe)
	    bytes = krb_mk_safe(data, data_buffer+4, len, &auth_dat.session,
				&ctrl_addr, &his_addr);
	else
	    bytes = krb_mk_priv(data, data_buffer+4, len, schedule, 
				&auth_dat.session,
				&ctrl_addr, &his_addr);
	if(bytes == -1){
	    reply(535, "Failed to make packet: %s.", strerror(errno));
	    return -1;
	}
	data_buffer[0] = (bytes >> 24) & 0xff;
	data_buffer[1] = (bytes >> 16) & 0xff;
	data_buffer[2] = (bytes >> 8) & 0xff;
	data_buffer[3] = bytes & 0xff;
	if(krb_net_write(fd, data_buffer, bytes+4) < 0)
	    return -1;
	length -= len;
	data = (unsigned char*)data + len;
	tx += len;
    }while(length);
    return tx;
}

int krb4_userok(char *name)
{
    if(!kuserok(&auth_dat, name)){
	do_login(232, name);
    }else{
	reply(530, "User %s access denied.", name);
    }
    return 0;
}


int
krb4_vprintf(const char *fmt, va_list ap)
{
    char buf[10240];
    char *p;
    char *enc;
    int code;
    int len;
  
    vsnprintf (buf, sizeof(buf), fmt, ap);
    enc = malloc(strlen(buf) + 31);
    if(prot_level == prot_safe){
	len = krb_mk_safe((u_char*)buf, (u_char*)enc, strlen(buf), &auth_dat.session, 
			  &ctrl_addr, &his_addr); 
	code = 631;
    }else if(prot_level == prot_private){
	len = krb_mk_priv((u_char*)buf, (u_char*)enc, strlen(buf), schedule, 
			  &auth_dat.session, &ctrl_addr, &his_addr); 
	code = 632;
    }else{
	len = 0; /* XXX */
	code = 631;
    }
    base64_encode(enc, len, &p);
    fprintf(stdout, "%d %s\r\n", code, p);
    free(enc);
    free(p);
    return 0;
}
OpenPOWER on IntegriCloud