4 * (c) 1995 Martin Mares
18 #define MODEM_PORT "/dev/cua3"
22 int termios_backed_up;
24 struct termios old_termios;
26 struct termios new_termios = {
32 { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
48 do pause(); while (!alarm_got);
54 int o = new_termios.c_cflag;
55 fprintf(stderr, "Pulsing DTR\n");
56 new_termios.c_cflag &= ~CBAUD;
57 if (tcsetattr(modem_handle, TCSANOW, &new_termios))
58 err("Unable to lower DTR");
60 new_termios.c_cflag = o;
61 if (tcsetattr(modem_handle, TCSANOW, &new_termios))
62 err("Unable to raise DTR");
68 fprintf(stderr, "Sending %s", s);
69 write(modem_handle, s, strlen(s));
73 wait_for(char *c, int tim)
78 fprintf(stderr,"Waiting %d sec for %s\n", tim, c);
82 l = read(modem_handle, &b, 1);
92 fprintf(stderr, "Got %c\n", b);
106 if (!wait_for("OK",5))
114 if (!fcntl(modem_handle, F_SETFL, O_NONBLOCK))
116 while (read(modem_handle, buf, 256) > 0) ;
117 fcntl(modem_handle, F_SETFL, 0);
122 modem_init(char *name)
124 modem_handle = open(name, O_RDWR);
125 if (modem_handle < 0)
126 err("Unable to open modem device");
127 if (tcgetattr(modem_handle, &old_termios))
128 err("Unable to fetch old termios");
129 termios_backed_up = 1;
130 new_termios.c_cflag = (old_termios.c_cflag | CRTSCTS) & ~HUPCL;
131 if (!(new_termios.c_cflag & CBAUD))
132 new_termios.c_cflag |= B19200;
133 if (tcsetattr(modem_handle, TCSANOW, &new_termios))
134 err("Unable to set termios");
152 if (modem_handle < 0)
154 fprintf(stderr, "Cleanup.\n");
163 if (termios_backed_up)
165 termios_backed_up = 0;
166 if (tcsetattr(modem_handle, TCSANOW, &old_termios))
167 err2("Termios cannot be restored");
169 fprintf(stderr, "Done.\n");
177 fprintf(stderr, "Caught fatal signal %d -- aborting !\n", n);
186 signal(SIGALRM, sig_alarm);
195 signal(SIGALRM, sig_alarm);
211 sprintf(buf, "atd%s\r\n", nr);
213 i = wait_for("CONNECT", 60);
219 struct termios tios = {
220 ICRNL | IXON | IXOFF,
223 ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE,
225 { 3, 0x1c, 0x7f, 0x15, 4, 0, 1, 0, 0x11, 0x13, 0x1a, 0, 0x12, 0x0f, 0x17,
234 char *port = MODEM_PORT;
237 err("Cannot fork()");
246 tios.c_cflag = new_termios.c_cflag | HUPCL;
247 tcsetattr(0, TCSANOW, &tios);
248 utmpname(_PATH_UTMP);
249 memset(&ut, 0, sizeof(ut));
250 strcpy(ut.ut_user, "LOGIN");
251 strcpy(ut.ut_line, port+5);
252 strncpy(ut.ut_id, ut.ut_line + 3, sizeof(ut.ut_id));
254 ut.ut_type = LOGIN_PROCESS;
255 ut.ut_pid = getpid();
258 wt = open(_PATH_WTMP, O_APPEND|O_WRONLY);
262 write(wt, &ut, sizeof(ut));
266 execl("/usr/sbin/callback-login", "/usr/sbin/callback-login", NULL);
271 i = open(MODEM_PORT, O_RDWR);
274 if (modem_handle >= 0)
275 tcsetattr(modem_handle, TCSANOW, &new_termios);
280 main(int argc, char **argv)
286 fprintf(stderr, "Must be run by root!\n");
289 if (getuid() != 1000 && getuid())
291 fprintf(stderr,"Only mj is allowed to call this utility!\n");
296 fprintf(stderr,"Usage: callback <number>\n");
303 modem_init(MODEM_PORT);
306 fprintf(stderr, "Connected...\n");
308 fprintf(stderr, "Disconnecting...\n");
313 fprintf(stderr, "Retrying...\n");
315 fprintf(stderr, "Failed.\n");