1 static char sccsid[] = "@(#)setup.c 4.1 (Berkeley) 09/12/82"; 2 3 /* 4 setup.c 5 6 support procedures used in setting up the network 7 8 */ 9 10 # include "defs.h" 11 12 char logfile[] = LOGFILE; 13 14 /* global variables */ 15 struct daemonparms netd; 16 17 /* 18 called in netdaemon and debugging software 19 handles parameter lists to setup 20 remote machine and pipes 21 */ 22 setupdaemon(argc,argv) 23 char **argv;{ 24 long timev; 25 int timei; 26 FILE *cfile; 27 28 parseargs(argc,argv); 29 30 cfile = fopen(INITFILE,"r"); 31 rdnetfile(cfile); 32 fclose(cfile); 33 err("remote %c local %c link %s inspeed %d outspeed %d length %d\n", 34 remote,local,netd.dp_device,netd.dp_inspeed, 35 netd.dp_outspeed,netd.dp_datasize); 36 err("debug %d time %d count %d onlyuid %d usehispeed=%d hispeedlink='%s'\n", 37 debugflg,netd.dp_oatime, netd.dp_maxbread,netd.dp_onlyuid, 38 netd.dp_usehispeed, netd.dp_hispeedlink); 39 err("sendonly %c rcvonly %c pipesim %c\n", 40 chfromf(netd.dp_sndorcv < 0),chfromf(netd.dp_sndorcv > 0), 41 chfromf(netd.dp_pipesim)); 42 setup(netd.dp_device); 43 timev = gettime(); 44 timei = timev >> 16; 45 srand(timei); 46 } 47 /* 48 49 see comment in netdaemon.c about the arguments 50 51 */ 52 parseargs(argc,argv) 53 char **argv; { 54 char stemp[30]; 55 remote = 0; 56 while(argc > 1 && argv[1][0] == '-'){ 57 argc--; argv++; 58 switch(argv[0][1]){ 59 case '8': 60 netd.dp_use8bit = 1; 61 break; 62 case 'd': 63 debugflg = 1; 64 break; 65 case 'h': 66 netd.dp_usehispeed = 1; 67 break; 68 case 'l': 69 netd.dp_trynetl = 0; 70 break; 71 case 'm': 72 harg(stemp); 73 remote = lookup(stemp); 74 break; 75 case 'o': /* only */ 76 if(argv[0][2] == 's') /* only send */ 77 netd.dp_sndorcv = -1; 78 else if(argv[0][2] == 'r') /* only receive */ 79 netd.dp_sndorcv = 1; 80 else if(argv[0][2] == 'u') /* only uid num */ 81 netd.dp_onlyuid = atoi(argv[1]); 82 break; 83 case 'p': 84 harg(stemp); 85 netd.dp_datasize = atol(stemp); 86 break; 87 case 'r': 88 harg(stemp); 89 netd.dp_rdfile = fdopen(atoi(stemp),"r"); 90 netd.dp_pipesim++; 91 break; 92 case 'w': 93 harg(stemp); 94 netd.dp_pwritefd = atoi(stemp); 95 netd.dp_pipesim++; 96 break; 97 /* ignore unknown options */ 98 } 99 } 100 if(remote == 0){ 101 fprintf(stderr,"Error- must specify machine - use -m option\n"); 102 exit(EX_USAGE); 103 } 104 } 105 /* 106 set the correct mode on the link device 107 */ 108 setup(str) 109 char *str; { 110 struct sgttyb stt; 111 # ifdef RAND 112 struct { 113 int t_xflags; 114 char t_col; 115 char t_delct; 116 char t_outqc_cc; 117 char t_rawqc_cc; 118 } exstt; 119 #define OUT8BIT 01 /* All 8 bits on output */ 120 #define IN8BIT 02 /* All 8 bits on input */ 121 # endif 122 123 initseqno(); 124 /* nothing to set up if we're simulating with pipes */ 125 if(netd.dp_pipesim)return; 126 127 if(netd.dp_usehispeed){ 128 str = netd.dp_hispeedlink; 129 netd.dp_datasize = SENDLEN - ACKLENGTH; 130 } 131 if(str == 0 || str[0] == 0){ 132 err("invalid net device\n"); 133 exit(EX_OSFILE); 134 } 135 netd.dp_linefd = open(str,2); 136 if(netd.dp_linefd < 0){ 137 perror(str); 138 exit(EX_OSERR); 139 } 140 /* set exclusive use for line */ 141 if(ioctl(netd.dp_linefd,TIOCEXCL,&stt) != 0 || 142 gtty(netd.dp_linefd,&stt) < 0){ 143 perror(str); 144 exit(EX_OSERR); 145 } 146 stt.sg_ispeed = netd.dp_inspeed; /* user set baud */ 147 stt.sg_ospeed = netd.dp_outspeed; /* user-set baud */ 148 stt.sg_erase = stt.sg_kill = 0; /* erase and kill off */ 149 stt.sg_flags = ANYP; /* even and odd parity, off everything else */ 150 if(stty(netd.dp_linefd,&stt) < 0){ 151 perror(str); 152 exit(EX_OSERR); 153 } 154 # ifdef RAND 155 /* set device into 8-bit mode */ 156 if(gtty((2<<8)|netd.dp_linefd,&exstt) < 0){ 157 perror(str); 158 exit(EX_OSERR); 159 } 160 exstt.t_xflags = OUT8BIT | IN8BIT; 161 if(stty((2<<8)|netd.dp_linefd, &exstt) < 0){ 162 perror(str); 163 exit(EX_OSERR); 164 } 165 # endif 166 /* set my own line discipline */ 167 /* NETLDISC is defined in sgtty.h on the CSVAX */ 168 /* setting the line discipline must be done AFTER the sttys */ 169 # ifdef NETLDISC 170 if(netd.dp_trynetl){ 171 netd.dp_linedis = NETLDISC; 172 if(ioctl(netd.dp_linefd,TIOCSETD,&netd.dp_linedis) != 0){ 173 printf("error - line discipline\n"); 174 perror(str); 175 printf("proceeding...\n"); 176 netd.dp_linedis = 0; 177 } 178 if(netd.dp_linedis){ 179 /* set the line into RAW mode */ 180 netd.dp_linedis = 0; 181 ioctl(netd.dp_linefd,TIOCSETD,&netd.dp_linedis); 182 netd.dp_linedis = NETLDISC; 183 stt.sg_ispeed = netd.dp_inspeed;/* user set baud */ 184 stt.sg_ospeed = netd.dp_outspeed; /* user-set baud */ 185 stt.sg_erase = stt.sg_kill = 0; 186 stt.sg_flags = ANYP|RAW; /* in raw mode */ 187 if(stty(netd.dp_linefd,&stt) < 0){ 188 perror(str); 189 exit(EX_OSERR); 190 } 191 ioctl(netd.dp_linefd,TIOCSETD,&netd.dp_linedis); 192 printf("Using network line discipline.\n"); 193 } 194 } 195 # endif 196 } 197 /*VARARGS0*/ 198 error(s,a,b,c,d,e,f,g,h) 199 char *s; { 200 char buf[10]; 201 if(remote != 0) sprintf(buf,"%s",longname(remote)); 202 else buf[0] = 0; 203 fflush(stdout); 204 if(debugflg){ 205 fprintf(stderr,s,a,b,c,d,e,f,g,h); 206 putc('\n',stderr); 207 } 208 addtolog(remote,"Err %s: ",buf); 209 addtolog(remote,s,a,b,c,d,e,f,g,h); 210 addtolog(remote,"\n"); 211 } 212 /* this is really not right - we should use the rcslog format */ 213 /* also, the user must be able to write on the 214 public logfile to get error messages such as 215 directory not found after he has 216 setuid'd from root 217 */ 218 /*VARARGS0*/ 219 addtolog(mach,s,a,b,c,d,e,f,g,h,i,j,k,l,m,n) 220 char *s; 221 { 222 static FILE *log = NULL; 223 struct stat statbuf; 224 logfile[strlen(logfile)-1] = mach; 225 if(log == NULL){ 226 if(stat(logfile,&statbuf) < 0)return; 227 log = fopen(logfile,"a"); 228 } 229 if(log == NULL)return; 230 fseek(log,0L,2); 231 fprintf(log,s,a,b,c,d,e,f,g,h,i,j,k,l,m,n); 232 fflush(log); 233 debug(s,a,b,c,d,e,f,g,h,i,h,k,l,m,n); 234 } 235