1/*	$OpenBSD: acu.c,v 1.12 2006/03/17 14:43:06 moritz Exp $	*/
2/*	$NetBSD: acu.c,v 1.4 1996/12/29 10:34:03 cgd Exp $	*/
3
4/*
5 * Copyright (c) 1983, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: stable/11/usr.bin/tip/tip/acu.c 353589 2019-10-15 20:04:15Z brooks $");
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)acu.c	8.1 (Berkeley) 6/6/93";
39static const char rcsid[] = "$OpenBSD: acu.c,v 1.12 2006/03/17 14:43:06 moritz Exp $";
40#endif
41#endif /* not lint */
42
43#include "tip.h"
44
45static acu_t *acu = NOACU;
46static int conflag;
47static void acuabort(int);
48static acu_t *acutype(char *);
49static jmp_buf jmpbuf;
50/*
51 * Establish connection for tip
52 *
53 * If DU is true, we should dial an ACU whose type is AT.
54 * The phone numbers are in PN, and the call unit is in CU.
55 *
56 * If the PN is an '@', then we consult the PHONES file for
57 *   the phone numbers.  This file is /etc/phones, unless overriden
58 *   by an exported shell variable.
59 *
60 * The data base files must be in the format:
61 *	host-name[ \t]*phone-number
62 *   with the possibility of multiple phone numbers
63 *   for a single host acting as a rotary (in the order
64 *   found in the file).
65 */
66char *
67con(void)
68{
69	char *cp = PN;
70	char *phnum, string[256];
71	FILE *fd;
72	volatile int tried = 0;
73
74	if (!DU) {		/* regular connect message */
75		if (CM != NOSTR)
76			parwrite(FD, CM, size(CM));
77		logent(value(HOST), "", DV, "call completed");
78		return (NOSTR);
79	}
80	/*
81	 * @ =>'s use data base in PHONES environment variable
82	 *        otherwise, use /etc/phones
83	 */
84	signal(SIGINT, acuabort);
85	signal(SIGQUIT, acuabort);
86	if (setjmp(jmpbuf)) {
87		signal(SIGINT, SIG_IGN);
88		signal(SIGQUIT, SIG_IGN);
89		printf("\ncall aborted\n");
90		logent(value(HOST), "", "", "call aborted");
91		if (acu != NOACU) {
92			setboolean(value(VERBOSE), FALSE);
93			if (conflag)
94				disconnect(NOSTR);
95			else
96				(*acu->acu_abort)();
97		}
98		return ("interrupt");
99	}
100	if ((acu = acutype(AT)) == NOACU)
101		return ("unknown ACU type");
102	if (*cp != '@') {
103		while (*cp) {
104			phnum = cp;
105			cp += strcspn(cp, ",");
106			if (*cp != '\0')
107				*cp++ = '\0';
108
109			if (strlen(phnum) == 0)
110				continue;
111
112			conflag = (*acu->acu_dialer)(phnum, CU);
113			if (conflag)
114				break;
115
116			logent(value(HOST), phnum, acu->acu_name, "call failed");
117			tried++;
118		}
119	} else {
120		if ((fd = fopen(PH, "r")) == NOFILE) {
121			printf("%s: ", PH);
122			return ("can't open phone number file");
123		}
124		while (fgets(string, sizeof(string), fd) != NOSTR) {
125			cp = &string[strcspn(string, " \t\n")];
126			if (*cp != '\0')
127				*cp++ = '\0';
128
129			if (strcmp(string, value(HOST)) != 0)
130				continue;
131
132			cp += strspn(cp, " \t\n");
133			phnum = cp;
134			*(cp + strcspn(cp, ",\n")) = '\0';
135
136			if (strlen(phnum) == 0)
137				continue;
138
139			conflag = (*acu->acu_dialer)(phnum, CU);
140			if (conflag)
141				break;
142
143			logent(value(HOST), phnum, acu->acu_name, "call failed");
144			tried++;
145		}
146		fclose(fd);
147	}
148	if (conflag) {
149		if (CM != NOSTR)
150			parwrite(FD, CM, size(CM));
151		logent(value(HOST), phnum, acu->acu_name, "call completed");
152		return (NOSTR);
153	} else if (!tried) {
154		logent(value(HOST), "", acu->acu_name, "missing phone number");
155		return ("missing phone number");
156	} else {
157		(*acu->acu_abort)();
158		return ("call failed");
159	}
160}
161
162void
163disconnect(char *reason)
164{
165	if (!conflag) {
166		logent(value(HOST), "", DV, "call terminated");
167		return;
168	}
169	if (reason == NOSTR) {
170		logent(value(HOST), "", acu->acu_name, "call terminated");
171		if (boolean(value(VERBOSE)))
172			printf("\r\ndisconnecting...");
173	} else
174		logent(value(HOST), "", acu->acu_name, reason);
175	(*acu->acu_disconnect)();
176}
177
178static void
179acuabort(int s)
180{
181	signal(s, SIG_IGN);
182	longjmp(jmpbuf, 1);
183}
184
185static acu_t *
186acutype(char *s)
187{
188	acu_t *p;
189	extern acu_t acutable[];
190
191	for (p = acutable; p->acu_name != NULL; p++)
192		if (!strcmp(s, p->acu_name))
193			return (p);
194	return (NOACU);
195}
196