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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Copyright (c) 1985, 1989 Regents of the University of California.
* 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 University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University 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 REGENTS 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 REGENTS 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.
*/
#include "ftp_var.h"
#include <sys/types.h>
#include <gssapi/gssapi.h>
#include <gssapi/gssapi_ext.h>
int auth_type; /* Authentication succeeded? If so, what type? */
char *radix_error(int);
static void get_inet_addr_info(struct sockaddr_in6 *, gss_buffer_t);
static char *radixN =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static char radix_pad = '=';
/*
* authenticate the user, if auth_type is AUTHTYPE_NONE
*
* Returns: 0 if there is no auth type
* 1 if success
* 2 if failure
*/
gss_OID mechoid;
gss_ctx_id_t gcontext; /* global gss security context */
static const char *gss_trials[] = { "ftp", "host" };
/* the number of elements in gss_trials array */
static const int n_gss_trials = sizeof (gss_trials)/sizeof (char *);
char *reply_parse;
int
do_auth(void)
{
int oldverbose = verbose;
uchar_t *out_buf = NULL;
size_t outlen;
int i;
if (auth_type != AUTHTYPE_NONE)
return (1); /* auth already succeeded */
/* Other auth types go here ... */
if (command("AUTH %s", "GSSAPI") == CONTINUE) {
OM_uint32 maj_stat, min_stat;
gss_name_t target_name;
gss_buffer_desc send_tok, recv_tok, *token_ptr;
gss_buffer_desc temp_buf;
char stbuf[FTPBUFSIZ];
int comcode, trial;
int req_flags;
struct gss_channel_bindings_struct chan;
get_inet_addr_info(&myctladdr, &temp_buf);
chan.initiator_addrtype = GSS_C_AF_INET; /* OM_uint32 */
chan.initiator_address.length = temp_buf.length;
chan.initiator_address.value = malloc(temp_buf.length);
memcpy(chan.initiator_address.value, temp_buf.value,
temp_buf.length);
get_inet_addr_info(&remctladdr, &temp_buf);
chan.acceptor_addrtype = GSS_C_AF_INET; /* OM_uint32 */
chan.acceptor_address.length = temp_buf.length;
chan.acceptor_address.value = malloc(temp_buf.length);
memcpy(chan.acceptor_address.value, temp_buf.value,
temp_buf.length);
chan.application_data.length = 0;
chan.application_data.value = 0;
if (verbose)
(void) printf("GSSAPI accepted as authentication type\n");
/* set the forward flag */
req_flags = GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG;
if (fflag)
req_flags |= GSS_C_DELEG_FLAG;
/* blob from gss-client */
for (trial = 0; trial < n_gss_trials; trial++) {
/* ftp@hostname first, then host@hostname */
/* the V5 GSSAPI binding canonicalizes this for us... */
(void) snprintf(stbuf, FTPBUFSIZ, "%s@%s",
gss_trials[trial], hostname);
if (debug)
(void) fprintf(stderr,
"Trying to authenticate to <%s>\n", stbuf);
send_tok.value = stbuf;
send_tok.length = strlen(stbuf) + 1;
maj_stat = gss_import_name(&min_stat, &send_tok,
GSS_C_NT_HOSTBASED_SERVICE, &target_name);
if (maj_stat != GSS_S_COMPLETE) {
user_gss_error(maj_stat, min_stat, "parsing name");
(void) fprintf(stderr, "name parsed <%s>\n", stbuf);
continue;
}
token_ptr = GSS_C_NO_BUFFER;
gcontext = GSS_C_NO_CONTEXT; /* structure copy */
do {
if (debug)
(void) fprintf(stderr,
"calling gss_init_sec_context\n");
if (mechstr && !mechoid &&
__gss_mech_to_oid(mechstr, (gss_OID*)&mechoid) !=
GSS_S_COMPLETE)
(void) printf("do_auth: %s: not a valid "
"security mechanism\n", mechstr);
if (!mechoid)
mechoid = GSS_C_NULL_OID;
maj_stat = gss_init_sec_context(&min_stat,
GSS_C_NO_CREDENTIAL,
&gcontext,
target_name,
mechoid,
req_flags,
0,
&chan, /* channel bindings */
token_ptr,
NULL, /* ignore mech type */
&send_tok,
NULL, /* ignore ret_flags */
NULL); /* ignore time_rec */
if (maj_stat != GSS_S_COMPLETE &&
maj_stat != GSS_S_CONTINUE_NEEDED) {
/* return an error if this is NOT the ftp ticket */
if (strcmp(gss_trials[trial], "ftp"))
user_gss_error(maj_stat, min_stat,
"initializing context");
(void) gss_release_name(&min_stat, &target_name);
/* could just be that we missed on the service name */
goto outer_loop;
}
if (send_tok.length != 0) {
int len = send_tok.length;
reply_parse = "ADAT="; /* for command() later */
oldverbose = verbose;
verbose = (trial == n_gss_trials-1)?0:-1;
outlen = ENCODELEN(send_tok.length);
out_buf = (uchar_t *)malloc(outlen);
if (out_buf == NULL) {
(void) fprintf(stderr, "memory error allocating "
"auth buffer\n");
maj_stat = GSS_S_FAILURE;
goto outer_loop;
}
auth_error = radix_encode(send_tok.value, out_buf,
outlen, &len, 0);
if (auth_error) {
(void) fprintf(stderr, "Base 64 encoding failed: %s\n",
radix_error(auth_error));
} else if ((comcode = command("ADAT %s", out_buf))
!= COMPLETE /* && comcode != 3 (335)*/) {
if (trial == n_gss_trials-1) {
(void) fprintf(stderr, "GSSAPI ADAT failed (%d)\n",
comcode);
/* force out of loop */
maj_stat = GSS_S_FAILURE;
}
/*
* backoff to the v1 gssapi is still possible.
* Send a new AUTH command. If that fails,
* terminate the loop
*/
if (command("AUTH %s", "GSSAPI") != CONTINUE) {
(void) fprintf(stderr,
"GSSAPI ADAT failed, AUTH restart failed\n");
/* force out of loop */
maj_stat = GSS_S_FAILURE;
}
goto outer_loop;
} else if (!reply_parse) {
(void) fprintf(stderr,
"No authentication data received from server\n");
if (maj_stat == GSS_S_COMPLETE) {
(void) fprintf(stderr,
"...but no more was needed\n");
goto gss_complete_loop;
} else {
user_gss_error(maj_stat, min_stat, "no reply.");
goto gss_complete_loop;
}
} else if (auth_error = radix_encode((uchar_t *)
reply_parse, out_buf, outlen, &i, 1)) {
(void) fprintf(stderr,
"Base 64 decoding failed: %s\n",
radix_error(auth_error));
} else {
/* everything worked */
token_ptr = &recv_tok;
recv_tok.value = out_buf;
recv_tok.length = i;
continue;
} /* end if (auth_error) */
/* get out of loop clean */
gss_complete_loop:
trial = n_gss_trials-1;
gss_release_buffer(&min_stat, &send_tok);
gss_release_name(&min_stat, &target_name);
goto outer_loop;
} /* end if (send_tok.length != 0) */
} while (maj_stat == GSS_S_CONTINUE_NEEDED);
outer_loop:
if (maj_stat == GSS_S_COMPLETE)
break;
} /* end for loop */
verbose = oldverbose;
if (out_buf != NULL)
free(out_buf);
if (maj_stat == GSS_S_COMPLETE) {
(void) printf("GSSAPI authentication succeeded\n");
reply_parse = NULL;
auth_type = AUTHTYPE_GSSAPI;
return (1);
} else {
(void) fprintf(stderr, "GSSAPI authentication failed\n");
reply_parse = NULL;
}
} /* end if (command...) */
/* Other auth types go here ... */
return (0);
}
/*
* Get the information for the channel structure.
*/
void
get_inet_addr_info(struct sockaddr_in6 *in_ipaddr, gss_buffer_t in_buffer)
{
size_t length;
char *value;
if (in_ipaddr == NULL) {
in_buffer->length = 0;
in_buffer->value = NULL;
return;
}
/* get the initiator address.value and address.length */
if (in_ipaddr->sin6_family == AF_INET6) {
struct in_addr in_ipv4addr;
struct sockaddr_in6 *sin6 =
(struct sockaddr_in6 *)in_ipaddr;
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
IN6_V4MAPPED_TO_INADDR(&sin6->sin6_addr,
&in_ipv4addr);
in_buffer->length = length = sizeof (struct in_addr);
in_buffer->value = value = malloc(length);
memcpy(value, &in_ipv4addr, length);
} else {
in_buffer->length = length = sizeof (struct in6_addr);
in_buffer->value = value = malloc(length);
memcpy(value, &(sin6->sin6_addr.s6_addr),
length);
}
} else {
in_buffer->length = length = sizeof (struct in_addr);
in_buffer->value = value = malloc(in_buffer->length);
memcpy(value,
&((struct sockaddr_in *)(in_ipaddr))->sin_addr,
length);
}
}
int
radix_encode(uchar_t *inbuf, uchar_t *outbuf, size_t buflen,
int *outlen, int decode)
{
int i, j, D;
char *p;
uchar_t c;
if (decode) {
for (i = j = 0;
inbuf[i] && inbuf[i] != radix_pad && (j < buflen);
i++) {
if ((p = strchr(radixN, inbuf[i])) == NULL)
return (1);
D = p - radixN;
switch (i&3) {
case 0:
outbuf[j] = D<<2;
break;
case 1:
outbuf[j++] |= D>>4;
outbuf[j] = (D&15)<<4;
break;
case 2:
outbuf[j++] |= D>>2;
outbuf[j] = (D&3)<<6;
break;
case 3:
outbuf[j++] |= D;
}
}
if (j == buflen && (inbuf[i] && inbuf[i] != radix_pad)) {
return (4);
}
switch (i&3) {
case 1: return (3);
case 2: if (D&15)
return (3);
if (strcmp((char *)&inbuf[i], "=="))
return (2);
break;
case 3: if (D&3)
return (3);
if (strcmp((char *)&inbuf[i], "="))
return (2);
}
*outlen = j;
} else {
for (i = j = 0; i < *outlen && j < buflen; i++)
switch (i%3) {
case 0:
outbuf[j++] = radixN[inbuf[i]>>2];
c = (inbuf[i]&3)<<4;
break;
case 1:
outbuf[j++] = radixN[c|inbuf[i]>>4];
c = (inbuf[i]&15)<<2;
break;
case 2:
outbuf[j++] = radixN[c|inbuf[i]>>6];
outbuf[j++] = radixN[inbuf[i]&63];
c = 0;
}
if (j == buflen && i < *outlen) {
return (4);
}
if (i%3)
outbuf[j++] = radixN[c];
switch (i%3) {
case 1:
outbuf[j++] = radix_pad;
/* FALLTHROUGH */
case 2:
outbuf[j++] = radix_pad;
break;
}
outbuf[*outlen = j] = '\0';
}
return (0);
}
char *
radix_error(int e)
{
switch (e) {
case 0: return ("Success");
case 1: return ("Bad character in encoding");
case 2: return ("Encoding not properly padded");
case 3: return ("Decoded # of bits not a multiple of 8");
case 4: return ("Buffer size error");
default: return ("Unknown error");
}
}
|