home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume4 / ringback < prev    next >
Text File  |  1989-02-03  |  8KB  |  278 lines

  1. Path: xanth!mcnc!gatech!cwjcc!hal!ncoast!allbery
  2. From: zeeff@b-tech.UUCP (Jon Zeeff)
  3. Newsgroups: comp.sources.misc
  4. Subject: v04i036: ringback getty for sharing a phone line
  5. Message-ID: <4732@b-tech.UUCP>
  6. Date: 25 Aug 88 18:19:00 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: zeeff@b-tech.UUCP (Jon Zeeff)
  9. Organization: Branch Technology, Ann Arbor, MI
  10. Lines: 265
  11. Approved: allbery@ncoast.UUCP
  12.  
  13. Posting-number: Volume 4, Issue 36
  14. Submitted-by: "Jon Zeeff" <zeeff@b-tech.UUCP>
  15. Archive-name: ringback
  16.  
  17. [People, *please* "shar" your submissions!!!!!
  18.  
  19. Apologies for the delay; "jetroff" was clogging the pipes.  It will probably
  20. start going out on September 6.  ++bsa]
  21.  
  22. #! /bin/sh
  23. echo x - ringback.c
  24. sed 's/^X//' << \EOF > ringback.c
  25. X/*
  26. X
  27. XRingback.c V1.3
  28. X
  29. XThis program implements a ring-back system that allows a phone line 
  30. Xthat is normally used as a voice line and a dial-out data line to be 
  31. Xused as a limited dial-in data and voice line.  It does not interfere 
  32. Xwith the use of the line for an answering machine.  
  33. X
  34. XIt solves a problem for my situation:
  35. X
  36. X1) I am normally in my office.  The phone line is used for dial-out via
  37. Xmodem and dial-in/dial-out voice traffic.
  38. X
  39. X2) I sometimes travel, at which time I connect an answering machine to
  40. Xthe line.  
  41. X
  42. X3) I'd like to be able to call in to my system via modem when I am 
  43. Xtravelling, without interfering with others use of my answering 
  44. Xmachine.  
  45. X
  46. X4) I don't want to pay for another phone line.
  47. X
  48. XThis program functions by watching a tty port for an incoming call on 
  49. Xa smart modem.  Forty seconds after an incoming call, it will spawn a 
  50. Xgetty on the line.  If no one calls and logs in, the getty will be 
  51. Xkilled by an uncaught alarm and the line will be set back to a no 
  52. Xanswer state.  
  53. X
  54. XThis implements a ringback dialin system.  Just call, wait a minute,
  55. Xand call again. 
  56. X
  57. XIf the program notices that a user (uucp or kermit) is using the line 
  58. Xfor an outgoing call, it will back off until that call is completed.  
  59. X(Note: the first modem response char will be eaten.  Uucp dialing scripts 
  60. Xshould be set up to tolerate this.) 
  61. X
  62. XWritten for Sys V.3 and HDB uucp (but easily modified for others)
  63. X
  64. XTwo arguments must be present - tty and type
  65. X
  66. XSample lines from /etc/inittab:
  67. X     rb:1234:respawn:/etc/ringback tty1 1200
  68. X
  69. XMake sure that you trust the people who are allowed to login on this 
  70. Xline.  It is possible for them to dial-out on the line.  
  71. X
  72. XTo install:
  73. X
  74. X1) Edit TTYID and TTYGID and UUGETTY.
  75. X2) Compile and install as /etc/ringback
  76. X3) Create an initab line like the above one.
  77. X
  78. X                 - Jon Zeeff (umix!b-tech!zeeff)
  79. X
  80. XThis program is in the public domain.
  81. X
  82. X*/
  83. X
  84. X#include <stdio.h>
  85. X#include <sys/types.h>
  86. X#include <sys/stat.h>
  87. X#include <signal.h>
  88. X#include <fcntl.h>
  89. X#include <termio.h>
  90. X
  91. Xchar *strcpy();
  92. Xchar *strcat();
  93. Xunsigned sleep();
  94. Xunsigned alarm();
  95. Xvoid exit();
  96. X
  97. X/* the id and gid for normal ownership of tty lines (usually uucp) */
  98. X
  99. X#define TTYID        5    /* uucp */
  100. X#define TTYGID        11    /* staff */
  101. X
  102. X/* if you have a getty/tty/modem combination that doesn't get in a babbling
  103. X   getty loop, then there is no need to define this.  If you do, make sure
  104. X   that /etc/uugetty exists.
  105. X*/ 
  106. X#define UUGETTY        /* #define UUGETTY */
  107. X
  108. Xmain(argc,argv)
  109. Xint argc;
  110. Xchar **argv;
  111. X{
  112. X
  113. Xchar c;
  114. XFILE *tty0,*tty1,*tty2;
  115. X#ifdef DEBUG
  116. XFILE *console;
  117. X#endif
  118. XFILE *tty;
  119. XFILE *lock;
  120. Xstruct termio ttymodes;
  121. Xstruct stat buf;
  122. Xchar tty_lock[100];
  123. Xchar tty_name[100];
  124. Xint i;
  125. X
  126. X/* name and location of tty locks - change this as needed */
  127. X
  128. Xstrcpy(tty_lock,"/usr/spool/locks/LCK.."); 
  129. Xstrcat(tty_lock,argv[1]);
  130. X
  131. Xstrcpy(tty_name,"/dev/");
  132. Xstrcat(tty_name,argv[1]);
  133. X
  134. Xif (argc != 3) exit(2);
  135. X
  136. Xunlink(tty_lock);
  137. Xumask(022);
  138. X
  139. Xsignal(SIGHUP,SIG_IGN);
  140. Xsignal(SIGINT,SIG_IGN);
  141. Xsignal(SIGQUIT,SIG_IGN);
  142. Xsignal(SIGTERM,SIG_IGN);
  143. X
  144. Xfor (;;) {
  145. X   
  146. X      /* Set the line permissions, owner, and group for dial-out use */
  147. X   
  148. X      chown(tty_name,TTYID,TTYGID);
  149. X      chmod(tty_name,0660);
  150. X   
  151. X      /* Open line - getty will close the first 3, so make sure we are #4 */
  152. X      /* so that the modem doesn't get reset on dtr drop and our autoanswer */
  153. X      /* setting don't get undone. */
  154. X
  155. X      tty0 = fopen(tty_name,"r+");
  156. X      tty1 = fopen(tty_name,"r+");
  157. X      tty2 = fopen(tty_name,"r+");
  158. X      tty = fopen(tty_name,"r+");
  159. X
  160. X      fclose(tty0); fclose(tty1); fclose(tty2);
  161. X
  162. X#ifdef DEBUG
  163. Xconsole = fopen("/dev/console","w");
  164. Xfprintf(console,"check 1\n"); fflush(console);
  165. X#endif
  166. X
  167. X      ttymodes.c_iflag = 0;
  168. X      ttymodes.c_oflag = NL1 | CR3;
  169. X      ttymodes.c_cflag = 0; 
  170. X      ttymodes.c_lflag = 0;
  171. X      ttymodes.c_line = 0;
  172. X      for (i = 0; i < NCC; ++i) ttymodes.c_cc[i] = 0; 
  173. X      ttymodes.c_cc[VMIN] = 1;        /* min number of characters */
  174. X      ttymodes.c_cc[VTIME] = 0;         /* no max time before return */
  175. X      ioctl(fileno(tty),TCSETA,&ttymodes);  /* hang up the line */
  176. X      sleep(1);
  177. X      ttymodes.c_cflag = B1200 | CS8 | CREAD | HUPCL; 
  178. X      ioctl(fileno(tty),TCSETA,&ttymodes);
  179. X      sleep(1);
  180. X#ifdef DEBUG
  181. Xfprintf(console,"check 1.3\n"); fflush(console);
  182. X#endif
  183. X   
  184. X      /* setup the modem */
  185. X   
  186. X      fputs("AT\r",tty); fflush(tty); sleep(1);  
  187. X      fputs("ATZ\r",tty); fflush(tty); sleep(2);  
  188. X      fputs("ATS0=0\r",tty); fflush(tty); sleep(1);  /* Don't answer phone */
  189. X      fputs("ATE0\r",tty); fflush(tty); sleep(1);
  190. X      fputs("ATV0\r",tty); fflush(tty); sleep(1);
  191. X      fputs("ATQ0\r",tty); fflush(tty); sleep(1);
  192. X   
  193. X#ifdef DEBUG
  194. Xfprintf(console,"check 2\n"); fflush(console);
  195. X#endif
  196. X      fcntl(fileno(tty),F_SETFL,O_NDELAY);    /* Set to nonblocking reads */
  197. X      while (read(fileno(tty),&c,(unsigned)1) == 1);   /* Flush input */
  198. X      fcntl(fileno(tty),F_SETFL,O_RDWR);      /* Set to blocking reads */
  199. X   
  200. X      /* Wait for incoming call ('2') or other usage (uucp or user) */
  201. X   
  202. X      /* Get char from tty port */
  203. X
  204. X      while (read(fileno(tty),&c,(unsigned)1) != 1);
  205. X
  206. X      /* 
  207. X         If we receive a '2' and no lock file is present, we can be sure
  208. X         that it is not someone calling out.
  209. X      */   
  210. X
  211. X#ifdef DEBUG
  212. Xfprintf(console,"check 2.5 %d\n",c); fflush(console);
  213. X#endif
  214. X
  215. X      if ((c&0x7f) == '2' && stat(tty_lock,&buf) == -1) {    
  216. X
  217. X          /* create a hdb style lock file */
  218. X
  219. X              lock = fopen(tty_lock,"w");
  220. X                if (lock) {
  221. X                   fprintf(lock,"%10.10d\n",(int)getpid());
  222. X                   fclose(lock);
  223. X             }
  224. X
  225. X          /* wait for this call to end */
  226. X     
  227. X              sleep(32);    /* should be long enough for answering machine
  228. X                   to answer if they let it ring. */
  229. X  
  230. X          /* Setup modem for incoming call */
  231. X              fputs("AT\r",tty);fflush(tty);sleep(1);
  232. X              fputs("ATZ\r",tty);fflush(tty);sleep(1);
  233. X              fputs("ATQ1\r",tty);fflush(tty);sleep(1); 
  234. X              fputs("ATS0=1\r",tty);fflush(tty);sleep(5);
  235. X
  236. X#ifdef DEBUG
  237. Xfprintf(console,"check 4\n"); fflush(console);
  238. X#endif
  239. X
  240. X                /* set things up so the getty will live a short life */
  241. X              alarm(120);
  242. X#ifndef UUGETTY  
  243. X              execl("/etc/getty","getty","-t","90","-h",
  244. X                   argv[1],argv[2],(char *)0);
  245. X#else
  246. X              unlink(tty_lock);
  247. X              execl("/etc/uugetty","uugetty","-r","-h","-t","90",
  248. X                   argv[1],argv[2],(char *)0);
  249. X#endif
  250. X
  251. X#ifdef DEBUG
  252. Xfprintf(console,"check 5\n"); fflush(console);
  253. X#endif
  254. X       } else {                         /* Must be someone using line */
  255. X   
  256. X              fclose(tty);                 /* Release line */
  257. X   
  258. X              /* Loop until lock file goes away (uucp or user done) */
  259. X    
  260. X              do {
  261. X                 while (stat(tty_lock,&buf) == 0) sleep(40);
  262. X                sleep(40);
  263. X             }  while (stat(tty_lock,&buf) == 0);
  264. X   
  265. X       } /* end_if */
  266. X   
  267. X   } /* next */
  268. X   
  269. X}
  270. EOF
  271. exit 0
  272. -- 
  273. Jon Zeeff                   Branch Technology,
  274. uunet!umix!b-tech!zeeff      zeeff%b-tech.uucp@umix.cc.umich.edu
  275.