summaryrefslogtreecommitdiff
path: root/floppyd.c
blob: 92ca38c5705d9cebec4449241b512e019f7f5910 (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
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
/*  Copyright 1999 Peter Schlaile.
 *  Copyright 1999-2005,2007-2009 Alain Knaff.
 *  This file is part of mtools.
 *
 *  Mtools is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Mtools is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
 *
 * the floppyd daemon running on the local X-Server
 *
 * written by:
 *
 * Peter Schlaile
 *
 * udbz@rz.uni-karlsruhe.de
 *
 * Large parts of the network code shamelessly stolen from 
 * transproxy by John Saunders <john@nlc.net.au>
 *
 * Rewritten in C by Alain Knaff.  Apparently C++ is still not as
 * portable as C.  */

#define DEBUG 0

#include "sysincludes.h"
#include "llong.h"

#ifdef USE_FLOPPYD

#define USE_FLOPPYD_BUFFERED_IO  1
#define FLOPPYD_DEFAULT_PORT     5703

#include "sysincludes.h"
#include "grp.h"
#include <X11/Xlib.h>
#include <X11/Xauth.h>

#include "floppyd_io.h"

#ifndef SIGCLD
#define SIGCLD SIGCHLD
#endif

/* For Linux 1.2.13 */
#ifndef SOMAXCONN
#define SOMAXCONN 5
#endif

/*
   To compile:

   gcc -Wall floppyd.cpp -o floppyd -lX11

   floppyd

   Communication to the clients works the following way:

   Client sends his protocol-version. If the version between server and client
   differ: bail out.

   After that,we send our .Xauthority-file (a maximum of MAX_XAUTHORITY_LENGTH 
   Bytes long) to the server.

   The server then checks, if it already has a .Xauthority file. If so
   it is interpreted as LOCK-File for the floppy-device and the communication
   gets terminated.

   (What if we have 2 floppy devices? Well. Two floppy users with different
   home-directories should work nicely...)

   Now, the data is written to the .Xauthority file. Then we try to open
   a connection to the local X-Server. If this fails -> bail out.

   ***

   The data packets are built as follows:

   Base-packets: 1 Dword length, then data.
                 length is in Network-Byte order. (4 Bytes)

   Commands are: 1. Packet Opcode (length 1), 1. Data packet as parameter.

   Return: 1.Packet: 1. Dword: Bytes processed, 2. Dword: Error-Code

   ***

   TODO:
	  * Implement some IOCTL calls to format floppy disks or so...
	  * Read is somewhat dirty implemented. Tries multiple times to
	    read the expected bytes from the socket stream. Don't know
	    why this is necessary. Maybe the socket stream is nonblocking
	    or something IT SHOULD NOT BE!
	    
*/

typedef unsigned char Byte;
typedef unsigned long Dword;
typedef mt_off_t Qword;

#define MAX_XAUTHORITY_LENGTH    3000
#define MAX_DATA_REQUEST         3000000
#define BUFFERED_IO_SIZE         16348


void serve_client(int sock, char **device_name, int n_dev, int close_stderr);


#ifdef USE_FLOPPYD_BUFFERED_IO
typedef struct io_buffer {
	Byte out_buffer[BUFFERED_IO_SIZE];
	Byte in_buffer[BUFFERED_IO_SIZE];
	
	long in_valid;
	long in_start;
	long out_valid;
	
	int handle;
} *io_buffer;

static io_buffer new_io_buffer (int _handle) {
	io_buffer buffer;

	buffer = New(struct io_buffer);

	buffer->handle = _handle;
	buffer->in_valid = buffer->in_start = 0;
	buffer->out_valid = 0;
	return buffer;
}


static void flush(io_buffer buffer) {
	if (buffer->out_valid) {
		if(write(buffer->handle, buffer->out_buffer, buffer->out_valid) < 0) {
			perror("floppyd flush");
		}
		buffer->out_valid = 0;
	}
}

static void free_io_buffer(io_buffer buffer) {
	flush(buffer);
	free(buffer);
}


static size_t buf_read (io_buffer buf, Byte* buffer, size_t nbytes) {
	size_t rval;
	
	if (nbytes <= buf->in_valid) {
		memcpy(buffer, buf->in_buffer+buf->in_start, nbytes);
		buf->in_valid -= nbytes;
		buf->in_start += nbytes;
		rval = nbytes;
	} else {
		if (buf->in_valid) 
			memcpy(buffer, buf->in_buffer+buf->in_start, 
				   buf->in_valid);
		nbytes -= buf->in_valid;
		buffer += buf->in_valid;
		if (nbytes > BUFFERED_IO_SIZE) {
			rval = read(buf->handle, buffer, nbytes);
			if (rval >= 0) {
				rval += buf->in_valid;
			}
			buf->in_valid = buf->in_start = 0;
		} else {
			rval = read(buf->handle, buf->in_buffer, 
						BUFFERED_IO_SIZE);
			if (rval >= 0) {
				if (rval < nbytes) {
					memcpy(buffer, buf->in_buffer, rval);
					rval += buf->in_valid;
					buf->in_valid = buf->in_start = 0;
				} else {
					size_t a;
					memcpy(buffer, buf->in_buffer, nbytes);
					buf->in_start = nbytes;
					a = buf->in_valid;
					buf->in_valid = rval-nbytes;
					rval = a + nbytes;
				}
			}
		}
	}
	return rval;
}

static size_t buf_write(io_buffer buf, void* buffer, size_t nbytes) {
	if (buf->out_valid + nbytes > BUFFERED_IO_SIZE) {
		flush(buf);
		return write(buf->handle, buffer, nbytes);
	}
	memcpy(buf->out_buffer+buf->out_valid, buffer, nbytes);
	buf->out_valid += nbytes;
	return nbytes;
}



#else

typedef int io_buffer;

io_buffer new_io_buffer (int handle) {
	return handle;
}


size_t buf_read (io_buffer handle, Byte* buffer, size_t nbytes) {
	return (read(handle, buffer, nbytes));
}

size_t buf_write(io_buffer handle, void* buffer, size_t nbytes) {
	return (write(handle, buffer, nbytes));
}


void free_io_buffer(io_buffer buffer) { }


void flush(io_buffer buffer) { }

#endif

typedef struct Packet {
	Byte* data;
	Dword len;
	Dword alloc_size;
} *Packet;

#include "byte_dword.h"

static Dword read_dword(io_buffer fp)
{
	Byte val[4];
	if (buf_read(fp, val, 4) < 4) {
		return 0xffffffff;
	}
	
	return byte2dword(val);
}

static void write_dword(io_buffer fp, Dword parm)
{
	Byte val[4];
	
	dword2byte(parm, val);
	
	buf_write(fp, val,4);
}


static Packet newPacket(void)
{
	Packet packet;

	packet = New(struct Packet);
	packet->data = NULL;
	packet->len = packet->alloc_size = 0;
	return packet;
}


static void destroyPacket(Packet packet)
{
	if(packet->data)
		free(packet->data);
	free(packet);
}

static void kill_packet(Packet packet)
{
	if(packet->data)
		free(packet->data);
	packet->data = NULL;
	packet->len = 0;
	packet->alloc_size = 0;
}

static void make_new(Packet packet, unsigned long l)
{
	if (l < packet->alloc_size) {
		packet->len = l;
		return;
	}
	kill_packet(packet);
	packet->len = packet->alloc_size = l;
	packet->data = malloc(l);
	memset(packet->data, 0, l);
}

static char send_packet(Packet packet, io_buffer fp)
{
	if (packet->data) {
		write_dword(fp, packet->len);
		buf_write(fp, packet->data, packet->len);
		flush(fp);
#if DEBUG
		fprintf(stderr, "send_packet(): Size: %li\n", packet->len);
#endif

#if DEBUG
		fprintf(stderr, "send_packet(): ");
		for (int i = 0; i < packet->len; i++) {
			fprintf(stderr, "%d ", packet->data[i]);
		}
		fprintf(stderr, "\n");
#endif		

	}
	return (packet->data != NULL);
}

static char recv_packet(Packet packet, io_buffer fp, Dword maxlength)
{
	int start;
	int l;
	Dword length = read_dword(fp);
#if DEBUG
	fprintf(stderr, "recv_packet(): Size: %li\n", length);
#endif
	if (length > maxlength || length == 0xffffffff ) {
		return 0;
	}
	make_new(packet, length);
	l = 0;
	for (start = 0; start < length; start += l) {
		l = buf_read(fp, packet->data+start, length-start);
		if (l == 0) {
			return 0;
		}
	}
	if (packet->len == 0) {
		return 0;
	}
#if DEBUG
	fprintf(stderr, "*** read: %li\n", packet->len);
#endif
	
#if DEBUG
	fprintf(stderr, "recv_packet(): ");
	for (i = 0; i < packet->len; i++) {
		fprintf(stderr, "%d ", packet->data[i]);
	}
	fprintf(stderr, "\n");
#endif		
	return 1;
}

static void read_packet(Packet packet, int fd, int length) {
	make_new(packet, length);
	packet->len = read(fd, packet->data, packet->len);
}

static int write_packet(Packet packet, int fd) {
	return (write(fd, packet->data, packet->len));
}

static void put_dword(Packet packet, int my_index, Dword val) {
	dword2byte(val, packet->data+my_index);
}

static void put_qword(Packet packet, int my_index, Qword val) {
	qword2byte(val, packet->data+my_index);
}

static Dword get_dword(Packet packet, int my_index) {
	return byte2dword(packet->data+my_index);
}	

static Qword get_qword(Packet packet, int my_index) {
	return byte2qword(packet->data+my_index);
}	

static Dword get_length(Packet packet) {
	return packet->len;
}

static int eat(char **ptr, int *len, unsigned char c) {
    /* remove length + size code + terminating 0 */
    if (*len < c + 3)
	return -1;
    (*ptr) += c + 2;
    (*len) -= c + 2;
    return 0;
}

static const char *dispName;

static char XAUTHORITY[]="XAUTHORITY";

static char do_auth(io_buffer sock, int *version) 
{
	int fd;
	Display* displ;
	Packet proto_version = newPacket();
	Packet mit_cookie;
	char *ptr;
	int len;

	char authFile[41]="/tmp/floppyd.XXXXXX";
	char template[4096];

	Packet reply = newPacket();

	make_new(reply, 4);

	if (!recv_packet(proto_version, sock, 4)) {
		put_dword(reply, 0, AUTH_PACKETOVERSIZE);
		send_packet(reply, sock);
		destroyPacket(reply);
		destroyPacket(proto_version);
		return 0;
	}

	*version = get_dword(proto_version, 0);
	if (*version > FLOPPYD_PROTOCOL_VERSION || 
	    *version < FLOPPYD_PROTOCOL_VERSION_OLD) {
		/* fail if client requests a newer version than us */
		put_dword(reply, 0, AUTH_WRONGVERSION);
		send_packet(reply, sock);
		destroyPacket(reply);
		destroyPacket(proto_version);
		return 0;
	}

	if(*version == FLOPPYD_PROTOCOL_VERSION_OLD) {
		put_dword(reply, 0, AUTH_SUCCESS);
	} else {
		Dword cap = FLOPPYD_CAP_EXPLICIT_OPEN;
		if(sizeof(mt_off_t) >= 8) {
			cap |= FLOPPYD_CAP_LARGE_SEEK;
		}
		make_new(reply, 12);
		put_dword(reply, 0, AUTH_SUCCESS);
		put_dword(reply, 4, FLOPPYD_PROTOCOL_VERSION);
		put_dword(reply, 8, cap);
	}
	send_packet(reply, sock);
	destroyPacket(proto_version);

	make_new(reply, 4);
	mit_cookie = newPacket();
	if (!recv_packet(mit_cookie, sock, MAX_XAUTHORITY_LENGTH)) {
		put_dword(reply, 0, AUTH_PACKETOVERSIZE);
		send_packet(reply, sock);
		destroyPacket(reply);
		destroyPacket(mit_cookie);
		return 0;
	}

	umask(077);
	fd = mkstemp(authFile);
	if(fd == -1) {
		/* Different error than file exists */
		put_dword(reply, 0, AUTH_DEVLOCKED);
		send_packet(reply, sock);
		close(fd);
		destroyPacket(reply);
		destroyPacket(mit_cookie);
		return 0;
	}
#ifdef HAVE_SETENV
	setenv(XAUTHORITY, authFile, 1);
#else
	{
	  char *buffer=malloc(strlen(XAUTHORITY)+strlen(authFile)+2);
	  strcpy(buffer, XAUTHORITY);
	  strcat(buffer, "=");
	  strcat(buffer, authFile);
	  putenv(buffer);
	}
#endif

	ptr = template;
	ptr[4095] = 0;
	*ptr++ = 1;
	*ptr++ = 0;
	*ptr++ = 0;
	gethostname(ptr+1, 4088);
	len = strlen(ptr+1);
	*ptr++ = len;
	ptr += len;
	*ptr++ = 0;
	*ptr++ = 1;
	*ptr++ = '0'; /* Display number */
	*ptr++ = '\0';

	if(write(fd, template, len+8) < len + 8) {
		close(fd);
		return 0;
	}
	ptr = (char *)mit_cookie->data;
	len = mit_cookie->len;

	if (eat(&ptr,&len,1) ||    /* the "type"    */
	    eat(&ptr,&len,*ptr) || /* the hostname  */
	    eat(&ptr,&len,*ptr)) { /* the display number */
	    destroyPacket(mit_cookie);
	    unlink(XauFileName());
	    put_dword(reply, 0, AUTH_BADPACKET);
	    send_packet(reply, sock);
	    destroyPacket(reply);
	    return 0;
	}

	if(write(fd, ptr, len) < len) {
		close(fd);
		return 0;
	}
	close(fd);

	destroyPacket(mit_cookie);

	displ = XOpenDisplay(dispName);
	if (!displ) {
		unlink(XauFileName());
		put_dword(reply, 0, AUTH_AUTHFAILED);
		send_packet(reply, sock);
		destroyPacket(reply);
		return 0;
	}
	XCloseDisplay(displ);

	put_dword(reply, 0, AUTH_SUCCESS);
	send_packet(reply, sock);
	destroyPacket(reply);
	unlink(XauFileName());
	return 1;
}

/*
 * Return the port number, in network order, of the specified service.
 */
static short getportnum(char *portnum)
{
	char			*digits = portnum;
	struct servent	*serv;
	short			port;

	for (port = 0; isdigit(*digits); ++digits)
		{
			port = (port * 10) + (*digits - '0');
		}

	if ((*digits != '\0') || (port <= 0))
		{
			if ((serv = getservbyname(portnum, "tcp")) != NULL)
				{
					port = ntohs(serv->s_port);
				}
			else
				{
					port = -1;
				}
			endservent();
		}

#if DEBUG
	fprintf(stderr, "Port lookup %s -> %hd\n", portnum, port);
#endif

	return (port);
}

/*
 * Return the IP address of the specified host.
 */
static IPaddr_t getipaddress(char *ipaddr)
{
	struct hostent	*host;
	IPaddr_t ip;

	if (((ip = inet_addr(ipaddr)) == INADDR_NONE)
	    &&
		(strcmp(ipaddr, "255.255.255.255") != 0))
		{
			if ((host = gethostbyname(ipaddr)) != NULL)
				{
					memcpy(&ip, host->h_addr, sizeof(ip));
				}
			endhostent();
		}

#if DEBUG
	fprintf(stderr, "IP lookup %s -> 0x%08lx\n", ipaddr, ip);
#endif

	return (ip);
}

/*
 * Find the userid of the specified user.
 */
static uid_t getuserid(char *user)
{
	struct passwd	*pw;
	uid_t			uid;

	if ((pw = getpwnam(user)) != NULL)
		{
			uid = pw->pw_uid;
		}
	else if (*user == '#')
		{
			uid = (uid_t)atoi(&user[1]);
		}
	else
		{
#ifdef HAVE_GETUSERID
			id = getuserid("nobody");
#else
			uid = 65535;
#endif
		}

#if DEBUG
	fprintf(stderr, "User lookup %s -> %d\n", user, uid);
#endif

	endpwent();

	return (uid);
}

/*
 * Find the groupid of the specified user.
 */
static uid_t getgroupid(uid_t uid)
{
	struct passwd	*pw;
	gid_t			gid;

	if ((pw = getpwuid(uid)) != NULL)
		{
			gid = pw->pw_gid;
		}
	else
		{
#ifdef HAVE_GETGROUPID
			id = getgroupid(uid);
#else
			gid = 65535;
#endif
		}

#if DEBUG
	fprintf(stderr, "Group lookup %d -> %d\n", uid, gid);
#endif

	endpwent();

	return (gid);
}

/*
 * Bind to the specified ip and port.
 */
static int bind_to_port(IPaddr_t bind_ip, short bind_port)
{
	struct sockaddr_in	addr;
	int					sock;

	/*
	 * Allocate a socket.
	 */
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		{
			perror("socket()");
			exit(1);
		}

	/*
	 * Set the SO_REUSEADDR option for debugging.
	 */
	{
	 	int	on = 1;
		if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 
			      (char *)&on, sizeof(on)) < 0) {
			perror("setsockopt");
			exit(1);
		}
	}

	/*
	 * Set the address to listen to.
	 */
	addr.sin_family = AF_INET;
	addr.sin_port = htons(bind_port);
	addr.sin_addr.s_addr = bind_ip;

	/*
	 * Bind our socket to the above address.
	 */
	if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
		{
			perror("bind()");
			exit(1);
		}

	/*
	 * Establish a large listen backlog.
	 */
	if (listen(sock, SOMAXCONN) < 0)
		{
			perror("listen()");
			exit(1);
		}

	return (sock);
}

static int sockethandle_now = -1;
  
/*
 * Catch alarm signals and exit.
 */
static void alarm_signal(int a)
{
	if (sockethandle_now != -1) {
		close(sockethandle_now);
		sockethandle_now = -1;
		unlink(XauFileName());
	}
	exit(1);
}


/*
 * This is the main loop when running as a server.
 */
static void server_main_loop(int sock, char **device_name, int n_dev)
{
	struct sockaddr_in	addr;
	unsigned int		len;

	/*
	 * Ignore dead servers so no zombies should be left hanging.
	 */
	signal(SIGCLD, SIG_IGN);

	for (;;) {
		int					new_sock;
		/*
		 * Accept an incoming connection.
		 */
		len = sizeof(addr);
		while ((new_sock = accept(sock, (struct sockaddr *)&addr, &len)) < 0){}
		
		/*
		 * Create a new process to handle the connection.
		 */
#if DEBUG == 0
		switch (fork()) {
			case -1:
				/*
				 * Under load conditions just ignore new connections.
				 */
				break;
				
			case 0:
				/*
				 * Start the proxy work in the new socket.
				 */
#endif
				serve_client(new_sock,device_name, n_dev, 0);
				exit(0);
#if DEBUG == 0
		}
#endif
		/*
		 * Close the socket as the child does the handling.
		 */
		close(new_sock);
		new_sock = -1;
	}
}

/*
 * Print some basic help information.
 */
static void usage(char *prog, const char *opt, int ret)
{
	if (opt)
		{
			fprintf(stderr, "%s: %s\n", prog, opt);
		}
	fprintf(stderr, "usage: %s [-s port [-r user] [-b ipaddr]] devicename [Names of local host]\n", 
			prog);
	fprintf(stderr, "    -d          Run as a server (default port 5703 + DISPLAY)\n");
	fprintf(stderr, "    -s port     Run as a server bound to the specified port.\n");
	fprintf(stderr, "    -r user     Run as the specified user in server mode.\n");
	fprintf(stderr, "    -b ipaddr   Bind to the specified ipaddr in server mode.\n");
	fprintf(stderr, "    -l          Do not attempt to connect to localhost:0 to validate connection\n");
	exit(ret);
}


static char *makeDisplayName(int dispNr)
{
	char result[80];
	sprintf(result, ":%d.0", dispNr);
	return strdup(result);
}

int main (int argc, char** argv) 
{
	int sockfd = 0;
	int			arg;
	int			run_as_server = 0;
	IPaddr_t		bind_ip = INADDR_ANY;
	unsigned short		bind_port = 0;
	uid_t			run_uid = 65535;
	gid_t			run_gid = 65535;
	char*			username = strdup("nobody");
	int			sock;

	char **device_name = NULL; 
	const char *floppy0 = "/dev/fd0";
	int n_dev;


	/*
	 * Parse the command line arguments.
	 */
	if(argc > 1 && !strcmp(argv[0], "--help"))
		usage(argv[0], NULL, 0);
	while ((arg = getopt(argc, argv, "ds:r:b:x:h")) != EOF)
		{
			switch (arg)
				{
					case 'd':
						run_as_server = 1;
						break;
					case 's':
						run_as_server = 1;
						bind_port = getportnum(optarg);
						break;

					case 'r':
						free(username); username = strdup(optarg);
						run_uid = getuserid(optarg);
						run_gid = getgroupid(run_uid);
						break;

					case 'b':
						run_as_server = 1;
						bind_ip = getipaddress(optarg);
						break;
					case 'x':
						dispName = strdup(optarg);
						break;

					case 'h':
						usage(argv[0], NULL, 0);
						break;
					case '?':
						usage(argv[0], NULL, 1);
						break;
				}
		}

	if(optind < argc) {
		device_name = argv + optind;
		n_dev = argc - optind;
	} else {
		device_name = (char **)&floppy0;
		n_dev = 1;
	}

	if(dispName == NULL)
		dispName = getenv("DISPLAY");
	if(dispName==NULL && bind_port != 0)
		dispName=makeDisplayName((unsigned short)(bind_port - 5703));
	if(dispName==NULL)		
		dispName=":0";

	if(bind_port == 0) {
		char *p = strchr(dispName,':');
		bind_port = FLOPPYD_DEFAULT_PORT;
		if(p != NULL)
			bind_port += atoi(p+1);
	}

	if(!run_as_server) {
		struct sockaddr_in	addr;
		unsigned int len = sizeof(addr);
		
		/* try to find out port that we are connected to */
		if(getsockname(0, (struct sockaddr*) &addr, &len) >= 0 && 
		   len == sizeof(addr)) {
			bind_port = ntohs(addr.sin_port);
		}
	}

	umask(0077);

	/*
	 * Test to make sure required args were provided and are valid.
	 */
	if (run_as_server && (bind_ip == INADDR_NONE)) {
		usage(argv[0], "The server ipaddr is invalid.", 1);
	}
	if (run_as_server && (bind_port == 0))	{
		usage(argv[0], "No server port was specified (or it was invalid).", 1);
	}


	/*
	 * See if we should run as a server.
	 */
	if (run_as_server) {
		/*
		 * Start by binding to the port, the child inherits this socket.
		 */
		sock = bind_to_port(bind_ip, bind_port);
		
		/*
		 * Start a server process. When DEBUG is defined, just run
		 * in the foreground.
		 */
#if DEBUG
		switch (0)
#else
			switch (fork())
#endif
				{
				case -1:
					perror("fork()");
					exit(1);
					
				case 0:
					/*
					 * Ignore some signals.
					 */
					signal(SIGHUP, SIG_IGN);
#if DEBUG
					signal(SIGINT, SIG_IGN);
#endif
					signal(SIGQUIT, SIG_IGN);
					signal(SIGTSTP, SIG_IGN);
					signal(SIGCONT, SIG_IGN);
					signal(SIGPIPE, alarm_signal);
					/*signal(SIGALRM, alarm_signal);*/
					
					/*
					 * Drop back to an untrusted user.
					 */
					setgid(run_gid);
					initgroups(username, -1);
					setuid(run_uid);
					
					/*
					 * Start a new session and group.
					 */
					setsid();
#ifdef HAVE_SETPGRP
#ifdef SETPGRP_VOID
					setpgrp();
#else
					setpgrp(0,0);
#endif
#endif
#if DEBUG
					close(2);
					open("/dev/null", O_WRONLY);
#endif
					/*
					 * Handle the server main loop.
					 */
					server_main_loop(sock, device_name, n_dev);
					
					/*
					 * Should never exit.
					 */
					exit(1);
				}
		
		/*
		 * Parent exits at this stage.
		 */
		exit(0);
	}

	signal(SIGHUP, alarm_signal);
#if DEBUG == 0
	signal(SIGINT, alarm_signal);
#endif
	signal(SIGQUIT, alarm_signal);
	signal(SIGTERM, alarm_signal);
	signal(SIGTSTP, SIG_IGN);
	signal(SIGCONT, SIG_IGN);
	signal(SIGPIPE, alarm_signal);
	/*signal(SIGALRM, alarm_signal);*/

	/* Starting from inetd */

	serve_client(sockfd, device_name, n_dev, 1);
	return 0;
}

static void send_reply(int rval, io_buffer sock, int len) {
	Packet reply = newPacket();

	make_new(reply, 8);
	put_dword(reply, 0, len);
	if (rval == -1) {
		put_dword(reply, 4, 0);
	} else {
		put_dword(reply, 4, errno);
	}
	send_packet(reply, sock);
	destroyPacket(reply);
}

static void send_reply64(int rval, io_buffer sock, mt_off_t len) {
	Packet reply = newPacket();

	make_new(reply, 12);
	put_qword(reply, 0, len);
	if (rval == -1) {
		put_dword(reply, 8, 0);
	} else {
		put_dword(reply, 8, errno);
	}
	send_packet(reply, sock);
	destroyPacket(reply);
}

static void cleanup(int x) {
	unlink(XauFileName());
	exit(-1);
}

#include "lockdev.h"

void serve_client(int sockhandle, char **device_name, int n_dev,
		  int close_stderr) {
	Packet opcode;
	Packet parm;

	int readOnly;
	int devFd;
	io_buffer sock;
	int stopLoop;
	int version;
	int needSendReply=0;
	int rval=0;
	
	/*
	 * Set the keepalive socket option to on.
	 */
	{
		int		on = 1;
		if(setsockopt(sockhandle, SOL_SOCKET, 
			      SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0) {
			perror("setsockopt");
			exit(1);
		}
			
	}


#if DEBUG == 0
	if(close_stderr) {
		close(2);
		open("/dev/null", O_WRONLY);
	}
#endif

	sock = new_io_buffer(sockhandle);
	
	/*
	 * Allow 60 seconds for any activity.
	 */
	alarm(60);
	
	version = 0;
	if (!do_auth(sock, &version)) {
		free_io_buffer(sock);
		return;
	}
	alarm(0);


	signal(SIGTERM, cleanup);
	signal(SIGALRM, cleanup);



	sockethandle_now = sockhandle;


	opcode = newPacket();
	parm = newPacket();

	devFd = -1;
	readOnly = 1;

	stopLoop = 0;
	if(version == FLOPPYD_PROTOCOL_VERSION_OLD) {
				/* old protocol */
		readOnly = 0;
		devFd = open(device_name[0], O_RDWR|O_LARGEFILE);
		
		if (devFd < 0) {
			readOnly = 1;
			devFd = open(device_name[0], 
				     O_RDONLY|O_LARGEFILE);
		}
		if(devFd < 0) {
			send_reply(0, sock, devFd);
			stopLoop = 1;
		}
		lock_dev(devFd, !readOnly, NULL);
	}


	while(!stopLoop) {
		int dev_nr = 0;
		/*
		 * Allow 60 seconds for any activity.
		 */
		/*alarm(60);*/

		if (!recv_packet(opcode, sock, 1)) {
			break;
		}
/*		if(opcode->data[0] != OP_CLOSE)*/
		    recv_packet(parm, sock, MAX_DATA_REQUEST);


		switch(opcode->data[0]) {
			case OP_OPRO:
				if(get_length(parm) >= 4)
					dev_nr = get_dword(parm,0);
				else
					dev_nr = 0;
				if(dev_nr >= n_dev) {
					send_reply(0, sock, -1);
					break;
				}

				devFd = open(device_name[dev_nr],
					     O_RDONLY | O_LARGEFILE);
#if DEBUG
				fprintf(stderr, "Device opened\n");
#endif
				if(devFd >= 0 && lock_dev(devFd, 0, NULL)) {
					send_reply(0, sock, -1);
					break;
				}
				send_reply(0, sock, devFd);
				readOnly = 1;
				break;
			case OP_OPRW:
				if(get_length(parm) >= 4)
					dev_nr = get_dword(parm,0);
				else
					dev_nr = 0;
				if(dev_nr >= n_dev) {
					send_reply(0, sock, -1);
					break;
				}
				devFd = open(device_name[dev_nr], O_RDWR);
				if(devFd >= 0 && lock_dev(devFd, 1, NULL)) {
					send_reply(0, sock, -1);
					break;
				}
				send_reply(0, sock, devFd);
				readOnly = 0;
				break;
			case OP_READ:
#if DEBUG
				fprintf(stderr, "READ:\n");
#endif
				read_packet(parm, devFd, get_dword(parm, 0));
				send_reply(devFd, sock, get_length(parm));
				if(get_length(parm) >= 0)
					send_packet(parm, sock);
				break;
			case OP_WRITE:
#if DEBUG
				fprintf(stderr, "WRITE:\n");
#endif
				if(readOnly) {
					errno = -EROFS;
					rval = -1;
				} else {
					rval = write_packet(parm, devFd);
				}
				send_reply(devFd, sock, rval);
				break;
			case OP_SEEK:
#if DEBUG
				fprintf(stderr, "SEEK:\n");
#endif

				lseek(devFd, 
				      get_dword(parm, 0), get_dword(parm, 4));
				send_reply(devFd, 
					   sock, 
					   lseek(devFd, 0, SEEK_CUR));
				break;
			case OP_SEEK64:
				if(sizeof(mt_off_t) < 8) {
#if DEBUG
					fprintf(stderr, "64 bit requested where not available!\n");
#endif
					errno = EINVAL;
					send_reply(devFd, sock, -1);
					break;
				}
#if DEBUG
				fprintf(stderr, "SEEK64:\n");
#endif
				mt_lseek(devFd, 
					 get_qword(parm,0), get_dword(parm,8));
				send_reply64(devFd, 
					     sock, 
					     mt_lseek(devFd, 0, SEEK_CUR));
				break;
			case OP_FLUSH:
#if DEBUG
				fprintf(stderr, "FLUSH:\n");
#endif
				fsync(devFd);
				send_reply(devFd, sock, 0);
				break;
			case OP_CLOSE:
#if DEBUG
				fprintf(stderr, "CLOSE:\n");
#endif

				close(devFd);
				needSendReply = 1;
				rval = devFd;
				devFd = -1;
				stopLoop = 1;
				break;
			case OP_IOCTL:
				/* Unimplemented for now... */
				break;
			default:
#if DEBUG
				fprintf(stderr, "Invalid Opcode!\n");
#endif
				errno = EINVAL;
				send_reply(devFd, sock, -1);
				break;
		}
		kill_packet(parm);
		alarm(0);
	}

	

#if DEBUG
	fprintf(stderr, "Closing down...\n");
#endif

	if (devFd >= 0) {
		close(devFd);
		devFd = -1;
	}

	free_io_buffer(sock);

	/* remove "Lock"-File  */
	unlink(XauFileName());

	if(needSendReply)
	    send_reply(rval, sock, 0);
	destroyPacket(opcode);
	destroyPacket(parm);
}

#else
#include <stdio.h>

int main(int argc, char **argv) 
{
	puts("Floppyd support not included!");
	return -1;
}
	
#endif