444
/* CPhipps 2000/02/11 - Do the open and test in one call, to avoid
445
* race condition. Open with safe permissions. */
446
fd = open(XauFileName(), O_WRONLY | O_CREAT | O_EXCL, 0600);
445
fd = mkstemp(authFile);
447
/* Different error than file exists */
450
448
put_dword(reply, 0, AUTH_DEVLOCKED);
451
449
send_packet(reply, sock);
784
775
int run_as_server = 0;
785
776
IPaddr_t bind_ip = INADDR_ANY;
786
short bind_port = FLOPPYD_DEFAULT_PORT;
777
unsigned short bind_port = 0;
787
778
uid_t run_uid = 65535;
788
779
gid_t run_gid = 65535;
789
780
char* username = strdup("nobody");
791
782
int port_is_supplied = 0;
794
784
char *server_hostname=NULL;
795
char* device_name = NULL;
798
const char *authKey = "XAUTHORITY";
785
char **device_name = NULL;
786
char *floppy0 = "/dev/fd0";
802
791
* Parse the command line arguments.
804
while ((arg = getopt(argc, argv, "lds:r:b:")) != EOF)
793
while ((arg = getopt(argc, argv, "ds:r:b:x:")) != EOF)
839
827
if(optind < argc) {
840
device_name = argv[optind++];
828
device_name = argv + optind;
829
n_dev = argc - optind;
831
device_name = &floppy0;
844
fprintf(stderr, "Ignoring extra argument %s\n", argv[optind]);
836
dispName = getenv("DISPLAY");
837
if(dispName==NULL && bind_port != 0)
838
dispName=makeDisplayName((unsigned short)(bind_port - 5703));
843
char *p = strchr(dispName,':');
844
bind_port = FLOPPYD_DEFAULT_PORT;
846
bind_port += atoi(p+1);
847
849
if(!run_as_server) {
865
867
if (run_as_server && (bind_ip == INADDR_NONE)) {
866
868
usage(argv[0], "The server ipaddr is invalid.");
868
if (run_as_server && (bind_port == -1)) {
870
if (run_as_server && (bind_port == 0)) {
869
871
usage(argv[0], "No server port was specified (or it was invalid).");
873
device_name = "/dev/fd0";
876
if (!device_name || !*device_name) {
877
usage(argv[0], "No device name specified.");
880
authFile = malloc(strlen(authKey) + 1+ strlen(device_name)*2+7);
881
strcpy(authFile, authKey);
882
strcat(authFile, "=/tmp/");
883
for(p=device_name, q = strlen(authKey) + authFile+6; *p; p++) {
901
876
* See if we should run as a server.
906
* Start by binding to the port, the child inherits this socket.
908
sock = bind_to_port(bind_ip, bind_port);
911
* Start a server process. When DEBUG is defined, just run
880
* Start by binding to the port, the child inherits this socket.
882
sock = bind_to_port(bind_ip, bind_port);
885
* Start a server process. When DEBUG is defined, just run
926
* Ignore some signals.
928
signal(SIGHUP, SIG_IGN);
900
* Ignore some signals.
902
signal(SIGHUP, SIG_IGN);
930
signal(SIGINT, SIG_IGN);
904
signal(SIGINT, SIG_IGN);
932
signal(SIGQUIT, SIG_IGN);
933
signal(SIGTSTP, SIG_IGN);
934
signal(SIGCONT, SIG_IGN);
935
signal(SIGPIPE, alarm_signal);
936
/*signal(SIGALRM, alarm_signal);*/
939
* Drop back to an untrusted user.
942
initgroups(username, -1);
946
* Start a new session and group.
906
signal(SIGQUIT, SIG_IGN);
907
signal(SIGTSTP, SIG_IGN);
908
signal(SIGCONT, SIG_IGN);
909
signal(SIGPIPE, alarm_signal);
910
/*signal(SIGALRM, alarm_signal);*/
913
* Drop back to an untrusted user.
916
initgroups(username, -1);
920
* Start a new session and group.
949
923
#ifdef SETPGRP_VOID
956
open("/dev/null", O_WRONLY);
930
open("/dev/null", O_WRONLY);
959
* Handle the server main loop.
961
server_main_loop(sock, device_name);
970
* Parent exits at this stage.
933
* Handle the server main loop.
935
server_main_loop(sock, device_name, n_dev);
944
* Parent exits at this stage.
975
949
signal(SIGHUP, alarm_signal);
1066
1042
if(version == FLOPPYD_PROTOCOL_VERSION_OLD) {
1067
1043
/* old protocol */
1069
devFd = open(device_name, O_RDWR);
1045
devFd = open(device_name[0], O_RDWR);
1071
1047
if (devFd < 0) {
1073
devFd = open(device_name,
1049
devFd = open(device_name[0],
1076
1052
if(devFd < 0) {
1077
1053
send_reply(0, sock, devFd);
1056
lock_dev(devFd, !readOnly, NULL);
1083
1060
while(!stopLoop) {
1086
1064
* Allow 60 seconds for any activity.
1090
1068
if (!recv_packet(opcode, sock, 1)) {
1091
fprintf(stderr, "No Recv packet\n");
1094
1071
/* if(opcode->data[0] != OP_CLOSE)*/
1095
1072
recv_packet(parm, sock, MAX_DATA_REQUEST);
1097
1075
switch(opcode->data[0]) {
1099
devFd = open(device_name, O_RDONLY);
1077
if(get_length(parm) >= 4)
1078
dev_nr = get_dword(parm,0);
1081
if(dev_nr >= n_dev) {
1082
send_reply(0, sock, -1);
1086
devFd = open(device_name[dev_nr], O_RDONLY);
1101
1088
fprintf(stderr, "Device opened\n");
1090
if(devFd >= 0 && lock_dev(devFd, 0, NULL)) {
1091
send_reply(0, sock, -1);
1103
1094
send_reply(0, sock, devFd);
1107
devFd = open(device_name, O_RDWR);
1098
if(get_length(parm) >= 4)
1099
dev_nr = get_dword(parm,0);
1102
if(dev_nr >= n_dev) {
1103
send_reply(0, sock, -1);
1106
devFd = open(device_name[dev_nr], O_RDWR);
1107
if(devFd >= 0 && lock_dev(devFd, 1, NULL)) {
1108
send_reply(0, sock, -1);
1108
1111
send_reply(0, sock, devFd);