summaryrefslogtreecommitdiff
blob: 2d45b7b1ff9537eeab8754b0be31d6f813580f9b (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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
--- netkit-ftp-0.17/configure
+++ netkit-ftp-0.17+ssl-0.2/configure
@@ -26,6 +26,7 @@
     --binmode=mode        Mode for binaries [755]
     --manmode=mode        Mode for manual pages [644]
     --with-c-compiler=cc  Program for compiling C source [guessed]
+    --enable-ssl          Enable SSL support
 EOF
 	exit 0;;
 	--verbose) ;;
@@ -39,6 +40,8 @@
 	--manmode=*) MANMODE=`echo $1 | sed 's/^[^=]*=//'` ;;
 	--with-c-compiler=*) CC=`echo $1 | sed 's/^[^=]*=//'` ;;
 	--without-readline|--disable-readline) WITHOUT_READLINE=1;;
+	--disable-ssl) ENABLE_SSL=no;;
+	--enable-ssl) ENABLE_SSL=yes;;
 	*) echo "Unrecognized option: $1"; exit 1;;
 esac 
 shift
@@ -152,3 +155,15 @@
 rm -f __conftest*
 
+##################################################
+## Enable SSL
+echo -n "Whether to enable SSL support... "
+if [ x"$ENABLE_SSL" = x"yes" ]; then
+	echo yes
+	CFLAGS="$CFLAGS -DUSE_SSL"
+	## we should do tests here, but screw it, i'm lazy :P
+	LIBS="$LIBS -lssl -lcrypto"
+else
+	echo no
+fi
+
 ##################################################
--- netkit-ftp-0.17/ftp/cmds.c
+++ netkit-ftp-0.17+ssl-0.2/ftp/cmds.c
@@ -207,6 +207,32 @@
 	}
 	port = ftp_port;
 	if (argc > 2) {
+#ifdef USE_SSL
+		/* not really an SSL enhancement but something that
+		 * should have always been here --tjh
+		 */
+		if (!isdigit(argv[2][0])) {
+		    struct        servent *destsp;
+
+		    destsp = getservbyname(argv[2], "tcp");
+
+		    /* if lookup fails for ssl-ftp we fallback to
+		     * the default (unofficial) port number
+		     */
+		    if ((strcmp(argv[2],"ssl-ftp")==0) && (destsp==NULL))
+		        port = 150;
+		    else {
+			if (destsp == NULL ) {
+			    printf("%s: bad port name-- %s\n",argv[1],argv[2]);
+			    printf ("usage: %s host-name [port]\n",argv[0]);
+			    code = -1;
+			    return;
+			} else {
+			    port = ntohs(destsp->s_port);
+			}
+		    }
+		} else 
+#endif /* USE_SSL */
 		port = atoi(argv[2]);
 		if (port < 1) {
 			printf("%s: bad port number-- %s\n", argv[1], argv[2]);
--- netkit-ftp-0.17/ftp/ftp.1
+++ netkit-ftp-0.17+ssl-0.2/ftp/ftp.1
@@ -97,6 +97,52 @@
 as report on data transfer statistics.
 .It Fl d
 Enables debugging.
+.It Fl z Ar option
+Set SSL (Secure Socket Layer) parameters. The default is to negotiate
+via ftp protocoll if SSL is availlable at server side and then to
+switch it on. In this mode you can connect to both conventional and
+SSL enhanced ftpd's.
+.Pp
+The SSL parameters are:
+.Bl -tag -width Fl
+.It Ic Ar debug
+Send SSL related debugging information to stderr.
+.It Ic Ar authdebug
+Enable authentication debugging.
+.It Ic Ar ssl
+Negotiate SSL at first, then use ftp protocol. ftp protocol
+negotiation goes encrypted. (Not yet implemented)
+.It Ic Ar nossl, Ar !ssl
+switch of SSL negotiation
+.It Ic Ar certrequired
+client certificate is mandatory
+.It Ic Ar secure
+Don't switch back to unencrypted mode (no SSL) if SSL is not available.
+.It Ic Ar verbose
+Be verbose about certificates etc.
+.It Ic Ar verify=int
+.\" TODO
+Set the SSL verify flags (SSL_VERIFY_* in 
+.Ar ssl/ssl.h
+).
+.\" TODO
+.It Ic Ar cert=cert_file
+.\" TODO
+Use the certificate(s) in
+.Ar cert_file .
+.It Ic Ar key=key_file
+.\" TODO
+Use the key(s) in
+.Ar key_file .
+.It Ic Ar cipher=ciph_list
+.\" TODO
+Set the preferred ciphers to
+.Ar ciph_list .
+.\" TODO: possible values; comma-separated list?
+(See 
+.Ar ssl/ssl.h
+).
+.El
 .El
 .Pp
 The client host with which
--- netkit-ftp-0.17/ftp/ftp.c
+++ netkit-ftp-0.17+ssl-0.2/ftp/ftp.c
@@ -1,3 +1,15 @@
+/* 
+ * The modifications to support SSLeay were done by Tim Hudson
+ * tjh@cryptsoft.com
+ *
+ * You can do whatever you like with these patches except pretend that
+ * you wrote them. 
+ *
+ * Email ssl-users-request@lists.cryptsoft.com to get instructions on how to
+ * join the mailing list that discusses SSLeay and also these patches.
+ *
+ */
+
 /*
  * Copyright (c) 1985, 1989 Regents of the University of California.
  * All rights reserved.
@@ -77,6 +89,17 @@
 static sigjmp_buf ptabort;
 static int ptabflg = 0;
 static int abrtflag = 0;
+static int
+auth_user(char *u,char *p);
+#ifdef USE_SSL
+static int	pdata = -1;
+static int 
+ssl_getc(SSL *ssl_con);
+static int
+ssl_putc_flush(SSL *ssl_con);
+static int
+ssl_putc(SSL *ssl_con, int oneint);
+#endif
 
 void lostpeer(int);
 extern int connected;
@@ -243,14 +266,7 @@
 		else
 			luser = tmp;
 	}
-	n = command("USER %s", luser);
-	if (n == CONTINUE) {
-		if (pass == NULL) {
-			/* fflush(stdout); */
-			pass = getpass("Password:");
-		}
-		n = command("PASS %s", pass);
-	}
+	n = auth_user(luser,pass);
 	if (n == CONTINUE) {
 		aflag++;
 		/* fflush(stdout); */
@@ -296,6 +312,9 @@
 	va_list ap;
 	int r;
 	void (*oldintr)(int);
+#ifdef USE_SSL
+        char outputbuf[8192];
+#endif /* USE_SSL */
 
 	abrtflag = 0;
 	if (debug) {
@@ -316,10 +335,27 @@
 	}
 	oldintr = signal(SIGINT, cmdabort);
 	va_start(ap, fmt);
+#ifdef USE_SSL
+        /* assemble the output into a buffer */
+	vsnprintf(outputbuf,sizeof(outputbuf),fmt,ap);
+	strcat(outputbuf,"\r\n");
+	if (ssl_active_flag) {
+	    SSL_write(ssl_con,outputbuf,strlen(outputbuf));
+        } else {
+	    fprintf(cout,"%s",outputbuf);
+	    fflush(cout);
+	}
+#else /* !USE_SSL */
 	vfprintf(cout, fmt, ap);
+#endif /* USE_SSL */
 	va_end(ap);
+
+#ifndef USE_SSL
+        /* we don't need this as we concatenated it above */
 	fprintf(cout, "\r\n");
 	(void) fflush(cout);
+#endif /* !USE_SSL */
+
 	cpend = 1;
 	r = getreply(!strcmp(fmt, "QUIT"));
 	if (abrtflag && oldintr != SIG_IGN)
@@ -343,25 +379,39 @@
 	int pflag = 0;
 	size_t px = 0;
 	size_t psize = sizeof(pasv);
+	char buf[16];
 
 	oldintr = signal(SIGINT, cmdabort);
 	for (;;) {
 		dig = n = code = 0;
 		cp = reply_string;
-		while ((c = getc(cin)) != '\n') {
+		while ((c = GETC(cin)) != '\n') {
 			if (c == IAC) {     /* handle telnet commands */
-				switch (c = getc(cin)) {
+				switch (c = GETC(cin)) {
 				case WILL:
 				case WONT:
-					c = getc(cin);
-					fprintf(cout, "%c%c%c", IAC, DONT, c);
-					(void) fflush(cout);
+					c = GETC(cin);
+					sprintf(buf,
+					        "%c%c%c", IAC, DONT, c);
+#ifdef USE_SSL
+					if (ssl_active_flag)
+						SSL_write(ssl_con,buf,3);
+					else
+#endif /* !USE_SSL */
+						fwrite(buf,3,1,cout);
+					(void) FFLUSH(cout);
 					break;
 				case DO:
 				case DONT:
-					c = getc(cin);
-					fprintf(cout, "%c%c%c", IAC, WONT, c);
-					(void) fflush(cout);
+					c = GETC(cin);
+					sprintf(buf, "%c%c%c", IAC, WONT, c);
+#ifdef USE_SSL
+					if (ssl_active_flag)
+						SSL_write(ssl_con,buf,3);
+					else
+#endif /* !USE_SSL */
+						fwrite(buf,3,1,cout);
+					(void) FFLUSH(cout);
 					break;
 				default:
 					break;
@@ -600,9 +650,18 @@
 		errno = d = 0;
 		while ((c = read(fileno(fin), buf, sizeof (buf))) > 0) {
 			bytes += c;
+#ifdef USE_SSL
+                        if (ssl_data_active_flag) {
+			    for (bufp = buf; c > 0; c -= d, bufp += d)
+				if ((d = SSL_write(ssl_data_con, bufp, c)) <= 0)
+					break;
+			} else 
+#endif /* !USE_SSL */
+                        {
 			for (bufp = buf; c > 0; c -= d, bufp += d)
 				if ((d = write(fileno(dout), bufp, c)) <= 0)
 					break;
+			}
 			if (hash) {
 				while (bytes >= hashbytes) {
 					(void) putchar('#');
@@ -654,16 +713,17 @@
 				}
 				if (ferror(dout))
 					break;
-				(void) putc('\r', dout);
+				(void) DATAPUTC('\r', dout);
 				bytes++;
 			}
-			(void) putc(c, dout);
+			(void) DATAPUTC(c, dout);
 			bytes++;
 	/*		if (c == '\r') {			  	*/
 	/*		(void)	putc('\0', dout);  (* this violates rfc */
 	/*			bytes++;				*/
 	/*		}                          			*/     
 		}
+		DATAFLUSH(dout);
 		if (hash) {
 			if (bytes < hashbytes)
 				(void) putchar('#');
@@ -688,6 +748,15 @@
 	if (closefunc != NULL)
 		(*closefunc)(fin);
 	(void) fclose(dout);
+
+#ifdef USE_SSL
+	if (ssl_data_active_flag && (ssl_data_con!=NULL)) {
+	    SSL_free(ssl_data_con);
+	    ssl_data_active_flag=0;
+	    ssl_data_con=NULL;
+	}
+#endif /* USE_SSL */
+
 	/* closes data as well, so discard it */
 	data = -1;
 	(void) getreply(0);
@@ -714,6 +783,15 @@
 		(void) close(data);
 		data = -1;
 	}
+
+#ifdef USE_SSL
+	if (ssl_data_active_flag && (ssl_data_con!=NULL)) {
+	    SSL_free(ssl_data_con);
+	    ssl_data_active_flag=0;
+	    ssl_data_con=NULL;
+	}
+#endif /* USE_SSL */
+
 	(void) getreply(0);
 	code = -1;
 	if (closefunc != NULL && fin != NULL)
@@ -908,6 +986,33 @@
 			return;
 		}
 		errno = d = 0;
+#ifdef USE_SSL
+                if (ssl_data_active_flag) {
+		    while ((c = SSL_read(ssl_data_con, buf, bufsize)) > 0) {
+			    if ((d = write(fileno(fout), buf, c)) != c)
+				    break;
+			    bytes += c;
+			    if (hash) {
+				    while (bytes >= hashbytes) {
+					    (void) putchar('#');
+					    hashbytes += HASHBYTES;
+				    }
+				    (void) fflush(stdout);
+			    }
+		    }
+		    if ( c < -1 ) {
+			static char errbuf[1024];
+
+			sprintf(errbuf,"ftp: SSL_read DATA error %s\n",
+				    ERR_error_string(ERR_get_error(),NULL));
+
+			/* tell the user ... who else */
+			fprintf(stderr,"%s", errbuf);
+			fflush(stderr);
+		    }
+		} else 
+#endif /* !USE_SSL */
+                {
 		while ((c = read(fileno(din), buf, bufsize)) > 0) {
 			if ((d = write(fileno(fout), buf, c)) != c)
 				break;
@@ -927,6 +1032,7 @@
 					hashbytes += TICKBYTES;
 			}
 		}
+		}
 		if (hash && bytes > 0) {
 			if (bytes < HASHBYTES)
 				(void) putchar('#');
@@ -973,7 +1079,7 @@
 				return;
 			}
 		}
-		while ((c = getc(din)) != EOF) {
+		while ((c = DATAGETC(din)) != EOF) {
 			if (c == '\n')
 				bare_lfs++;
 			while (c == '\r') {
@@ -991,7 +1097,7 @@
 						hashbytes += TICKBYTES;
 				}
 				bytes++;
-				if ((c = getc(din)) != '\n' || tcrflag) {
+				if ((c = DATAGETC(din)) != '\n' || tcrflag) {
 					if (ferror(fout))
 						goto break2;
 					(void) putc('\r', fout);
@@ -1039,6 +1145,15 @@
 		(void) signal(SIGPIPE, oldintp);
 	(void) gettimeofday(&stop, (struct timezone *)0);
 	(void) fclose(din);
+
+#ifdef USE_SSL
+	if (ssl_data_active_flag && (ssl_data_con!=NULL)) {
+	    SSL_free(ssl_data_con);
+	    ssl_data_active_flag=0;
+	    ssl_data_con=NULL;
+	}
+#endif /* USE_SSL */
+
 	/* closes data as well, so discard it */
 	data = -1;
 	(void) getreply(0);
@@ -1071,6 +1186,15 @@
 		(void) close(data);
 		data = -1;
 	}
+
+#ifdef USE_SSL
+	if (ssl_data_active_flag && (ssl_data_con!=NULL)) {
+	    SSL_free(ssl_data_con);
+	    ssl_data_active_flag=0;
+	    ssl_data_con=NULL;
+	}
+#endif /* USE_SSL */
+
 	if (bytes > 0)
 		ptransfer("received", bytes, &start, &stop);
 	(void) signal(SIGINT, oldintr);
@@ -1207,6 +1331,7 @@
 	struct sockaddr_in from;
 	int s, tos;
 	socklen_t fromlen = sizeof(from);
+	int ret;
 
         if (passivemode)
             return (fdopen(data, lmode));
@@ -1224,6 +1349,67 @@
 	if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
 		perror("ftp: setsockopt TOS (ignored)");
 #endif
+
+#ifdef USE_SSL
+	ssl_data_active_flag=0;
+	if (ssl_active_flag && ssl_encrypt_data) {
+	    /* do SSL */
+	    if (ssl_data_con!=NULL) {
+	      SSL_free(ssl_data_con);
+	      ssl_data_con=NULL;
+	    }
+	    ssl_data_con=(SSL *)SSL_new(ssl_ctx);
+
+	    SSL_set_fd(ssl_data_con,data);
+	    set_ssl_trace(ssl_data_con);
+
+	    SSL_set_verify(ssl_data_con,ssl_verify_flag,NULL);
+
+	    /* this is the "magic" call that makes 
+	     * this quick assuming Eric has this going
+	     * okay! ;-)
+	     */
+	    SSL_copy_session_id(ssl_data_con,ssl_con);
+
+	    /* we are doing I/O and not using select so 
+	     * it is "safe" to read ahead
+	     */
+	    /* SSL_set_read_ahead(ssl_data_con,1); */
+
+	    if (debug) {
+		fprintf(stderr,"===>START SSL connect on DATA\n");
+		fflush(stderr);
+	    }
+
+	    if ((ret=SSL_connect(ssl_data_con))<=0) {
+                static char errbuf[1024];
+
+	        sprintf(errbuf,"ftp: SSL_connect DATA error %d - %s\n",
+			    ret,ERR_error_string(ERR_get_error(),NULL));
+
+		/* tell the user ... who else */
+		fprintf(stderr,"%s", errbuf);
+		fflush(stderr);
+
+		/* abort time methinks ... */
+		close(data);
+		return NULL;
+	    } else {
+		if (ssl_debug_flag) {
+		    BIO_printf(bio_err,"[SSL DATA Cipher %s]\n",
+			            SSL_get_cipher(ssl_con));
+		}
+		ssl_data_active_flag=1;
+	    }
+
+	    if (debug) {
+		fprintf(stderr,"===>DONE SSL connect on DATA %d\n",data);
+		fflush(stderr);
+	    }
+
+	}
+#endif /* USE_SSL */
+
 	return (fdopen(data, lmode));
 }
 
@@ -1609,3 +1795,142 @@
 	}
 	(void) getreply(0);
 }
+
+static int
+auth_user(char *u,char *p)
+{
+	int n;
+
+#ifdef USE_SSL
+        if (ssl_enabled) {
+	    n = command("AUTH SSL");
+	    if (n == ERROR) {		/* do normal USER/PASS */
+		printf("SSL not available\n");
+		/* spit the dummy as we will only talk ssl
+		 * when running in "secure" mode 
+		 */
+		if (ssl_secure_flag)
+		    return ERROR;
+	    } else if (n == CONTINUE || n == COMPLETE) {
+		/* do SSL */
+		ssl_con=(SSL *)SSL_new(ssl_ctx);
+
+		SSL_set_fd(ssl_con,fileno(cout));
+		set_ssl_trace(ssl_con);
+
+		SSL_set_verify(ssl_con,ssl_verify_flag,NULL);
+
+		/* Add in any certificates if you want to here ... */
+		if (my_ssl_cert_file) {
+		    if (!SSL_use_certificate_file(ssl_con, my_ssl_cert_file,
+			X509_FILETYPE_PEM)) {
+			fprintf(stderr,"%s: ",my_ssl_cert_file);
+			ERR_print_errors_fp(stderr);
+			exit(1);
+		    } else {
+			if (!my_ssl_key_file)
+			    my_ssl_key_file = my_ssl_cert_file;
+			if (!SSL_use_RSAPrivateKey_file(ssl_con, my_ssl_key_file,
+			    X509_FILETYPE_PEM)) {
+			    fprintf(stderr,"%s: ", my_ssl_key_file);
+			    ERR_print_errors_fp(stderr);
+			    exit(1);
+			}
+		    }
+		}
+
+		if (SSL_connect(ssl_con)<=0) {
+		    static char errbuf[1024];
+
+		    sprintf(errbuf,"ftp: SSL_connect error %s\n",
+				ERR_error_string(ERR_get_error(),NULL));
+		    perror(errbuf);
+		    /* abort time methinks ... */
+		    exit(1);
+		} else {
+		    fprintf(stderr,"[SSL Cipher %s]\n",SSL_get_cipher(ssl_con));
+		    fflush(stderr);
+		    ssl_active_flag=1;
+		}
+
+		n = command("USER %s",u);
+		if (n == CONTINUE) {
+			if(p == NULL)
+				p = getpass("Password:");
+			n = command("PASS %s",p);
+		}
+		return (n);
+	    }
+	}
+#endif /* USE_SSL */
+	n = command("USER %s",u);
+	if (n == CONTINUE) {
+		if(p == NULL)
+		p = getpass("Password:");
+		n = command("PASS %s",p);
+	}
+	return(n);
+}
+
+#ifdef USE_SSL
+
+/* we really shouldn't have code like this! --tjh */
+static int 
+ssl_getc(SSL *ssl_con)
+{
+    char onebyte;
+    int ret;
+
+    if ((ret=SSL_read(ssl_con,&onebyte,1))!=1) {
+      /* we want to know this stuff! */
+      if (ssl_debug_flag || (ret!=0)) {
+	  fprintf(stderr,"ssl_getc: SSL_read failed %d = %d\n",ret,errno);
+	  fflush(stderr);
+      }
+      return -1;
+    } else {
+	if (ssl_debug_flag) {
+	    BIO_printf(bio_err,"ssl_getc: SSL_read %d (%c) ",onebyte & 0xff,isprint(onebyte)?onebyte:'.');
+	}
+	return onebyte & 0xff;
+    }
+}
+
+
+/* got back to this an implemented some rather "simple" buffering */
+static char putc_buf[BUFSIZ];
+static int putc_buf_pos=0;
+
+static int
+ssl_putc_flush(SSL *ssl_con)
+{
+    if (putc_buf_pos>0) {
+	if (SSL_write(ssl_con,putc_buf,putc_buf_pos)!=putc_buf_pos) {
+	    if (ssl_debug_flag) {
+		BIO_printf(bio_err,"ssl_putc_flush: WRITE FAILED\n");
+	    }
+	    putc_buf_pos=0;
+	    return -1;
+	}
+    }
+    putc_buf_pos=0;
+    return 0;
+}
+
+int 
+ssl_putc(SSL *ssl_con,int oneint)
+{
+    char onebyte;
+
+    onebyte = oneint & 0xff;
+
+    /* make sure there is space */
+    if (putc_buf_pos>=BUFSIZ) 
+	if (ssl_putc_flush(ssl_con)!=0)
+	  return EOF;
+    putc_buf[putc_buf_pos++]=onebyte;
+
+    return onebyte;
+}
+
+#endif /* USE_SSL */
--- netkit-ftp-0.17/ftp/ftp_var.h
+++ netkit-ftp-0.17+ssl-0.2/ftp/ftp_var.h
@@ -158,3 +158,6 @@
 void setpeer(int argc, char *argv[]);
 void quit(void);
 void changetype(int newtype, int show);
+
+#include "sslapp.h"
+#include "ssl_port.h"
--- netkit-ftp-0.17/ftp/main.c
+++ netkit-ftp-0.17+ssl-0.2/ftp/main.c
@@ -1,3 +1,15 @@
+/* 
+ * The modifications to support SSLeay were done by Tim Hudson
+ * tjh@cryptsoft.com
+ *
+ * You can do whatever you like with these patches except pretend that
+ * you wrote them. 
+ *
+ * Email ssl-users-request@lists.cryptsoft.com to get instructions on how to
+ * join the mailing list that discusses SSLeay and also these patches.
+ *
+ */
+
 /*
  * Copyright (c) 1985, 1989 Regents of the University of California.
  * All rights reserved.
@@ -82,6 +94,75 @@
 static void cmdscanner(int top);
 static char *slurpstring(void);
 
+#ifdef USE_SSL
+
+/* icky way of doing things ... */
+#include "sslapp.c"
+
+/*
+#include "ssl_err.h"
+*/
+
+SSL *ssl_con;
+SSL *ssl_data_con;
+int ssl_data_active_flag=0;
+
+/* for the moment this is a compile time option only --tjh */
+int ssl_encrypt_data=1;
+int ssl_enabled=1;
+
+char *my_ssl_key_file=NULL;
+char *my_ssl_cert_file=NULL;
+
+BIO *bio_err=NULL;
+
+static long
+bio_dump_cb(BIO *bio,
+	    int cmd,
+	    char *argp,
+	    int argi,
+	    long argl,
+	    long ret)
+        {
+        BIO *out;
+
+/*
+        out=(BIO *)BIO_get_callback_arg(bio);
+*/
+	out=bio_err;
+        if (out == NULL) return(ret);
+
+        if (cmd == (BIO_CB_READ|BIO_CB_RETURN))
+                {
+                BIO_printf(out,"read from %08X (%d bytes => %ld (%X))\n",
+                        bio,argi,ret,ret);
+                BIO_dump(out,argp,(int)ret);
+		BIO_flush(out);
+                }
+        else if (cmd == (BIO_CB_WRITE|BIO_CB_RETURN))
+                {
+                BIO_printf(out,"write to %08X (%d bytes => %ld (%X))\n",
+                        bio,argi,ret,ret);
+                BIO_dump(out,argp,(int)ret);
+		BIO_flush(out);
+                }
+        return( (cmd & BIO_CB_RETURN) ? ret : 1);
+        }
+
+int
+set_ssl_trace(SSL *con) 
+{
+    if (con!=NULL) {
+	if (ssl_debug_flag) {
+	    BIO_set_callback(SSL_get_rbio(con),bio_dump_cb);
+	    BIO_set_callback_arg(SSL_get_rbio(con),bio_err);
+	}
+    }
+    return 0;
+}
+
+#endif /* USE_SSL */
+
 static
 void
 usage(void)
@@ -106,6 +187,7 @@
 	int top;
 	struct passwd *pw = NULL;
 	char homedir[MAXPATHLEN];
+	char *optarg;
 
 	tick = 0;
 
@@ -134,6 +216,7 @@
 
 	argc--, argv++;
 	while (argc > 0 && **argv == '-') {
+	        optarg=*(argv+1);
 		for (cp = *argv + 1; *cp; cp++)
 			switch (*cp) {
 
@@ -174,6 +257,48 @@
 				usage();
 				exit(0);
 
+#ifdef USE_SSL
+			case 'z':
+				if (!optarg) {
+					fprintf(stderr, "ftp: the -z option requires an argument\n");
+					exit(1);
+				}
+				if (strcmp(optarg, "debug") == 0 ) {
+				    ssl_debug_flag=1;
+				}
+				if (strcmp(optarg, "ssl") == 0 ) {
+				    ssl_only_flag=1;
+				}
+				/* disable *all* ssl stuff */
+				if ( (strcmp(optarg, "!ssl") == 0 ) || 
+				     (strcmp(optarg, "nossl") == 0 ) )  {
+				    ssl_enabled=0;
+				}
+				if (strcmp(optarg, "secure") == 0 ) {
+				    ssl_secure_flag=1;
+				}
+				if (strcmp(optarg, "certsok") == 0 ) {
+				    ssl_certsok_flag=1;
+				}
+				if (strcmp(optarg, "verbose") == 0 ) {
+				    ssl_verbose_flag=1;
+				}
+				if (strncmp(optarg, "verify=", strlen("verify=")) == 0 ) {
+				    ssl_verify_flag=atoi(optarg+strlen("verify="));
+				}
+				if (strncmp(optarg, "cert=", strlen("cert=")) == 0 ) {
+				    my_ssl_cert_file=optarg+strlen("cert=");
+				}
+				if (strncmp(optarg, "key=", strlen("key=")) == 0 ) {
+				    my_ssl_key_file=optarg+strlen("key=");
+				}
+
+                                /* we have swallowed an extra arg */
+				argc--;
+                                argv++;
+				break;
+#endif /* USE_SSL */
+
 			default:
 				fprintf(stdout,
 				  "ftp: %c: unknown option\n", *cp);
@@ -202,6 +323,18 @@
 		homedir[sizeof(homedir)-1] = 0;
 		home = homedir;
 	}
+
+#ifdef USE_SSL
+        if (ssl_enabled) {
+	    if (!do_ssleay_init(0)) {
+		fprintf(stderr,"ftp: SSLeay initialisation failed\n");
+		fflush(stderr);
+		ERR_print_errors_fp(stderr);
+		exit(1);
+	    }
+	}
+#endif /* USE_SSL */
+
 	if (argc > 0) {
 		if (sigsetjmp(toplevel, 1))
 			exit(0);
--- netkit-ftp-0.17/ftp/ssl_port.h
+++ netkit-ftp-0.17+ssl-0.2/ftp/ssl_port.h
@@ -0,0 +1,85 @@
+/* ssl_port.h    - standard porting things 
+ *
+ * The modifications to support SSLeay were done by Tim Hudson
+ * tjh@mincom.oz.au
+ *
+ * You can do whatever you like with these patches except pretend that
+ * you wrote them. 
+ *
+ * Email ssl-users-request@mincom.oz.au to get instructions on how to
+ * join the mailing list that discusses SSLeay and also these patches.
+ *
+ */
+
+#ifndef HEADER_SSL_PORT_H
+#define HEADER_SSL_PORT_H
+
+#ifdef USE_SSL
+
+#include <stdio.h>
+
+#define OLDPROTO NOPROTO
+#define NOPROTO
+#include <openssl/buffer.h>
+#undef NOPROTO
+#define NOPROTO OLDPROTO
+
+#include <openssl/x509.h>
+#include <openssl/ssl.h>
+#include <openssl/err.h>
+
+extern SSL *ssl_con;
+extern SSL_CTX *ssl_ctx;
+extern int ssl_debug_flag;
+extern int ssl_only_flag;
+extern int ssl_active_flag;
+extern int ssl_verify_flag;
+extern int ssl_secure_flag;
+extern int ssl_enabled;
+
+extern int ssl_encrypt_data;
+extern SSL *ssl_data_con;
+extern int ssl_data_active_flag;
+
+extern char *my_ssl_cert_file;
+extern char *my_ssl_key_file;
+extern int ssl_certsok_flag;
+
+extern int set_ssl_trace(SSL *s);
+
+extern FILE *cin, *cout;
+
+#define is_ssl_fd(X,Y)    ( (SSL_get_fd((X))==0) || \
+                            (SSL_get_fd((X))==1) || \
+                            (SSL_get_fd((X))==pdata) || \
+			    (SSL_get_fd((X))==(Y)) \
+			  )
+
+#define is_ssl_fp(X,Y)    ( ( (SSL_get_fd((X))==0) && (fileno((Y))==0) ) || \
+                            ( (SSL_get_fd((X))==1) && (fileno((Y))==1) ) || \
+                            ( (SSL_get_fd((X))==pdata) && \
+			    			  (fileno((Y))==pdata) ) || \
+			    (SSL_get_fd((X))==fileno(Y)) \
+			  )
+
+/* these macros make things much easier to handle ... */
+
+#define FFLUSH(X)         (ssl_active_flag && (((X)==cin)||((X)==cout)) ? 1 : fflush((X)) )
+
+#define GETC(X)           (ssl_active_flag && (((X)==cin)||((X)==cout)) ? ssl_getc(ssl_con) : getc((X)) )
+
+#define DATAGETC(X)       (ssl_data_active_flag && ((fileno(X)==data)||(fileno(X)==pdata)) ? ssl_getc(ssl_data_con) : getc((X)) )
+#define DATAPUTC(X,Y)     (ssl_data_active_flag && ((fileno(Y)==data)||(fileno(Y)==pdata)) ? ssl_putc(ssl_data_con,(X)) : putc((X),(Y)) )
+#define DATAFLUSH(X)      (ssl_data_active_flag && ((fileno(X)==data)||(fileno(X)==pdata)) ? ssl_putc_flush(ssl_data_con) : fflush((X)) )
+
+#else
+
+#define GETC(X)           getc((X))
+#define DATAGETC(X)       getc((X))
+#define DATAPUTC(X,Y)     putc((X),(Y))
+#define DATAFLUSH(X)      fflush((X))
+#define FFLUSH(X)         fflush((X))
+
+#endif /* USE_SSL */
+
+#endif /*  HEADER_SSL_PORT_H */
--- netkit-ftp-0.17/ftp/sslapp.c
+++ netkit-ftp-0.17+ssl-0.2/ftp/sslapp.c
@@ -0,0 +1,186 @@
+/* sslapp.c	- ssl application code */
+
+/*
+ * The modifications to support SSLeay were done by Tim Hudson
+ * tjh@cryptsoft.com
+ *
+ * You can do whatever you like with these patches except pretend that
+ * you wrote them.
+ *
+ * Email ssl-users-request@lists.cryptsoft.com to get instructions on how to
+ * join the mailing list that discusses SSLeay and also these patches.
+ *
+ */
+
+#ifdef USE_SSL
+
+#include "sslapp.h"
+
+SSL_CTX *ssl_ctx;
+SSL *ssl_con;
+int ssl_debug_flag=0;
+int ssl_only_flag=0;
+int ssl_active_flag=0;
+int ssl_verify_flag=SSL_VERIFY_NONE;
+int ssl_secure_flag=0;
+int ssl_certsok_flag=0;
+int ssl_cert_required=0;
+int ssl_verbose_flag=0;
+int ssl_disabled_flag=0;
+char *ssl_cert_file=NULL;
+char *ssl_key_file=NULL;
+char *ssl_cipher_list=NULL;
+char *ssl_log_file=NULL;
+
+/* fwd decl */
+static void
+client_info_callback(SSL *s, int where, int ret);
+
+int 
+do_ssleay_init(int server)
+{
+  char *p;
+
+  /* make sure we have somewhere we can log errors to */
+  if (bio_err==NULL) {
+    if ((bio_err=BIO_new(BIO_s_file()))!=NULL) {
+      if (ssl_log_file==NULL)
+	BIO_set_fp(bio_err,stderr,BIO_NOCLOSE);
+      else {
+	if (BIO_write_filename(bio_err,ssl_log_file)<=0) {
+	  /* not a lot we can do */
+	}
+      }
+    }
+  }
+
+  /* rather simple things these days ... the old SSL_LOG and SSL_ERR
+   * vars are long gone now SSLeay8 has rolled around and we have 
+   * a clean interface for doing things
+   */
+  if (ssl_debug_flag)
+    BIO_printf(bio_err,"SSL_DEBUG_FLAG on\r\n");
+
+
+  /* init things so we will get meaningful error messages
+   * rather than numbers 
+   */
+  SSL_load_error_strings();
+
+  SSLeay_add_ssl_algorithms();
+  ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_method());
+
+  /* we may require a temp 512 bit RSA key because of the
+   * wonderful way export things work ... if so we generate
+   * one now!
+   */
+  if (server) {
+    if (SSL_CTX_need_tmp_RSA(ssl_ctx)) {
+      RSA *rsa;
+
+      if (ssl_debug_flag)
+	  BIO_printf(bio_err,"Generating temp (512 bit) RSA key ...\r\n");
+      rsa=RSA_generate_key(512,RSA_F4,NULL,NULL);
+      if (ssl_debug_flag)
+	  BIO_printf(bio_err,"Generation of temp (512 bit) RSA key done\r\n");
+   
+      if (!SSL_CTX_set_tmp_rsa(ssl_ctx,rsa)) {
+	BIO_printf(bio_err,"Failed to assign generated temp RSA key!\r\n");
+      }
+      RSA_free(rsa);
+      if (ssl_debug_flag)
+	  BIO_printf(bio_err,"Assigned temp (512 bit) RSA key\r\n");
+    }
+  }
+
+  /* also switch on all the interoperability and bug
+   * workarounds so that we will communicate with people
+   * that cannot read poorly written specs :-)
+   */
+  SSL_CTX_set_options(ssl_ctx,SSL_OP_ALL);
+
+  /* the user can set whatever ciphers they want to use */
+  if (ssl_cipher_list==NULL) {
+      p=getenv("SSL_CIPHER");
+      if (p!=NULL)
+        SSL_CTX_set_cipher_list(ssl_ctx,p);
+  } else
+      SSL_CTX_set_cipher_list(ssl_ctx,ssl_cipher_list);
+
+  /* for verbose we use the 0.6.x info callback that I got
+   * eric to finally add into the code :-) --tjh
+   */
+  if (ssl_verbose_flag) {
+      SSL_CTX_set_info_callback(ssl_ctx,client_info_callback);
+  }
+
+  /* Add in any certificates if you want to here ... */
+  if (ssl_cert_file) {
+      if (!SSL_CTX_use_certificate_file(ssl_ctx, ssl_cert_file, 
+		      X509_FILETYPE_PEM)) {
+	  BIO_printf(bio_err,"Error loading %s: ",ssl_cert_file);
+	  ERR_print_errors(bio_err);
+	  BIO_printf(bio_err,"\r\n");
+	  return(0);
+      } else {
+	  if (!ssl_key_file)
+	      ssl_key_file = ssl_cert_file;
+	  if (!SSL_CTX_use_RSAPrivateKey_file(ssl_ctx, ssl_key_file,
+		      X509_FILETYPE_PEM)) {
+	      BIO_printf(bio_err,"Error loading %s: ",ssl_key_file);
+	      ERR_print_errors(bio_err);
+	      BIO_printf(bio_err,"\r\n");
+	      return(0);
+	  }
+      }
+  }
+
+  /* make sure we will find certificates in the standard
+   * location ... otherwise we don't look anywhere for
+   * these things which is going to make client certificate
+   * exchange rather useless :-)
+   */
+  SSL_CTX_set_default_verify_paths(ssl_ctx);
+
+  /* now create a connection */
+  ssl_con=(SSL *)SSL_new(ssl_ctx);
+  SSL_set_verify(ssl_con,ssl_verify_flag,NULL);
+
+#if 0
+  SSL_set_verify(ssl_con,ssl_verify_flag,client_verify_callback);
+#endif
+
+  return(1);
+}
+
+
+static void
+client_info_callback(SSL *s, int where, int ret)
+{
+  if (where==SSL_CB_CONNECT_LOOP) {
+    BIO_printf(bio_err,"SSL_connect:%s %s\r\n",
+		    SSL_state_string(s),SSL_state_string_long(s));
+  } else if (where==SSL_CB_CONNECT_EXIT) {
+    if (ret == 0) {
+      BIO_printf(bio_err,"SSL_connect:failed in %s %s\r\n",
+	      SSL_state_string(s),SSL_state_string_long(s));
+    } else if (ret < 0) {
+      BIO_printf(bio_err,"SSL_connect:error in %s %s\r\n",
+	      SSL_state_string(s),SSL_state_string_long(s));
+    }
+  }
+}
+
+
+#else /* !USE_SSL */
+
+/* something here to stop warnings if we build without SSL support */
+static int dummy_func()
+{
+  int i;
+
+  i++;
+}
+
+#endif /* USE_SSL */
+
--- netkit-ftp-0.17/ftp/sslapp.h
+++ netkit-ftp-0.17+ssl-0.2/ftp/sslapp.h
@@ -0,0 +1,63 @@
+/* sslapp.h	- ssl application code */
+
+/*
+ * The modifications to support SSLeay were done by Tim Hudson
+ * tjh@cryptsoft.com
+ *
+ * You can do whatever you like with these patches except pretend that
+ * you wrote them.
+ *
+ * Email ssl-users-request@mincom.oz.au to get instructions on how to
+ * join the mailing list that discusses SSLeay and also these patches.
+ *
+ */
+
+#ifdef USE_SSL
+
+#include <stdio.h>
+
+#include <openssl/crypto.h>
+
+#define SSL_set_pref_cipher(c,n)        SSL_set_cipher_list(c,n)
+#define ONELINE_NAME(X) X509_NAME_oneline(X,NULL,0)
+  
+#define OLDPROTO NOPROTO
+#define NOPROTO
+#include <openssl/bio.h>
+#undef NOPROTO
+#define NOPROTO OLDPROTO
+#undef OLDPROTO
+#include <openssl/buffer.h>
+
+#include <openssl/x509.h>
+#include <openssl/ssl.h>
+#include <openssl/err.h>
+
+extern BIO *bio_err;
+extern SSL *ssl_con;
+extern SSL_CTX *ssl_ctx;
+extern int ssl_debug_flag;
+extern int ssl_only_flag;
+extern int ssl_active_flag;
+extern int ssl_verify_flag;
+extern int ssl_secure_flag;
+extern int ssl_verbose_flag;
+extern int ssl_disabled_flag;
+extern int ssl_cert_required;
+extern int ssl_certsok_flag;
+
+extern char *ssl_log_file; 
+extern char *ssl_cert_file; 
+extern char *ssl_key_file;
+extern char *ssl_cipher_list;
+
+/* we hide all the initialisation code in a separate file now */
+extern int do_ssleay_init(int server);
+
+/*extern int display_connect_details();
+extern int server_verify_callback();
+extern int client_verify_callback();*/
+
+#endif /* USE_SSL */
+
+