1772
|
1 /* openuucp.c Establish a uucp connection with the remote.
|
|
2 Copyright (C) 1990, 1993 Rick Adams and Bob Billson
|
|
3
|
|
4 This file is part of the OS-9 UUCP package, UUCPbb.
|
|
5
|
|
6 This program is free software; you can redistribute it and/or modify
|
|
7 it under the terms of the GNU General Public License as published by
|
|
8 the Free Software Foundation; either version 2 of the License, or
|
|
9 (at your option) any later version.
|
|
10
|
|
11 This program is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 GNU General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with this program; if not, write to the Free Software
|
|
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
19
|
|
20 The author of UUCPbb, Bob Billson, can be contacted at:
|
|
21 bob@kc2wz.bubble.org or uunet!kc2wz!bob or by snail mail:
|
|
22 21 Bates Way, Westfield, NJ 07090
|
|
23 */
|
|
24
|
|
25 #include "uucp.h"
|
|
26 #include "uucico.h"
|
|
27 #include <ctype.h>
|
|
28
|
|
29 #define DLE '\x10' /* DLE character */
|
|
30
|
|
31 static char *handshake_ok = "Handshake successful",
|
|
32 *no_proto = "no supported protocol",
|
|
33 *startG = "start g protocol handshake",
|
|
34 *whoweare = "telling remote who we are: ";
|
|
35
|
|
36
|
|
37 /**********************************\
|
|
38 * open uucp session in master mode *
|
|
39 \**********************************/
|
|
40
|
|
41 int mopenuucp()
|
|
42 {
|
|
43 char string[200];
|
|
44 register char *p;
|
|
45 int i;
|
|
46
|
|
47 p = string;
|
|
48
|
|
49 if (debuglvl > 0)
|
|
50 fprintf (log, "mopenuucp: %s\n", startG);
|
|
51
|
|
52 /* receive "Shere" from remote */
|
|
53 if (recv0 (p) == TIMEDOUT)
|
|
54 {
|
|
55 logerror ("mopenuucp: remote didn't send Shere");
|
|
56 return (ABORT);
|
|
57 }
|
|
58
|
|
59 if (strncmp (p, "Shere", 5) != 0)
|
|
60 {
|
|
61 logerror ("mopenuucp: Bad Shere string from remote");
|
|
62
|
|
63 if (debuglvl > 3)
|
|
64 fprintf (log, "mopenuucp: remote sent: %s\n", p);
|
|
65
|
|
66 return (ABORT);
|
|
67 }
|
|
68
|
|
69 if (debuglvl > 0)
|
|
70 fputs ("mopenuucp: got Shere\n", log);
|
|
71
|
|
72 lognorm ("Login successful");
|
|
73
|
|
74 /* called the right remote, right? */
|
|
75 if (*(p+5) == '=')
|
|
76 {
|
|
77 if (strncmp (p+6, sysname, strlen (sysname)) != 0) /* oops!! */
|
|
78 {
|
|
79 char tmp[60];
|
|
80
|
|
81 sprintf (tmp, "called wrong system (%s)", p+6);
|
|
82 logerror (tmp);
|
|
83 return (ABORT);
|
|
84 }
|
|
85 }
|
|
86 else if (*(p+5) != '\0')
|
|
87 {
|
|
88 char tmp[65];
|
|
89
|
|
90 sprintf (tmp, "mopenuucp: Strange Shere: %s", p);
|
|
91 logerror (tmp);
|
|
92 }
|
|
93
|
|
94 /* tell 'em who we are */
|
|
95 sprintf (p, "S%s", nodename);
|
|
96
|
|
97 if (debuglvl > 0)
|
|
98 fprintf (log, "mopenuucp: %s%s\n", whoweare, p);
|
|
99
|
|
100 send0 (p);
|
|
101
|
|
102 /* find out what remote thinks of us */
|
|
103 if (recv0 (p) == TIMEDOUT)
|
|
104 return (ABORT);
|
|
105
|
|
106 if (*p != 'R')
|
|
107 {
|
|
108 char tmp[128];
|
|
109
|
|
110 sprintf (tmp, "Bad response to handshake string (%s)", p);
|
|
111 logerror (tmp);
|
|
112 return (ABORT);
|
|
113 }
|
|
114
|
|
115 if (strncmp (p+1, "OK", 2) == 0)
|
|
116 {
|
|
117 if (debuglvl > 0)
|
|
118 fputs ("mopenuucp: remote says we are OK\n", log);
|
|
119 }
|
|
120 else if (strcmp (p+1, "LCK") == 0)
|
|
121 {
|
|
122 logerror ("Remote says we are already talking");
|
|
123 return (ABORT);
|
|
124 }
|
|
125 else if (strcmp (p+1, "CB") == 0)
|
|
126 {
|
|
127 logerror ("Remote will call back");
|
|
128 return (ABORT);
|
|
129 }
|
|
130 else if (strcmp (p+1, "BADSEQ") == 0)
|
|
131 {
|
|
132 logerror ("bad sequence number");
|
|
133 return (ABORT);
|
|
134 }
|
|
135 else
|
|
136 {
|
|
137 char tmp[65];
|
|
138
|
|
139 sprintf (tmp, "Handshake failed (%s)", p+1);
|
|
140 logerror (tmp);
|
|
141 }
|
|
142
|
|
143 /* receive list of supported protocols from remote */
|
|
144 if (recv0 (p) == TIMEDOUT)
|
|
145 return (ABORT);
|
|
146
|
|
147 /* is "g" protocol supported? */
|
|
148 if (*p != 'P')
|
|
149 {
|
|
150 logerror ("mopenuucp: Bad protocol handshake");
|
|
151 return (ABORT);
|
|
152 }
|
|
153
|
|
154 if (strstr (p, "g") == NULL)
|
|
155 {
|
|
156 send0 ("UN");
|
|
157 logerror (no_proto);
|
|
158 return (ABORT);
|
|
159 }
|
|
160
|
|
161 /* tell 'em to use g protocol */
|
|
162 protocol = 'g';
|
|
163 sprintf (p, "U%c", protocol);
|
|
164 send0 (p);
|
|
165 sprintf (p, "%s (protocol '%c')", handshake_ok, protocol);
|
|
166 lognorm (p);
|
|
167 return (MS_SNDRCV);
|
|
168 }
|
|
169
|
|
170
|
|
171
|
|
172 /*********************************\
|
|
173 * open uucp session in slave mode *
|
|
174 \*********************************/
|
|
175
|
|
176 int sopenuucp()
|
|
177 {
|
|
178 char string[200];
|
|
179 register char *p;
|
|
180 char *incoming = "Incoming call from ";
|
|
181
|
|
182 p = string;
|
|
183
|
|
184 if (debuglvl > 0)
|
|
185 fprintf (log, "sopenuucp: %s\n", startG);
|
|
186
|
|
187 /* send "Shere" to remote */
|
|
188 sprintf (p, "Shere=%s", nodename);
|
|
189
|
|
190 if (debuglvl > 0)
|
|
191 fprintf (log, "sopenuucp: %s%s\n", whoweare, p);
|
|
192
|
|
193 send0 (p);
|
|
194
|
|
195 /* find out who they are */
|
|
196 if (recv0 (p) == TIMEDOUT)
|
|
197 return (ABORT);
|
|
198
|
|
199 if (*p != 'S')
|
|
200 {
|
|
201 strcpy (sysname, "unknown");
|
|
202 sprintf (p, "%s%s (port %s)", incoming, sysname, device);
|
|
203 lognorm (p);
|
|
204 logerror ("sopenuucp: Bad introduction string");
|
|
205 return (ABORT);
|
|
206 }
|
|
207
|
|
208 if (debuglvl > 0)
|
|
209 fputs ("sopenuucp: got remote's S<hostname>\n", log);
|
|
210
|
|
211 /* isolate system name */
|
|
212 if ((p = strchr (string, ' ')) != NULL)
|
|
213 *p = '\0';
|
|
214
|
|
215 p = string;
|
|
216 strcpy (sysname, p+1);
|
|
217
|
|
218 /* log the connection */
|
|
219 sprintf (p, "%s%s on port %s",
|
|
220 incoming, sysname, *device != '\0' ? device : "unknown");
|
|
221 lognorm (p);
|
|
222
|
|
223 /* Are they allowed in here? */
|
|
224 if (SystemIsOK (FALSE) == FALSE)
|
|
225 {
|
|
226 send0 ("RYou are unknown to me");
|
|
227 sprintf (p, "WARNING--call from unknown system: %s", sysname);
|
|
228 lognorm (p);
|
|
229 return (ABORT);
|
|
230 }
|
|
231
|
|
232 /* For now we don't tell the remote we will call them back or reject
|
|
233 the login otherwise. Instead, we tell remote that all is fine. */
|
|
234 send0 ("ROK");
|
|
235
|
|
236 /* send list of supported protocols to remote */
|
|
237 send0 ("Pg");
|
|
238
|
|
239 /* is "g" protocol okay? */
|
|
240 if (recv0 (p) == TIMEDOUT)
|
|
241 return (ABORT);
|
|
242
|
|
243 /* Ug if okay, UN if not */
|
|
244 if (*p != 'U' || *(p+2) != '\0')
|
|
245 {
|
|
246 logerror ("sopenuucp: Bad protocol response string");
|
|
247 return (ABORT);
|
|
248 }
|
|
249
|
|
250 if (*(p+1) == 'N')
|
|
251 {
|
|
252 logerror ("sopenuucp: No supported protocol");
|
|
253 return (ABORT);
|
|
254 }
|
|
255 protocol = *(p+1);
|
|
256 sprintf (string, "%s (protocol '%c')", handshake_ok, protocol);
|
|
257 lognorm (string);
|
|
258 return (MS_SNDRCV);
|
|
259 }
|
|
260
|
|
261
|
|
262
|
|
263 /* Is this valid system? The remote's name is in the global variable
|
|
264 'sysname'. TRUE is returned if we talk to the remote. FALSE if not or on
|
|
265 error. */
|
|
266
|
|
267 int SystemIsOK (quitonerror)
|
|
268 int quitonerror;
|
|
269 {
|
|
270 char buff[SYSLINE];
|
|
271 int sysnamlen = strlen (sysname);
|
|
272 register char *p;
|
|
273 char *p1;
|
|
274 FILE *fpsys;
|
|
275
|
|
276 p = buff;
|
|
277
|
|
278 if ((fpsys = fopen (SYSTEMS, "r")) == NULL)
|
|
279 {
|
|
280 openlog();
|
|
281 strcpy (p, "SystemIsOk: can't open Systems file");
|
|
282 logerror (p);
|
|
283 closelog();
|
|
284
|
|
285 if (quitonerror)
|
|
286 fatal (p);
|
|
287 else
|
|
288 return (FALSE);
|
|
289 }
|
|
290
|
|
291 /* ignore comment lines beginning with #, <space>, <tab> or CR */
|
|
292 while (mfgets (p, SYSLINE, fpsys) != NULL)
|
|
293 if (ISCOMMENT (*p) == FALSE)
|
|
294 {
|
|
295 /* isolate system name */
|
|
296 if ((p1 = strchr (p, ' ')) != NULL)
|
|
297 *p1 = '\0';
|
|
298
|
|
299 if (strucmp (sysname, p) == 0)
|
|
300 {
|
|
301 fclose (fpsys);
|
|
302 return (TRUE);
|
|
303 }
|
|
304 }
|
|
305
|
|
306 /* no entry for this system */
|
|
307 fclose (fpsys);
|
|
308 sprintf (p, "no entry in Systems file for: %s", sysname);
|
|
309 openlog();
|
|
310 logerror (p);
|
|
311
|
|
312 if (!quiet && log != stderr)
|
|
313 fprintf (stderr, "%s\n\n", buff);
|
|
314
|
|
315 return (FALSE);
|
|
316 }
|
|
317
|
|
318
|
|
319
|
|
320 /* Receive a NULL terminated string. This is one used when no protocol is
|
|
321 being used. The basic idea for this is borrowed from Taylor (GNU) UUCP. */
|
|
322
|
|
323 int recv0 (string)
|
|
324 char *string;
|
|
325 {
|
|
326 register char *p;
|
|
327 char c;
|
|
328 flag gotDLE = FALSE;
|
|
329
|
|
330 if (debuglvl > 7)
|
|
331 fputs ("<< ", log);
|
|
332
|
|
333 /* Keep reading the port until we time out or get an ending null */
|
|
334 p = string;
|
|
335 while (readport (&c, PKTTIME, MAXTRY) != TIMEDOUT)
|
|
336 {
|
|
337 /* strip any parity bit */
|
|
338 c &= 0x7f;
|
|
339
|
|
340 /* Log the byte if the debug level is high enough. Don't bother
|
|
341 logging the terminating null since we know it must be there. */
|
|
342
|
|
343 if (debuglvl > 7)
|
|
344 if (gotDLE && c == '\0')
|
|
345 ;
|
|
346 else
|
|
347 chardump (c);
|
|
348
|
|
349 /* Look for the DLE to indicate the start of a null-terminated
|
|
350 message. We ignored everything until we get the DLE. */
|
|
351 if (!gotDLE)
|
|
352 {
|
|
353 if (c == DLE)
|
|
354 gotDLE = TRUE;
|
|
355
|
|
356 continue;
|
|
357 }
|
|
358
|
|
359 /* We got the DLE, so start assembling the string until we get a
|
|
360 NULL signaling the end. */
|
|
361
|
|
362 /* Huh? Another DLE? Something happened here. Act like it is the
|
|
363 first one we saw. */
|
|
364 if (c == DLE)
|
|
365 {
|
|
366 p = string;
|
|
367 continue;
|
|
368 }
|
|
369
|
|
370 /* Some systems send trailing \n on Shere line. This is a definite
|
|
371 no-no and should not occur...but... This mod should be safe. */ if (c == '\x0d' || c == '\x0a')
|
|
372 c = '\0';
|
|
373
|
|
374 *p++ = c;
|
|
375
|
|
376 if (c == '\0')
|
|
377 {
|
|
378 if (debuglvl > 7)
|
|
379 putc ('\n', log);
|
|
380
|
|
381 return (TRUE);
|
|
382 }
|
|
383 }
|
|
384 /* we must have timed out */
|
|
385 return (TIMEDOUT);
|
|
386 }
|
|
387
|
|
388
|
|
389
|
|
390 /* Send a NULL terminated string. This is only used when no protocol is
|
|
391 being used. */
|
|
392
|
|
393 int send0 (string)
|
|
394 register char *string;
|
|
395 {
|
|
396 int lport, result, sendlen = strlen (string) + 1;
|
|
397
|
|
398 lport = port == 0 ? 1 : port;
|
|
399
|
|
400 if (debuglvl > 7)
|
|
401 {
|
|
402 fputs (">> [$10]", log);
|
|
403 strdump (string);
|
|
404 }
|
|
405
|
|
406 /* send DLE... */
|
|
407 if ((result = write (lport, "\x10", 1)) != 1)
|
|
408 if (debuglvl > 0)
|
|
409 logerror ("send0: didn't write enough data, probably won't write the rest either.");
|
|
410
|
|
411 /* ...and the rest of the string including its ending \0 */
|
|
412 result = write (lport, string, sendlen);
|
|
413 return (result == sendlen ? TRUE : FALSE);
|
|
414 }
|
|
415
|
|
416
|
|
417
|
|
418 /* dump out string for diagnostic output */
|
|
419
|
|
420 int strdump (string)
|
|
421 char *string;
|
|
422 {
|
|
423 register char *c;
|
|
424
|
|
425 for (c = string; *c != '\0'; ++c)
|
|
426 chardump (*c);
|
|
427
|
|
428 putc ('\n', log);
|
|
429 }
|
|
430
|
|
431
|
|
432
|
|
433 /* Dump out a char for diagnostic output. This is called by strdump() above
|
|
434 and recv0(). */
|
|
435
|
|
436 int chardump (c)
|
|
437 register char c;
|
|
438 {
|
|
439 if (isprint (c))
|
|
440 {
|
|
441 if (c == '\x0d')
|
|
442 fputs ("<CR>", log);
|
|
443 else if (c == '\x0a')
|
|
444 fputs ("<LF>", log);
|
|
445 else
|
|
446 fprintf (log, "%c", c);
|
|
447 }
|
|
448 else
|
|
449 fprintf (log, "[$%02x]", c & 0xff);
|
|
450 }
|