Debian Codegraph @

tcpip/ttylinkd.c

raw
1 /*
2 * ttylinkd: A ttylink daemon using the ntalkd protocol.
3 * Copyright (C) 1996,1197,2000 Craig Small ([email protected])
4 *
5 * This program is free software ; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
18 * 02110-1301, USA.
19 */
20 /*
21 * Versions:
22 * 13/01/96 cs Original Version
23 * 29/01/96 cs Added AX.25 proper support, thanks Tomi OH2BNS!!
24 * 27/01/97 cs Added support for stdin/stdout.
25 * 04/03/97 cs Added config file, no more defines.
26 * 09/03/97 tjd Enhanced to allow specifying [email protected].
27 * rather than be confined to localhost.
28 * 04/01/00 cs Minor stuff with email addresses
29 */
30 #include <ctype.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <netdb.h>
36 #include <unistd.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39
40 #include <config.h>
41 #include <scm-version.h>
42
43 #include <protocols/talkd.h>
44
45 #include <syslog.h>
46 #include <sys/types.h>
47 #include <sys/errno.h>
48 #include <sys/time.h>
49 #include <sys/ioctl.h>
50
51 #include <sys/socket.h>
52 #include <netax25/ax25.h>
53 #include <netrose/rose.h>
54
55 #include "../pathnames.h"
56
57
58 static char version[] = "ttylink daemon (Version " FULL_VER ") ready.\n";
59
60 #define SYSOP_USER "root"
61
62 /* TTY control characters from the sysop's talk screen */
63 static char erasec;
64 static char killc;
65 static char werasec;
66
67 /* Users address family */
68 static int userfamily;
69
70 #define ADDR_SIZE 256
71 static char sysop_addr[ADDR_SIZE];
72 static char *sysop_user=NULL, *sysop_host=NULL;
73 static char config_file[128];
74
75 static int send_control(int skt, struct in_addr addr, CTL_MSG msg, CTL_RESPONSE *resp);
76 static void alarm_handle(int i);
77 static void do_talk(int skt);
78 static void read_config_file(int dummy);
79
80 /*static char *Commands[] = {
81 "leaving invitation",
82 "look-up",
83 "delete",
84 "announce" };
85 */
86 static char *Responses[] = {
87 "success",
88 "sysop not logged on",
89 "failed for unknown reason",
90 "sysop's machine unknown",
91 "permission denied",
92 "unknown request",
93 "bad version",
94 "bad address",
95 "bad control address" };
96
97
98 int main(int argc, char *argv[])
99 {
100 struct sockaddr_in ctl_sin;
101 struct in_addr my_addr, rem_addr;
102 int ctl_skt, skt, new_skt;
103 CTL_RESPONSE resp;
104 CTL_MSG msg;
105 struct protoent *ppe;
106 struct hostent *phe, *rhe=NULL;
107 char hostname[256];
108 int local_id, remote_id;
109 char buf[256];
110 char user[NAME_SIZE];
111 struct sockaddr sa, msg_sa;
112 struct sockaddr_in *peer_sin=NULL, *msg_sin;
113 struct sockaddr_ax25 *peer_sax;
114 struct sockaddr_rose *peer_srose;
115 socklen_t sa_len, length;
116 int i;
117
118 /* Open up the system logger */
119 openlog(argv[0], LOG_PID, LOG_DAEMON);
120
121 write(STDOUT_FILENO, version, strlen(version));
122
123 /* Work out who is calling us */
124 userfamily = AF_UNSPEC;
125 memset(user, 0, NAME_SIZE);
126 strcpy(sysop_addr, SYSOP_USER);
127 strcpy(config_file, CONF_TTYLINKD_FILE);
128 for (i=1 ; i < argc ; i++)
129 {
130 if (argv[i][0] == '-')
131 {
132 switch (argv[i][1])
133 {
134 case 'v':
135 return 0;
136 break;
137 case 'h':
138 printf("ttylinkd comes with ABSOLUTELY NO WARRANY, see the file COPYING\n");
139 printf("This is free software, and you are welcome to redistribute it\n");
140 printf("under the terms of the GNU General Public License.\n\n");
141 printf("Usage: %s [-hv] [-c <callsign>] [-f <config_file>]\n", argv[0]);
142 return 0;
143 break;
144 case 'f':
145 if (i+2 > argc)
146 {
147 printf("The -f flag needs a parameter.\n");
148 return 0;
149 break;
150 }
151 strncpy(config_file, argv[++i], 127);
152 break;
153 case 'c':
154 if ( i+2 > argc)
155 {
156 printf("The -c flag need a parameter.\n");
157 return 0;
158 break;
159 }
160 strncpy(user, argv[++i], NAME_SIZE);
161 break;
162 default:
163 fprintf(stderr, "%s: Unknown flag, type %s -h for help\n", argv[0], argv[0]);
164 return -1;
165 break;
166 } /*switch */
167 } /* - */
168 } /* for */
169 if (user[0] == '\0')
170 {
171 sa_len = sizeof(sa);
172 if (getpeername(STDOUT_FILENO, &sa, &sa_len) < 0)
173 {
174 fprintf(stderr, "%s: getpeername() failed, you must specify a callsign in stdin mode.\n", argv[0]);
175 syslog(LOG_CRIT | LOG_DAEMON, "main(): getpeername() failed.");
176 return 0;
177 } else {
178 userfamily = sa.sa_family;
179 switch (sa.sa_family) {
180 case AF_INET:
181 peer_sin = (struct sockaddr_in*)&sa;
182 write(STDOUT_FILENO, buf, strlen(buf));
183 sprintf(buf, "Please enter your callsign: ");
184 write(STDOUT_FILENO, buf, strlen(buf));
185 fflush(stdout);
186 if (fgets(user, NAME_SIZE-1, stdin) == NULL)
187 return 0;
188 for (i = 0; user[i] != '\0' && user[i] != '\n' && user[i] != '\r'; i++)
189 ;
190 user[i] = '\0';
191 if (strlen(user) < 1)
192 return 0;
193 userfamily = AF_INET;
194 break;
195 case AF_AX25:
196 case AF_NETROM:
197 peer_sax = (struct sockaddr_ax25*)&sa;
198 for (i=0 ; i < 6 ; i++)
199 {
200 user[i] = tolower(((peer_sax->sax25_call.ax25_call[i]) >>1)&0x7f);
201 if (user[i] == ' ')
202 break;
203 }
204 user[i] = '\0';
205 break;
206 case AF_ROSE:
207 peer_srose = (struct sockaddr_rose*)&sa;
208 for (i=0 ; i < 6 ; i++)
209 {
210 user[i] = tolower(((peer_srose->srose_call.ax25_call[i]) >>1)&0x7f);
211 if (user[i] == ' ')
212 break;
213 }
214 user[i] = '\0';
215 break;
216 default:
217 syslog(LOG_DAEMON | LOG_CRIT, "Unsupported address family.");
218 exit(1);
219 }
220
221 }
222 } /* argc */
223
224 /* Read the configuration file to find the System Operator. */
225 read_config_file(0);
226 /* ... and parse it into user@host */
227 sysop_user=strtok(sysop_addr,"@");
228 sysop_host=strtok(NULL,"@");
229
230 ppe = getprotobyname("udp");
231 if (!ppe) {
232 syslog(LOG_DAEMON | LOG_CRIT, "Cannot find udp protocol entry.");
233 exit(1);
234 }
235
236 /* Obtain our local hostname */
237 gethostname(hostname,256);
238
239 /* Get the remote address, or use ours if remote isn't specified */
240 if (sysop_host)
241 {
242 rhe = gethostbyname(sysop_host);
243 if (rhe == NULL)
244 {
245 syslog(LOG_DAEMON | LOG_CRIT, "main(): gethostbyname failed.");
246 exit(1);
247 }
248 }
249 else
250 {
251 rhe = gethostbyname(hostname);
252 if (rhe == NULL)
253 {
254 syslog(LOG_DAEMON | LOG_CRIT, "main(): gethostbyname failed.");
255 exit(1);
256 }
257 }
258 memcpy(&rem_addr, rhe->h_addr, rhe->h_length);
259
260 /* Get our local address */
261 phe = gethostbyname(hostname);
262 if (phe == NULL)
263 {
264 syslog(LOG_DAEMON | LOG_CRIT, "main(): gethostbyname failed.");
265 exit(1);
266 }
267 memcpy(&my_addr, phe->h_addr, phe->h_length);
268
269 /* Create local data socket */
270 memset(&msg_sa, 0, sizeof(msg_sa));
271
272 msg_sa.sa_family = AF_INET;
273 msg_sin = (struct sockaddr_in*)&msg_sa;
274 msg_sin->sin_port = htons(0);
275 memcpy(&(msg_sin->sin_addr), phe->h_addr, phe->h_length);
276
277 skt = socket(PF_INET, SOCK_STREAM, 0);
278 if (skt < 0)
279 {
280 syslog(LOG_DAEMON | LOG_CRIT, "main(): socket() failed.");
281 exit(1);
282 }
283
284 if (bind( skt, &msg_sa, sizeof(msg_sa)) != 0)
285 {
286 syslog(LOG_DAEMON | LOG_CRIT, "main(): bind() failed.");
287 exit(1);
288 }
289
290 length = sizeof(msg_sa);
291 if (getsockname(skt, &msg_sa, &length) < 0)
292 {
293 syslog(LOG_DAEMON | LOG_CRIT, "main(): getsockname() failed.");
294 exit(1);
295 }
296
297 /* Create local control socket */
298 memset(&ctl_sin, 0, sizeof(ctl_sin));
299
300 ctl_sin.sin_family = AF_INET;
301 memcpy(&ctl_sin.sin_addr, phe->h_addr, phe->h_length);
302 ctl_sin.sin_port = htons(0);
303
304 ctl_skt = socket(PF_INET, SOCK_DGRAM, 0);
305 if (ctl_skt < 0)
306 {
307 syslog(LOG_DAEMON | LOG_CRIT, "main(): socket() while attempting to create control socket.");
308 close(skt);
309 exit(1);
310 }
311
312 if (bind(ctl_skt, &ctl_sin, sizeof(ctl_sin)) != 0)
313 {
314 syslog(LOG_DAEMON | LOG_CRIT, "main(): Error when trying to bind() control socket.");
315 close(skt);
316 exit(1);
317 }
318
319 length = sizeof(ctl_sin);
320 if (getsockname(ctl_skt, (struct sockaddr*)&ctl_sin, &length) < 0)
321 {
322 syslog(LOG_DAEMON | LOG_CRIT,"main(): Error when trying to getsockname() for control socket.");
323 close(skt);
324 close(ctl_skt);
325 exit(1);
326 }
327
328 /* Start talking to the talk daemon */
329 memset(&msg, 0, sizeof(msg));
330 msg.vers = TALK_VERSION;
331 msg.id_num = htonl(0);
332 msg.addr.sa_family = ntohs(AF_INET);
333 memcpy(&(msg.ctl_addr), &msg_sa, sizeof(struct osockaddr));
334 /*msg.ctl_addr = *(struct sockaddr*)&ctl_sin;
335 msg.ctl_addr = *(struct sockaddr*)&ctl_sin;*/
336 msg.ctl_addr.sa_family = ntohs(AF_INET);
337 msg.pid = htonl(getpid());
338 strncpy(msg.l_name, user, NAME_SIZE-1);
339 strncpy(msg.r_name, sysop_user, NAME_SIZE-1);
340
341
342 /* Now look for an invite */
343 msg.type = LOOK_UP;
344 send_control(ctl_skt, rem_addr, msg, &resp);
345
346 /* The person not there? Send an announce and wake him up */
347 msg.type = ANNOUNCE;
348 memcpy(&(msg.addr), &msg_sa, sizeof(struct osockaddr));
349 msg.addr.sa_family = htons(AF_INET);
350 msg.id_num = -1;
351 i = send_control(ctl_skt, rem_addr, msg, &resp);
352 if ( i != SUCCESS)
353 {
354 if (i > BADCTLADDR)
355 printf("Cannot talk to sysop errno=%d.\n",i);
356 else
357 printf("Cannot talk to sysop, reason: %s.\n",Responses[i]);
358
359 close(skt);
360 close(ctl_skt);
361 return 0;
362 }
363 /* Update the ID number so that we both know what we're talking about. */
364 remote_id = resp.id_num;
365
366 /* Get the TCP port ready for a connection */
367 if (listen(skt, 5) != 0)
368 {
369 syslog(LOG_DAEMON | LOG_CRIT, "main(): Error when trying to listen() on socket.");
370 exit(1);
371 }
372
373 /* Now we have to make an invitation for the other user */
374 msg.type = LEAVE_INVITE;
375 if (send_control(ctl_skt, my_addr, msg, &resp) != SUCCESS)
376 {
377 printf("Problem with leaving an invitation\n");
378 syslog(LOG_DAEMON | LOG_CRIT, "main(): Cannot leave invititation.");
379 close(skt);
380 close(ctl_skt);
381 return 0;
382 }
383 local_id = resp.id_num;
384
385 sprintf(buf, "Paging sysop.\n");
386 write(STDOUT_FILENO, buf, strlen(buf));
387
388 /* Wait for the sysop to connect to us */
389 signal(SIGALRM, alarm_handle);
390 alarm(30);
391
392 new_skt = accept(skt, NULL, NULL);
393 if (new_skt < 0)
394 {
395 if (errno == EINTR)
396 {
397 /* Delete invitations from both daemons */
398 msg.type = DELETE;
399 msg.id_num = htonl(local_id);
400 send_control(ctl_skt, my_addr, msg, &resp);
401 msg.id_num = htonl(remote_id);
402 send_control(ctl_skt, rem_addr, msg, &resp);
403
404 close(skt);
405 close(ctl_skt);
406 return 0;
407 }
408 syslog(LOG_DAEMON | LOG_WARNING, "main(): accept() failed. (%m)");
409 }
410 alarm(0);
411 signal(SIGALRM, SIG_DFL);
412
413 close(skt);
414 skt = new_skt;
415
416 /* Delete invitations from both daemons */
417 msg.type = DELETE;
418 msg.id_num = htonl(local_id);
419 send_control(ctl_skt, my_addr, msg, &resp);
420 msg.id_num = htonl(remote_id);
421 send_control(ctl_skt, rem_addr, msg, &resp);
422
423 sprintf(buf, "Sysop has responded.\n");
424 write(STDOUT_FILENO, buf, strlen(buf));
425
426 /*
427 * A little thing that they don't mention anywhere is the fact that the
428 * first three characters on a connection are used to work out to erase,
429 * kill and word erase characters. Nice to know eh?
430 */
431 /* We send ours first, but then again we get data in cooked form, so
432 * we don't use this. */
433 buf[0] = '\0';
434 buf[1] = '\0';
435 buf[2] = '\0';
436 if (write(skt, buf, 3) != 3)
437 {
438 printf("Connection lost\n");
439 close(skt);
440 close(ctl_skt);
441 return 0;
442 }
443 /* Get their character stuff */
444 if (read(skt, buf, 3) != 3)
445 {
446 printf("Connection lost\n");
447 close(skt);
448 close(ctl_skt);
449 return 0;
450 }
451 erasec = buf[0];
452 killc = buf[1];
453 werasec = buf[2];
454
455 /* Tell the sysop who this person is */
456 if (sa.sa_family == AF_AX25)
457 {
458 sprintf(buf, "Incoming ttylink from %s.\n", user);
459 write(skt, buf, strlen(buf));
460 }
461 if (sa.sa_family == AF_INET)
462 {
463 sprintf(buf, "Incoming ttylink from %s@%s.\n", user, inet_ntoa(peer_sin->sin_addr));
464 write(skt, buf, strlen(buf));
465 }
466
467
468 do_talk(skt);
469
470 close(skt);
471 close(ctl_skt);
472 return 0;
473 }
474
475 /*
476 * Used to send control messages to our friendly local talk daemon
477 */
478 static int send_control(int skt, struct in_addr addr, CTL_MSG msg,
479 CTL_RESPONSE *resp)
480 {
481 fd_set fdvar;
482 struct timeval timeout;
483 struct sockaddr_in sin;
484 static int talk_port;
485 struct servent *pse;
486
487 /* Look up talk port once only */
488 if (talk_port == 0)
489 {
490 if ((pse = getservbyname("ntalk", "udp")) == NULL)
491 {
492 perror("getservbyname, assuming 518");
493 talk_port = 518;
494 } else {
495 talk_port = pse->s_port;
496 }
497 }
498
499 /* Create the socket address */
500 memset(&sin, 0, sizeof(sin));
501 sin.sin_addr = addr;
502 sin.sin_family = AF_INET;
503 sin.sin_port = talk_port;
504
505 if (sendto(skt, &msg, sizeof(msg), 0, (struct sockaddr *)&sin, sizeof(sin)) != sizeof(msg))
506 {
507 syslog(LOG_DAEMON | LOG_ERR, "send_control(): sendto failed (%m).");
508 return -1;
509 }
510
511 /* Wait for reply */
512 FD_ZERO(&fdvar);
513 FD_SET(skt, &fdvar);
514 timeout.tv_sec = RING_WAIT;
515 timeout.tv_usec = 0;
516
517 if (select(32, &fdvar, NULL, NULL, &timeout) < 0)
518 syslog(LOG_DAEMON | LOG_ERR, "send_control(): select failed. (%m)");
519
520 /*
521 * The server is ignoring us, see ya later
522 */
523 if (!(FD_ISSET(skt, &fdvar)))
524 {
525 printf("Talk server not responding after %d seconds, aborting.\n", RING_WAIT);
526 return -1;
527 }
528
529 /* Get the message */
530 if (recv(skt, resp, sizeof(resp),0) ==0)
531 syslog(LOG_DAEMON | LOG_ERR, "send_control(): recv failed. (%m)");
532
533 return resp->answer;
534 }
535
536 /* Used to process the data from the sysop */
537 static int send_sysop_data(char *buf, int len)
538 {
539 static char outbuf[82];
540 static char *bptr = outbuf;
541 int i;
542
543 for (i = 0; i < len; i++)
544 {
545 /* Check for erase character */
546 if (buf[i] == erasec)
547 {
548 if (bptr > outbuf)
549 bptr--;
550 continue;
551 }
552
553 /* Check for kill character */
554 if (buf[i] == killc)
555 {
556 bptr = outbuf;
557 continue;
558 }
559
560 /* Check for word-erase character */
561 if (buf[i] == werasec)
562 {
563 while ( (bptr > outbuf) && (*bptr != ' ') )
564 bptr--;
565 continue;
566 }
567
568 /* Check for newline character */
569 if (buf[i] == '\n')
570 {
571 if ( (userfamily == AF_AX25) || (userfamily == AF_NETROM) || (userfamily == AF_ROSE) )
572 {
573 *bptr = '\r';
574 bptr++;
575 } else {
576 *bptr = '\r';
577 bptr++;
578 *bptr = '\n';
579 bptr++;
580 }
581 } else {
582 *bptr = buf[i];
583 bptr++;
584 }
585
586 /* Check for carriage return, which means send it */
587 /* We also send if we have more than 80 characters */
588 if (buf[i] == '\n' || (bptr - outbuf) > 80 )
589 {
590 if (write(STDOUT_FILENO, outbuf, (bptr - outbuf) ) != (bptr - outbuf) )
591 {
592 return -1;
593 }
594 bptr = outbuf;
595 }
596 } /* for */
597 return 0;
598 }
599
600 /* Used to process the data from the user - len must not exceed 256 */
601 static int send_user_data(int skt, char *buf, int len)
602 {
603 char outbuf[256];
604 char *bptr = outbuf;
605 int i;
606
607 for (i = 0; i < len; i++)
608 {
609 if (buf[i] == '\r')
610 {
611 if (userfamily == AF_INET)
612 {
613 *bptr = '\n';
614 bptr++;
615 /*
616 * check if this is a <CR><LF> or a <CR><NUL>
617 * and skip the <LF> or <NUL>.
618 */
619 if (buf[i + 1] == '\n' || buf[i + 1] == '\0')
620 {
621 i++;
622 }
623 } else
624 {
625 *bptr = '\n';
626 bptr++;
627 }
628 continue;
629 }
630 *bptr = buf[i];
631 bptr++;
632 } /* for */
633
634 if (write(skt, outbuf, bptr - outbuf) != bptr - outbuf)
635 {
636 return -1;
637 }
638 return 0;
639 }
640
641
642 /* The main talking loop */
643 static void do_talk(int skt)
644 {
645 fd_set fdvar;
646 char inbuf[256], outbuf[256];
647 struct timeval timeout;
648 int i;
649
650
651 while (1)
652 {
653 FD_ZERO(&fdvar);
654 FD_SET(skt, &fdvar);
655 FD_SET(STDIN_FILENO, &fdvar);
656
657 timeout.tv_sec = 30;
658 timeout.tv_usec = 0;
659 if (select(32, &fdvar, NULL, NULL, &timeout) == 0)
660 {
661 if (ioctl(STDIN_FILENO, FIONREAD, (struct sgttyb *)&i) < 0)
662 return;
663 if (ioctl(skt, FIONREAD, (struct sgttyb*)&i) < 0)
664 return;
665 }
666
667 if (FD_ISSET(skt, &fdvar))
668 {
669 i = read(skt, inbuf, 256);
670 if (i <= 0)
671 {
672 strcpy(inbuf,"Sysop closed connection.\n");
673 write(STDOUT_FILENO, inbuf, strlen(inbuf));
674 return;
675 }
676 if (send_sysop_data(inbuf, i) < 0)
677 {
678 strcpy(outbuf, "User closed connection.\n");
679 write(skt, outbuf, strlen(outbuf));
680 return;
681 }
682 }
683 if (FD_ISSET(STDIN_FILENO, &fdvar))
684 {
685 i = read(STDIN_FILENO, outbuf, 256);
686 if (i <= 0)
687 {
688 strcpy(outbuf, "User closed connection.\n");
689 write(skt, outbuf, strlen(outbuf));
690 return;
691 }
692 if (send_user_data(skt, outbuf, i) < 0)
693 {
694 strcpy(inbuf,"Sysop closed connection.\n");
695 write(STDOUT_FILENO, inbuf, strlen(inbuf));
696 return;
697 }
698 }
699 }
700 }
701
702 static void alarm_handle(int i)
703 {
704 char buf[256];
705
706 strcpy(buf, "Sysop not responding.\n");
707 write(STDOUT_FILENO, buf, strlen(buf));
708 }
709
710 static void read_config_file(int dummy)
711 {
712 char buf[128];
713 char param[20], value[108];
714 FILE *fp;
715
716 fp = fopen(config_file, "r");
717 if (fp == NULL) {
718 syslog(LOG_DAEMON | LOG_ERR, "Cannot find configuration file: %s (%m)\n",config_file);
719 return;
720 }
721 /* Reset any variables here */
722
723 while ( fgets(buf, 128, fp) != NULL) {
724 if ( buf[0] == '#')
725 continue;
726 if (sscanf(buf, "%[^=]%*c%[^\n]", param, value) == 2) {
727 /* A big wierdo switch */
728 if (strcasecmp(param, "sysop") == 0) {
729 strncpy(sysop_addr, value, ADDR_SIZE);
730 }
731 }
732 } /* while */
733 fclose(fp);
734 signal(SIGHUP, read_config_file);
735 }