1 #ident "$Id: mgetty.c,v 4.40 2005/12/31 15:54:01 gert Exp $ Copyright (c) Gert Doering"
2
3 /* mgetty.c
4 *
5 * mgetty main module - initialize modem, lock, get log name, call login
6 *
7 * some parts of the code (lock handling, writing of the utmp entry)
8 * are based on the "getty kit 2.0" by Paul Sutcliffe, Jr.,
9 * paul@devon.lns.pa.us, and are used with permission here.
10 *
11 * $Log: mgetty.c,v $
12 * Revision 4.40 2005/12/31 15:54:01 gert
13 * correctly handle non-adaptive fax answer in class 1/1.0 mode (modem will
14 * send "CONNECT", meaning "I'm in fax mode, please send DIS frame"...)
15 *
16 * Revision 4.39 2005/11/24 17:00:30 gert
17 * add CVS Log keyword
18 *
19 */
20
21 #include <stdio.h>
22 #include "syslibs.h"
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/times.h>
27
28 #include <sys/stat.h>
29 #include <signal.h>
30
31 #include "version.h"
32 #include "mgetty.h"
33 #include "policy.h"
34 #include "tio.h"
35 #include "fax_lib.h"
36 #include "mg_utmp.h"
37
38 #include "config.h"
39 #include "conf_mg.h"
40
41 #ifdef VOICE
42 #include "voice/include/voice.h"
43 #endif
44
45 /* how much time may pass between two RINGs until mgetty goes into */
46 /* "waiting" state again */
47 int ring_chat_timeout = 10;
48
49 /* what kind of "surprising" things are recognized */
50 chat_action_t ring_chat_actions[] = { { "CONNECT", A_CONN },
51 { "NO CARRIER", A_FAIL },
52 { "BUSY", A_FAIL },
53 { "ERROR", A_FAIL },
54 { "+FCON", A_FAX },
55 { "+FCO\r", A_FAX },
56 { "FAX", A_FAX },
57 { "+FHS:", A_FAIL },
58 { "+FHNG:", A_FAIL },
59 #ifdef VOICE
60 { "VCON", A_VCON },
61 #endif
62 { NULL, A_FAIL } };
63
64 /* the same actions are recognized while answering as are */
65 /* when waiting for RING, except for "CONNECT" */
66
67 chat_action_t * answer_chat_actions = &ring_chat_actions[1];
68
69
70 /* prototypes for system functions (that are missing in some
71 * system header files)
72 */
73 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
74 time_t time _PROTO(( time_t * tloc ));
75 #endif
76
77 /* logname.c */
78 int getlogname _PROTO(( char * prompt, TIO * termio,
79 char * buf, int maxsize, int max_login_time,
80 boolean do_fido, boolean env_ttyprompt ));
81
82 /* conf_mg.c */
83 void exit_usage _PROTO((int num));
84
85 char * Device; /* device to use */
86 char * DevID; /* device name withouth '/'s */
87
88 extern time_t call_start; /* time when we sent ATA */
89 /* defined in faxrec.c */
90
91 void gettermio _PROTO((char * tag, boolean first, TIO * tio));
92
93 /* "simulated RING" handler */
94 boolean virtual_ring = FALSE;
sig_pick_phone(SIG_HDLR_ARGS)95 static RETSIGTYPE sig_pick_phone(SIG_HDLR_ARGS)
96 {
97 signal( SIGUSR1, sig_pick_phone );
98 virtual_ring = TRUE;
99 }
100 /* handle other signals: log them, and say goodbye... */
101 static RETSIGTYPE sig_goodbye _P1 ( (signo), int signo )
102 {
103 lprintf( L_AUDIT, "failed dev=%s, pid=%d, got signal %d, exiting",
104 Device, getpid(), signo );
105 rmlocks();
106 exit(10);
107 }
108
109 /* create a file with the process ID of the mgetty currently
110 * active on a given device in it.
111 */
112 static char pid_file_name[ MAXPATH ];
_P0(void)113 static void make_pid_file _P0( void )
114 {
115 FILE * fp;
116
117 sprintf( pid_file_name, "%s/mgetty.pid.%s", VARRUNDIR, DevID );
118
119 fp = fopen( pid_file_name, "w" );
120 if ( fp == NULL )
121 lprintf( L_ERROR, "can't create pid file %s", pid_file_name );
122 else
123 {
124 fprintf( fp, "%d\n", (int) getpid() ); fclose( fp );
125 }
126 if ( chmod( pid_file_name, 0644 ) != 0 )
127 lprintf( L_ERROR, "can't chmod() pid file" );
128 }
129
130
131 enum mgetty_States
132 { St_unknown,
133 St_go_to_jail, /* reset after unwanted call */
134 St_waiting, /* wait for activity on tty */
135 St_check_modem, /* check if modem is alive */
136 St_wait_for_RINGs, /* wait for <n> RINGs before ATA */
137 St_answer_phone, /* ATA, wait for CONNECT/+FCO(N) */
138 St_nologin, /* no login allowed, wait for
139 RINGing to stop */
140 St_dialout, /* parallel dialout, wait for
141 lockfile to disappear */
142 St_get_login, /* prompt "login:", call login() */
143 St_callback_login, /* ditto, but after callback */
144 St_incoming_fax /* +FCON detected */
145 } mgetty_state = St_unknown;
146
147 /* called on SIGUSR2. Exit, if no user online, ignore otherwise */
sig_new_config(SIG_HDLR_ARGS)148 static RETSIGTYPE sig_new_config(SIG_HDLR_ARGS)
149 {
150 signal( SIGUSR2, sig_new_config );
151 if ( mgetty_state != St_answer_phone &&
152 mgetty_state != St_get_login &&
153 mgetty_state != St_callback_login &&
154 mgetty_state != St_incoming_fax )
155 {
156 lprintf( L_AUDIT, "exit dev=%s, pid=%d, got signal USR2, exiting",
157 Device, getpid() );
158 rmlocks();
159 exit(0);
160 }
161 lprintf( L_MESG, "Got SIGUSR2, modem is off-hook --> ignored" );
162 }
163
164 enum mgetty_States st_sig_callback _P2( (pid, devname),
165 int pid, char * devname )
166 {
167 TIO tio;
168
169 lprintf( L_MESG, "Got callback signal from pid=%d!", pid );
170
171 /* reopen device */
172 if ( mg_open_device( devname, FALSE ) == ERROR )
173 {
174 lprintf( L_FATAL, "stsc: can't reopen device" );
175 exit(0);
176 }
177
178 /* setup device (but do *NOT*!! set speed) */
179 if ( tio_get( STDIN, &tio ) == ERROR )
180 {
181 lprintf( L_FATAL, "stsc: can't get TIO" ); exit(0);
182 }
183 tio_mode_sane( &tio, c_bool( ignore_carrier ) );
184 tio_default_cc( &tio );
185 tio_mode_raw( &tio );
186 tio_set_flow_control( STDIN, &tio, DATA_FLOW );
187 if ( tio_set( STDIN, &tio ) == ERROR )
188 {
189 lprintf( L_FATAL, "stsc: can't set TIO" ); exit(0);
190 }
191
192 /* make line controlling tty */
193 mg_get_ctty( STDIN, devname );
194
195 /* steal lock file from callback process */
196 lprintf( L_MESG, "stealing lock file from pid=%d", pid );
197 if ( steal_lock( Device, pid ) == ERROR ) return St_dialout;
198
199 /* signal user */
200 printf( "...ok\r\n" );
201
202 /* signal callback process (but give it some time to enter pause()! */
203 delay(500);
204 if ( kill( pid, SIGUSR1 ) < 0 )
205 {
206 lprintf( L_ERROR, "can't signal callback process" );
207 }
208
209 /* now give user a login prompt! */
210 return St_callback_login;
211 }
212
213 /* line locked, parallel dialout in process.
214 *
215 * Two things can happen now:
216 * - lock file disappears --> dialout terminated, exit(), restart
217 * - get signal SIGUSR1 --> dialout was callback, mgetty takes over
218 */
219 enum mgetty_States st_dialout _P1( (devname), char * devname )
220 {
221 int pid;
222
223 /* the line is locked, a parallel dialout is in process */
224
225 virtual_ring = FALSE; /* used to signal callback */
226
227 /* write a note to utmp/wtmp about dialout, including process args
228 * (don't do this on two-user-license systems!)
229 */
230 #ifndef USER_LIMIT
231 pid = checklock( Device ); /* !! FIXME, ugly */
232 make_utmp_wtmp( Device, UT_USER, "dialout", get_ps_args(pid) );
233 #endif
234
235 /* close all file descriptors -> other processes can read port */
236 close(0);
237 close(1);
238 close(2);
239
240 /* this is kind of tricky: sometimes uucico dial-outs do still
241 collide with mgetty. So, when my uucico times out, I do
242 *immediately* restart it. The double check makes sure that
243 mgetty gives me at least 5 seconds to restart uucico */
244
245 do {
246 /* wait for lock to disappear */
247 while ( ( pid = checklock(Device) ) != NO_LOCK )
248 {
249 sleep(10);
250
251 /* virtual ring? this would mean an active callback! */
252 if ( virtual_ring )
253 {
254 return st_sig_callback( pid, devname );
255 }
256 }
257
258 /* wait a moment, then check for reappearing locks */
259 sleep(5);
260 }
261 while ( checklock(Device) != NO_LOCK );
262
263 /* OK, leave & get restarted by init */
264 exit(0);
265 } /* end st_dialout() */
266
267 void get_ugid _PROTO(( conf_data * user, conf_data * group,
268 uid_t * uid, gid_t * gid ));
269
270 int main _P2((argc, argv), int argc, char ** argv)
271 {
272 char devname[MAXLINE+1]; /* full device name (with /dev/) */
273 char buf[MAXLINE+1];
274 TIO tio;
275 FILE *fp;
276 int i;
277
278 action_t what_action;
279 int rings_wanted;
280 int rings = 0;
281 int dist_ring = 0; /* type of RING detected */
282
283 #if defined(_3B1_) || defined(MEIBE) || defined(sysV68)
284 extern struct passwd *getpwuid(), *getpwnam();
285 #endif
286
287 uid_t uid; /* typical uid for UUCP */
288 gid_t gid;
289
290 #ifdef VOICE
291 boolean use_voice_mode = TRUE;
292 #endif
293
294 /* startup: initialize all signal handlers *NOW*
295 */
296 (void) signal(SIGHUP, SIG_IGN);
297
298 /* set to remove lockfile(s) and print "got signal..." message
299 */
300 (void) signal(SIGINT, sig_goodbye);
301 (void) signal(SIGQUIT, sig_goodbye);
302 (void) signal(SIGTERM, sig_goodbye);
303
304 /* sometimes it may be desired to have mgetty pick up the phone even
305 if it didn't RING often enough (because you accidently picked it up
306 manually...) or if it didn't RING at all (because you have a fax
307 machine directly attached to the modem...), so send mgetty a signal
308 SIGUSR1 and it will behave as if a RING was seen
309 In addition, this is used by the "callback" module.
310 */
311 signal( SIGUSR1, sig_pick_phone );
312
313 /* for reloading the configuration file, we need a way to tell mgetty
314 "restart, but only if no user is online". Use SIGUSR2 for that
315 */
316 signal( SIGUSR2, sig_new_config );
317
318 #ifdef HAVE_SIGINTERRUPT
319 /* some systems, notable BSD 4.3, have to be told that system
320 * calls are not to be automatically restarted after those signals.
321 */
322 siginterrupt( SIGINT, TRUE );
323 siginterrupt( SIGALRM, TRUE );
324 siginterrupt( SIGHUP, TRUE );
325 siginterrupt( SIGUSR1, TRUE );
326 siginterrupt( SIGUSR2, TRUE );
327 #endif
328
329 Device = "unknown";
330
331 /* process the command line
332 */
333 mgetty_parse_args( argc, argv );
334
335 /* normal System V getty argument handling
336 */
337
338 if (optind < argc)
339 Device = argv[optind++];
340 else {
341 lprintf(L_FATAL,"no line given");
342 exit_usage(2);
343 }
344
345 /* remove leading /dev/ prefix */
346 if ( strncmp( Device, "/dev/", 5 ) == 0 ) Device += 5;
347
348 /* need full name of the device */
349 sprintf( devname, "/dev/%s", Device);
350
351 /* Device ID = Device name without "/dev/", all '/' converted to '-' */
352 DevID = mydup( Device );
353 for ( i=0; DevID[i] != 0; i++ )
354 if ( DevID[i] == '/' ) DevID[i] = '-';
355
356 /* name of the logfile is device-dependant */
357 sprintf( buf, LOG_PATH, DevID );
358 log_init_paths( argv[0], buf, &Device[strlen(Device)-3] );
359
360 #ifdef VOICE
361 lprintf( L_MESG, "vgetty: %s", vgetty_version);
362 #endif
363 lprintf( L_MESG, "mgetty: %s", mgetty_version);
364 lprintf( L_NOISE, "%s compiled at %s, %s", __FILE__, __DATE__, __TIME__ );
365 i=getppid();
366 lprintf( L_NOISE, "user id: %d, pid: %d, parent pid: %d", getuid(), getpid(), i);
367 if ( i != 1 )
368 {
369 char *n = get_ps_args(i);
370 lprintf( L_WARN, "WARNING: parent process not init(pid=1), but pid=%d (%s)", i, n != NULL? n: "unknown" );
371 }
372
373 /* read configuration file */
374 mgetty_get_config( Device );
375
376 #ifdef VOICE
377 check_system();
378 voice_config("vgetty", DevID);
379 voice_register_event_handler(vgetty_handle_event);
380 #endif
381
382 #ifdef USE_GETTYDEFS
383 if (optind < argc)
384 conf_set_string( &c.gettydefs_tag, argv[optind++] );
385
386 lprintf( L_MESG, "gettydefs tag used: %s", c_string(gettydefs_tag) );
387 #endif
388
389 make_pid_file();
390
391 lprintf(L_MESG, "check for lockfiles");
392
393 /* deal with the lockfiles; we don't want to charge
394 * ahead if uucp, kermit or whatever else is already
395 * using the line.
396 * (Well... if we reach this point, most propably init has
397 * hung up anyway :-( )
398 */
399
400 /* check for existing lock file(s)
401 */
402 if (checklock(Device) != NO_LOCK)
403 {
404 st_dialout(NULL);
405 }
406
407 /* try to lock the line
408 */
409 lprintf(L_MESG, "locking the line");
410
411 if ( makelock(Device) == FAIL )
412 {
413 st_dialout(NULL);
414 }
415
416 /* the line is mine now ... */
417
418 /* set proper port ownership and permissions
419 */
420 get_ugid( &c.port_owner, &c.port_group, &uid, &gid );
421 chown( devname, uid, gid );
422 if ( c_isset(port_mode) )
423 chmod( devname, c_int(port_mode) );
424
425 /* if necessary, kill any processes that still has the serial device
426 * open (Marc Boucher, Marc Schaefer).
427 */
428 #if defined( EXEC_FUSER )
429 sprintf( buf, EXEC_FUSER, devname );
430 if ( ( i = system( buf ) ) != 0 )
431 lprintf( L_WARN, "%s: return code %d", buf, i );
432 #endif
433
434 /* setup terminal */
435
436 /* Currently, the tio set here is ignored.
437 The invocation is only for the sideeffects of:
438 - loading the gettydefs file if enabled.
439 - setting port speed appropriately, if not set yet.
440 */
441 gettermio(c_string(gettydefs_tag), TRUE, (TIO *) NULL);
442
443 /* open + initialize device (mg_m_init.c) */
444 if ( mg_get_device( devname, c_bool(blocking),
445 c_bool(toggle_dtr), c_int(toggle_dtr_waittime),
446 c_int(speed) ) == ERROR )
447 {
448 lprintf( L_FATAL, "cannot get terminal line dev=%s, exiting", Device);
449 exit(30);
450 }
451
452 /* drain input - make sure there are no leftover "NO CARRIER"s
453 * or "ERROR"s lying around from some previous dial-out
454 */
455 clean_line( STDIN, 1);
456
457 /* do modem initialization, normal stuff first, then fax
458 */
459 if ( c_bool(direct_line) )
460 Connect = "DIRECT"; /* for "\I" in issue/prompt */
461 else
462 {
463 /* initialize data part */
464 if ( mg_init_data( STDIN, c_chat(init_chat), c_bool(need_dsr),
465 c_chat(force_init_chat) ) == FAIL )
466 {
467 lprintf( L_AUDIT, "failed in mg_init_data, dev=%s, pid=%d",
468 Device, getpid() );
469 tio_flush_queue( STDIN, TIO_Q_BOTH ); /* unblock flow ctrl */
470 rmlocks();
471 exit(1);
472 }
473
474 /* if desired, get some "last call statistics" info */
475 if ( c_isset(statistics_chat) )
476 {
477 get_statistics( STDIN, c_chat(statistics_chat),
478 c_isset(statistics_file)? c_string(statistics_file): NULL );
479 }
480
481 /* initialize ``normal'' fax functions */
482 if ( ( ! c_bool(data_only) ) &&
483 strcmp( c_string(modem_type), "data" ) != 0 &&
484 mg_init_fax( STDIN, c_string(modem_type),
485 c_string(station_id), c_bool(fax_only),
486 c_int(fax_max_speed) ) == SUCCESS )
487 {
488 /* initialize fax polling server (only if faxmodem) */
489 if ( c_isset(fax_server_file) )
490 {
491 faxpoll_server_init( STDIN, c_string(fax_server_file) );
492 }
493 }
494
495 #ifdef VOICE
496 voice_fd = STDIN;
497 voice_init();
498
499 if ( use_voice_mode ) {
500 /* With external modems, the auto-answer LED can be used
501 * to show a status flag. vgetty uses this to indicate
502 * that new messages have arrived.
503 */
504 vgetty_message_light();
505 }
506 #endif /* VOICE */
507
508 /* some modems forget some of their settings during fax/voice
509 * initialization -- use this as 'last chance' to set those things
510 * [don't care for errors here]
511 */
512 if ( c_isset( post_init_chat ) )
513 {
514 lprintf( L_NOISE, "running post_init_chat" );
515 do_chat( STDIN, c_chat(post_init_chat), NULL, NULL, 10, TRUE );
516 }
517 }
518
519 /* wait .3s for line to clear (some modems send a \n after "OK",
520 this may confuse the "call-chat"-routines) */
521
522 clean_line( STDIN, 3);
523
524 /* remove locks, so any other process can dial-out. When waiting
525 for "RING" we check for foreign lockfiles, if there are any, we
526 give up the line - otherwise we lock it again */
527
528 rmlocks();
529
530 #if ( defined(linux) && defined(NO_SYSVINIT) ) || defined(sysV68)
531 /* on linux, "simple init" does not make a wtmp entry when you
532 * log so we have to do it here (otherwise, "who" won't work) */
533 make_utmp_wtmp( Device, UT_INIT, "uugetty", NULL );
534 #endif
535
536 /* sleep... waiting for activity */
537 mgetty_state = St_waiting;
538
539 while ( mgetty_state != St_get_login &&
540 mgetty_state != St_callback_login )
541 {
542 switch (mgetty_state) /* state machine */
543 {
544 case St_go_to_jail:
545 /* after a rejected call (caller ID, not enough RINGs,
546 * /etc/nologin file), do some cleanups, and go back to
547 * field one: St_waiting
548 */
549 CallTime = CallName = CalledNr = ""; /* dirty */
550 CallerId = "none";
551 clean_line( STDIN, 3); /* let line settle */
552 rmlocks();
553 mgetty_state = St_waiting;
554 break;
555
556 case St_waiting:
557 /* wait for incoming characters (using select() or poll() to
558 * prevent eating away from processes dialing out)
559 */
560 lprintf( L_MESG, "waiting..." );
561
562 /* ignore accidential sighup, caused by dialout or such
563 */
564 signal( SIGHUP, SIG_IGN );
565
566 /* here's mgetty's magic. Wait with select() or something
567 * similar non-destructive for activity on the line.
568 * If called with "-b" or as "getty", the blocking has
569 * already happened in the open() call.
570 */
571 if ( ! c_bool(blocking) )
572 {
573 int wait_time = c_int(modem_check_time)*1000;
574
575 if ( ! wait_for_input( STDIN, wait_time ) &&
576 ! c_bool(direct_line) && ! virtual_ring )
577 {
578 /* no activity - is the modem alive or dead? */
579 log_close();
580 mgetty_state = St_check_modem;
581 break;
582 }
583 }
584
585 /* close (and reopen) log file, to make sure it hasn't been
586 * moved away while sleeping and waiting for 'activity'
587 */
588 log_close();
589
590 /* check for LOCK files, if there are none, grab line and lock it
591 */
592
593 lprintf( L_NOISE, "checking lockfiles, locking the line" );
594
595 if ( makelock(Device) == FAIL)
596 {
597 lprintf( L_NOISE, "lock file exists (dialout)!" );
598 mgetty_state = St_dialout;
599 break;
600 }
601
602 /* now: honour SIGHUP
603 */
604 signal(SIGHUP, sig_goodbye );
605
606 rings = 0;
607
608 /* check, whether /etc/nologin.<device> exists. If yes, do not
609 answer the phone. Instead, wait for ringing to stop. */
610 #ifdef NOLOGIN_FILE
611 sprintf( buf, NOLOGIN_FILE, DevID );
612
613 if ( access( buf, F_OK ) == 0 )
614 {
615 lprintf( L_MESG, "%s exists - do not accept call!", buf );
616 mgetty_state = St_nologin;
617 break;
618 }
619 #endif
620 mgetty_state = St_wait_for_RINGs;
621 break;
622
623
624 case St_check_modem:
625 /* some modems have the nasty habit of just dying after some
626 time... so, mgetty regularily checks with AT...OK whether
627 the modem is still alive */
628 lprintf( L_MESG, "checking if modem is still alive" );
629
630 if ( makelock( Device ) == FAIL )
631 {
632 mgetty_state = St_dialout; break;
633 }
634
635 /* try twice */
636 if ( mdm_command( "AT", STDIN ) == SUCCESS ||
637 mdm_command( "AT", STDIN ) == SUCCESS )
638 {
639 mgetty_state = St_go_to_jail; break;
640 }
641
642 lprintf( L_FATAL, "modem on dev=%s doesn't react!", Device );
643
644 /* give up */
645 exit(30);
646
647 break;
648
649 case St_nologin:
650 #ifdef NOLOGIN_FILE
651 /* if a "/etc/nologin.<device>" file exists, wait for RINGing
652 to stop, but count RINGs (in case the user removes the
653 nologin file while the phone is RINGing), and if the modem
654 auto-answers, handle it properly */
655
656 sprintf( buf, NOLOGIN_FILE, DevID );
657
658 /* while phone is ringing... */
659
660 while( wait_for_ring( STDIN, NULL, 10, ring_chat_actions,
661 &what_action, &dist_ring ) == SUCCESS )
662 {
663 rings++;
664 if ( access( buf, F_OK ) != 0 || /* removed? */
665 virtual_ring == TRUE ) /* SIGUSR1? */
666 {
667 mgetty_state = St_wait_for_RINGs; /* -> accept */
668 break;
669 }
670 }
671
672 /* did nologin file disappear? */
673 if ( mgetty_state != St_nologin ) break;
674
675 /* phone stopped ringing (do_chat() != SUCCESS) */
676 switch( what_action )
677 {
678 case A_TIMOUT: /* stopped ringing */
679 lprintf( L_AUDIT, "rejected, rings=%d", rings );
680 mgetty_state = St_go_to_jail;
681 break;
682 case A_CONN: /* CONNECT */
683 clean_line( STDIN, 5 );
684 printf( "\r\n\r\nSorry, no login allowed\r\n" );
685 printf( "\r\nGoodbye...\r\n\r\n" );
686 sleep(5); exit(20); break;
687 case A_FAX: /* +FCON */
688 mgetty_state = St_incoming_fax; break;
689 default:
690 lprintf( L_MESG, "unexpected action: %d", what_action );
691 exit(20);
692 }
693 #endif
694 break;
695
696
697 case St_dialout:
698 /* wait for lock file to disappear *OR* for callback in progress */
699 mgetty_state = st_dialout(devname);
700 break;
701
702 case St_wait_for_RINGs:
703 /* Wait until the proper number of RING strings have been
704 seen. In case the modem auto-answers (yuck!) or someone
705 hits DATA/VOICE, we'll accept CONNECT, +FCON, ... also. */
706
707 if ( c_bool(direct_line) ) /* no RING needed */
708 {
709 mg_get_ctty( STDIN, devname ); /* get controll.tty */
710 mgetty_state = St_get_login;
711 break;
712 }
713
714 dist_ring=0; /* yet unspecified RING type */
715
716 if ( c_bool(ringback) ) /* don't pick up on first call */
717 {
718 int n = 0;
719
720 while( wait_for_ring( STDIN, NULL, 17, ring_chat_actions,
721 &what_action, &dist_ring ) == SUCCESS &&
722 ! virtual_ring )
723 { n++; }
724
725 if ( what_action != A_TIMOUT ) goto Ring_got_action;
726
727 lprintf( L_MESG, "ringback: phone stopped after %d RINGs, waiting for re-ring", n );
728 }
729
730 /* how many rings to wait for (normally) */
731 rings_wanted = c_int(rings_wanted);
732 #ifdef VOICE
733 if ( use_voice_mode ) {
734 /* modify, if toll saver, or in vgetty answer-file */
735 vgetty_rings(&rings_wanted);
736 }
737 #endif /* VOICE */
738
739 while ( rings < rings_wanted )
740 {
741 if ( wait_for_ring( STDIN, c_chat(msn_list),
742 ( c_bool(ringback) && rings == 0 )
743 ? c_int(ringback_time) : ring_chat_timeout,
744 ring_chat_actions, &what_action,
745 &dist_ring ) == FAIL)
746 {
747 break; /* ringing stopped, or "action" */
748 }
749 rings++;
750 }
751
752 /* enough rings? */
753 if ( rings >= rings_wanted )
754 {
755 mgetty_state = St_answer_phone; break;
756 }
757
758 Ring_got_action:
759 /* not enough rings, timeout or action? */
760
761 switch( what_action )
762 {
763 case A_TIMOUT: /* stopped ringing */
764 if ( rings == 0 && /* no ring *AT ALL* */
765 ! c_bool(ringback))/* and not "missed" ringback */
766 {
767 lprintf( L_WARN, "huh? Junk on the line?" );
768 lprintf( L_WARN, " >>> could be a dial-out program without proper locking - check this!" );
769 rmlocks(); /* line is free again */
770 exit(0); /* let init restart mgetty */
771 }
772 if ( c_bool(ringback) )
773 lprintf( L_AUDIT, "missed ringback!" );
774 else
775 lprintf( L_AUDIT, "phone stopped ringing (rings=%d, dev=%s, pid=%d, caller='%s')", rings, Device, getpid(), CallerId );
776
777 mgetty_state = St_go_to_jail;
778 break;
779 case A_CONN: /* CONNECT */
780 mg_get_ctty( STDIN, devname );
781 mgetty_state = St_get_login; break;
782 case A_FAX: /* +FCON */
783 mgetty_state = St_incoming_fax; break;
784 #ifdef VOICE
785 case A_VCON:
786 vgetty_button(rings);
787 use_voice_mode = FALSE;
788 mgetty_state = St_answer_phone;
789 break;
790 #endif
791 case A_FAIL:
792 lprintf( L_AUDIT, "failed A_FAIL dev=%s, pid=%d, caller='%s'",
793 Device, getpid(), CallerId );
794 exit(20);
795 default:
796 lprintf( L_MESG, "unexpected action: %d", what_action );
797 exit(20);
798 }
799 break;
800
801
802 case St_answer_phone:
803 /* Answer an incoming call, after the desired number of
804 RINGs. If we have caller ID information, and checking
805 it is desired, do it now, and possibly reject call if
806 not allowed in. If we have to do some chat with the modem
807 to get the Caller ID, do it now. */
808
809 if ( c_isset(getcnd_chat) )
810 {
811 do_chat( STDIN, c_chat(getcnd_chat), NULL, NULL, 10, TRUE );
812 }
813
814 /* Check Caller ID. Static table first, then cnd-program. */
815
816 if ( !cndlookup() ||
817 ( c_isset(cnd_program) &&
818 cnd_call( c_string(cnd_program), Device, dist_ring ) == 1))
819 {
820 lprintf( L_AUDIT, "denied caller dev=%s, pid=%d, caller='%s'",
821 Device, getpid(), CallerId);
822 clean_line( STDIN, 80 ); /* wait for ringing to stop */
823
824 mgetty_state = St_go_to_jail;
825 break;
826 }
827
828 /* from here, there's no way back. Either the call will succeed
829 and mgetty will exec() something else, or it will fail and
830 mgetty will exit(). */
831
832 /* get line as ctty: hangup will come through
833 */
834 mg_get_ctty( STDIN, devname );
835
836 /* remember time of phone pickup */
837 call_start = time( NULL );
838
839 #ifdef VOICE
840 if ( use_voice_mode ) {
841 int rc;
842 /* Answer in voice mode. The function will return only if it
843 detects a data call, otherwise it will call exit(). */
844 rc = vgetty_answer(rings, rings_wanted, dist_ring);
845
846 /* The modem will be in voice mode when voice_answer is
847 * called. If the function returns, the modem is ready
848 * to be connected in DATA mode with ATA.
849 *
850 * Exception: a CONNECT has been seen (-> go to "login:")
851 * or a fax connection is established (go to fax receive)
852 */
853
854 if ( rc == VMA_CONNECT )
855 { mgetty_state = St_get_login; break; }
856 if ( rc == VMA_FCO || rc == VMA_FCON || rc == VMA_FAX )
857 { mgetty_state = St_incoming_fax; break; }
858 }
859 #endif /* VOICE */
860
861 if ( do_chat( STDIN, c_chat(answer_chat), answer_chat_actions,
862 &what_action, c_int(answer_chat_timeout), TRUE)
863 == FAIL )
864 {
865 if ( what_action == A_FAX )
866 {
867 mgetty_state = St_incoming_fax;
868 break;
869 }
870
871 lprintf( L_AUDIT,
872 "failed %s dev=%s, pid=%d, caller='%s', conn='%s', name='%s'",
873 what_action == A_TIMOUT? "timeout": "A_FAIL",
874 Device, getpid(), CallerId, Connect, CallName );
875
876 rmlocks();
877 exit(1);
878 }
879 #ifdef CLASS1
880 /* in fax class 1, without adaptive answer, the sequence
881 * "ATA...CONNECT" means "we're in fax mode" - and it means
882 * that the fax1 receiver must not wait for CONNECT, because
883 * it's already there. Boy, is this ugly...
884 */
885 if ( c_bool(fax_only) &&
886 ( modem_type == Mt_class1 || modem_type == Mt_class1_0 ))
887 {
888 fax1_receive_have_connect = TRUE;
889 mgetty_state = St_incoming_fax;
890 break;
891 }
892 #endif
893
894 /* some (old) modems require the host to change port speed
895 * to the speed returned in the CONNECT string, usually
896 * CONNECT 2400 / 1200 / "" (meaning 300)
897 */
898 if ( c_bool(autobauding) )
899 {
900 int cspeed;
901
902 if ( strlen( Connect ) == 0 ) /* "CONNECT\r" */
903 cspeed = 300;
904 else
905 cspeed = atoi(Connect);
906
907 lprintf( L_MESG, "autobauding: switch to %d bps", cspeed );
908
909 if ( tio_check_speed( cspeed ) >= 0 )
910 { /* valid speed */
911 conf_set_int( &c.speed, cspeed );
912 tio_get( STDIN, &tio );
913 tio_set_speed( &tio, cspeed );
914 tio_set( STDIN, &tio );
915 }
916 else
917 {
918 lprintf( L_ERROR, "autobauding: cannot parse 'CONNECT %s'",
919 Connect );
920 }
921 }
922
923 mgetty_state = St_get_login;
924 break;
925
926 case St_incoming_fax:
927 /* incoming fax, receive it (->faxrec.c) */
928
929 lprintf( L_MESG, "start fax receiver..." );
930 get_ugid( &c.fax_owner, &c.fax_group, &uid, &gid );
931 faxrec( c_string(fax_spool_in), c_int(switchbd),
932 uid, gid, c_int(fax_mode),
933 c_isset(notify_mail)? c_string(notify_mail): NULL );
934
935 /* some modems require a manual hangup, with a pause before it. Notably
936 this is the creatix fax/voice modem, which is quite widespread,
937 unfortunately... */
938
939 delay(1500);
940 mdm_command( "ATH0", STDIN );
941
942 rmlocks();
943 exit( 0 );
944 break;
945
946 default:
947 /* unknown machine state */
948
949 lprintf( L_WARN, "unknown state: %s", mgetty_state );
950 exit( 33 );
951 } /* end switch( mgetty_state ) */
952 } /* end while( state != St_get_login ) */
953
954 /* this is "state St_get_login". Not included in switch/case,
955 because it doesn't branch to other states. It may loop for
956 a while, but it will never return
957 */
958
959 /* wait for line to clear (after "CONNECT" a baud rate may
960 be sent by the modem, on a non-MNP-Modem the MNP-request
961 string sent by a calling MNP-Modem is discarded here, too) */
962
963 clean_line( STDIN, 3);
964
965 tio_get( STDIN, &tio );
966 /* honor carrier now: terminate if modem hangs up prematurely
967 * (can be bypassed if modem / serial port broken)
968 */
969 if ( !c_bool( ignore_carrier ) )
970 {
971 tio_carrier( &tio, TRUE );
972 tio_set( STDIN, &tio );
973 }
974 else
975 lprintf( L_MESG, "warning: carrier signal is ignored" );
976
977 /* make utmp and wtmp entry (otherwise login won't work)
978 */
979 make_utmp_wtmp( Device, UT_LOGIN, "LOGIN",
980 strcmp( CallerId, "none" ) != 0 ? CallerId: Connect );
981
982 /* wait a little bit befor printing login: prompt (to give
983 * the other side time to get ready)
984 */
985 delay( c_int(prompt_waittime) );
986
987 /* loop until a successful login is made
988 */
989 for (;;)
990 {
991 /* protect against blocked output (for whatever reason) */
992 signal( SIGALRM, sig_goodbye );
993 alarm( 60 );
994
995 /* set ttystate for /etc/issue ("before" setting) */
996 gettermio(c_string(gettydefs_tag), TRUE, &tio);
997
998 /* we have carrier, assert flow control (including HARD and IXANY!) */
999 tio_set_flow_control( STDIN, &tio, DATA_FLOW | FLOW_XON_IXANY );
1000 tio_set( STDIN, &tio );
1001
1002 #ifdef NeXT
1003 /* work around NeXT's weird problems with POSIX termios vs. sgtty */
1004 NeXT_repair_line(STDIN);
1005 #endif
1006
1007 fputc('\r', stdout); /* just in case */
1008
1009 if (c_isset(issue_file))
1010 {
1011 /* display ISSUE, if desired
1012 */
1013 lprintf( L_NOISE, "print welcome banner (%s)", c_string(issue_file));
1014
1015 if (c_string(issue_file)[0] == '!') /* exec */
1016 {
1017 system( c_string(issue_file)+1 );
1018 }
1019 else if (c_string(issue_file)[0] != '/')
1020 {
1021 printf( "%s\r\n", ln_escape_prompt( c_string(issue_file) ) );
1022 }
1023 else if ( (fp = fopen(c_string(issue_file), "r")) != (FILE *) NULL)
1024 {
1025 while ( fgets(buf, sizeof(buf), fp) != (char *) NULL )
1026 {
1027 char * p = ln_escape_prompt( buf );
1028 if ( p != NULL ) fputs( p, stdout );
1029 fputc('\r', stdout );
1030 }
1031 fclose(fp);
1032 }
1033 }
1034
1035 /* set permissions to "rw-------" for login */
1036 (void) chmod(devname, 0600);
1037
1038 /* set ttystate for login ("after"),
1039 * cr-nl mapping flags are set by getlogname()!
1040 */
1041 #ifdef USE_GETTYDEFS
1042 gettermio(c_string(gettydefs_tag), FALSE, &tio);
1043 tio_set( STDIN, &tio );
1044
1045 lprintf(L_NOISE, "i: %06o, o: %06o, c: %06o, l: %06o, p: %s",
1046 tio.c_iflag, tio.c_oflag, tio.c_cflag, tio.c_lflag,
1047 c_string(login_prompt));
1048 #endif
1049 /* turn off alarm (getlogname has its own timeout) */
1050 alarm(0);
1051
1052 /* read a login name from tty
1053 (if just <cr> is pressed, re-print issue file)
1054
1055 also adjust ICRNL / IGNCR to characters recv'd at end of line:
1056 cr+nl -> IGNCR, cr -> ICRNL, NL -> 0/ and: cr -> ONLCR, nl -> 0
1057 for c_oflag */
1058
1059 if ( getlogname( c_string(login_prompt), &tio, buf, sizeof(buf),
1060 c_bool(blocking)? 0: c_int(login_time),
1061 c_bool(do_send_emsi), c_bool(env_ttyprompt) ) == -1 )
1062 {
1063 continue;
1064 }
1065
1066 /* remove PID file (mgetty is due to exec() login) */
1067 (void) unlink( pid_file_name );
1068
1069 /* dreadful hack for Linux, set TERM if desired */
1070 if ( c_isset(termtype) )
1071 {
1072 set_env_var( "TERM", c_string(termtype) );
1073 }
1074
1075 /* catch "standard question #29" (DCD low -> /bin/login gets stuck) */
1076 i = tio_get_rs232_lines(STDIN);
1077 if ( i != -1 && (( i & TIO_F_DCD ) == 0 ) )
1078 {
1079 lprintf( L_WARN, "WARNING: starting login while DCD is low!" );
1080 }
1081
1082 /* hand off to login dispatcher (which will call /bin/login) */
1083 login_dispatch( buf, mgetty_state == St_callback_login? TRUE: FALSE,
1084 c_string(login_config) );
1085
1086 /* doesn't return, if it does, something broke */
1087 exit(FAIL);
1088 }
1089 }
1090
1091 void
1092 gettermio _P3 ((id, first, tio), char *id, boolean first, TIO *tio )
1093 {
1094 char *rp;
1095
1096 #ifdef USE_GETTYDEFS
1097 static loaded = 0;
1098 GDE *gdp;
1099 #endif
1100
1101 /* default setting */
1102 if ( tio != NULL ) tio_mode_sane( tio, c_bool( ignore_carrier ) );
1103 rp = NULL;
1104
1105 #ifdef USE_GETTYDEFS
1106 /* if gettydefs used, override "tio_mode_sane" settings */
1107
1108 if (!loaded)
1109 {
1110 if (!loadgettydefs(GETTYDEFS)) {
1111 lprintf(L_WARN, "Couldn't load gettydefs - using defaults");
1112 }
1113 loaded = 1;
1114 }
1115 if ( (gdp = getgettydef(id)) != NULL )
1116 {
1117 lprintf(L_NOISE, "Using %s gettydefs entry, \"%s\"", gdp->tag,
1118 first? "before" : "after" );
1119 if (first) /* "before" -> set port speed */
1120 {
1121 if ( c.speed.flags == C_EMPTY || /* still default value */
1122 c.speed.flags == C_PRESET ) /* -> use gettydefs */
1123 conf_set_int( &c.speed, tio_get_speed( &(gdp->before)) );
1124 } else /* "after" -> set termio flags *BUT NOT* speed */
1125 if ( tio != NULL )
1126 {
1127 *tio = gdp->after;
1128 tio_set_speed( tio, c_int(speed) );
1129 }
1130 rp = gdp->prompt;
1131 }
1132
1133 #endif
1134
1135 if ( rp ) /* set login prompt only if still default */
1136 {
1137 if ( c.login_prompt.flags == C_EMPTY ||
1138 c.login_prompt.flags == C_PRESET )
1139 {
1140 c.login_prompt.d.p = (void *) rp;
1141 c.login_prompt.flags = C_CONF;
1142 }
1143 }
1144 }
1145