File: ifcico.c

package info (click to toggle)
ifmail 2.14tx8.10-32
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,056 kB
  • sloc: ansic: 30,328; perl: 4,955; yacc: 839; makefile: 716; sh: 424; cpp: 235; lex: 206; awk: 24
file content (267 lines) | stat: -rw-r--r-- 6,065 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#ifdef HAS_SYSLOG
#include <syslog.h>
#endif
#include "getopt.h"
#include "lutil.h"
#include "xutil.h"
#include "ftn.h"
#include "nodelist.h"
#include "trap.h"
#include "config.h"
#include "version.h"
#include "needed.h"
#include "setproctitle.h"

#if defined(SHORT_PID_T)
#define pid_t short
#elif defined(INT_PID_T)
#define pid_t int
#endif

int master=0;
int forcedcalls=0;
char *forcedphone=NULL;
char *forcedline=NULL;
#if defined(HAS_TCP) || defined(HAS_TERM)
char *inetaddr=NULL;
int tcp_mode=0;
#endif
#ifdef NOISEDEBUG
int junklevel=0;
#endif

int call(faddr *);
fa_list *callall(void);
int answer(char *);
void mkdirs(char*);
void setargspace(char**,char**);

void usage(void)
{
#ifdef NOISEDEBUG
#if defined(HAS_TCP) || defined(HAS_TERM)
	confusage("-r<role> -j<num> -a<inetaddr> -t<mode> <node> ...");
#else
	confusage("-r<role> -j<num> <node> ...");
#endif
	fprintf(stderr,_("-j<num>\t\tdamage every <num> byte\t[%d]\n"),
								junklevel);
#else
#if defined(HAS_TCP) || defined(HAS_TERM)
	confusage("-r<role> -a<inetaddr> -t<mode> <node> ...");
#else
	confusage("-r<role> <node> ...");
#endif
#endif
	fprintf(stderr,_("-r 0|1\t\t1 - master, 0 - slave\t[0]\n"));
	fprintf(stderr,_("-n<phone>\tforced phone number\n"));
	fprintf(stderr,_("-l<ttydevice>\tforced tty device\n"));
#if defined(HAS_TCP) || defined(HAS_TERM)
	fprintf(stderr,_("-a<inetaddr>\tuse TCP/IP instead of modem\n"));
	fprintf(stderr,_("-t<mode>\t0 - IFC, 1 - telnet\t[0]\n"));
#endif
	fprintf(stderr,_("  <node>\tshould be in domain form, e.g. f11.n22.z3\n"));
	fprintf(stderr,_("\t\t(this implies master mode)\n"));
	fprintf(stderr,_("\n or: %s tsync|yoohoo|**EMSI_INQC816\n"),myname);
	fprintf(stderr,_("\t\t(this implies slave mode)\n"));
}

int main(argc,argv,envp)
int argc;
char *argv[];
char *envp[];
{
	int c,uid;
	fa_list *callist=NULL,**tmpl;
	faddr *tmp;
	int rc,maxrc,callno=0,succno=0;
	char *answermode=NULL,*p;
#ifdef NEED_FORK
	pid_t child,waitret;
	int status;
#endif

	initsetproctitle(argc, argv, envp);

#if defined(HAS_SYSLOG) && defined(CICOLOG)
	logfacility=CICOLOG;
#endif

	setmyname(argv[0]);
	catch(myname);
#ifdef NOISEDEBUG
#if defined(HAS_TCP) || defined(HAS_TERM)
	while ((c=getopt(argc,argv,"j:x:r:n:l:a:t:I:h")) != -1)
#else
	while ((c=getopt(argc,argv,"j:x:r:n:l:I:h")) != -1)
#endif
#else
#if defined(HAS_TCP) || defined(HAS_TERM)
	while ((c=getopt(argc,argv,"x:r:n:l:a:t:I:h")) != -1)
#else
	while ((c=getopt(argc,argv,"x:r:n:l:I:h")) != -1)
#endif
#endif
	if (confopt(c,optarg)) switch (c)
	{
#ifdef NOISEDEBUG
	case 'j':	junklevel=atoi(optarg); break;
#endif
	case 'r':	master=atoi(optarg);
			if ((master != 0) && (master != 1))
			{
				usage();
				exit(1);
			}
			break;
	case 'l':	forcedline=xstrcpy(optarg); break;
#if defined(HAS_TCP) || defined(HAS_TERM)
	case 'a':	inetaddr=xstrcpy(optarg); break;
	case 't':	tcp_mode=atoi(optarg); break;
#endif
	case 'n':	forcedphone=xstrcpy(optarg); break;
	default:	usage(); exit(1);
	}

	if (readconfig())
	{
		fprintf(stderr,"Error getting configuration, aborting\n");
		exit(1);
	}

	maxpsize=0L; /* we want classic packet names here */

	tmpl=&callist;

	while (argv[optind])
	{
		for (p=argv[optind];(*p) && (*p == '*');p++);
		if ((strcasecmp(argv[optind],"tsync") == 0) ||
		    (strcasecmp(argv[optind],"yoohoo") == 0) ||
		    (strcasecmp(argv[optind],"binkp") == 0) ||
		    (strncasecmp(p,"EMSI_",5) == 0))
		{
			master=0;
			answermode=argv[optind];
			debug(10,"inbound \"%s\" mode",S(answermode));
		}
		else
		{
			debug(8,"callist entry \"%s\"",argv[optind]);
			if ((tmp=parsefaddr(argv[optind])))
			{
				*tmpl=(fa_list *)xmalloc(sizeof(fa_list));
				(*tmpl)->next=NULL;
				(*tmpl)->addr=tmp;
				tmpl=&((*tmpl)->next);
			}
			else logerr("unrecognizable address \"%s\"",argv[optind]);
		}
		optind++;
	}

	if (callist)
	{
		master=1;
		forcedcalls=1;
	}

	/*
	   The following witchkraft about uid-s is necessary to make
	   access() work right.  Unforunately, access() checks the real
	   uid, if ifcico is invoked with supervisor real uid (as when
	   called by uugetty) it returns X_OK for the magic files that
	   even do not have `x' bit set.  Therefore, `reference' magic
	   file requests are taken for `execute' requests (and the
	   actual execution natually fails).  Here we set real uid equal
	   to effective.  If real uid is not zero, all these fails, but
	   in this case it is not necessary anyway.
	*/

	uid=geteuid();
	seteuid(0);
	setuid(uid);
	seteuid(uid);
	debug(2,"uid=%d, euid=%d",getuid(),geteuid());

	umask(066); /* packets may contain confidential information */

#ifdef HAS_BSD_SIGNALS
	siginterrupt(SIGALRM,1);
	siginterrupt(SIGPIPE,1);
#endif

	p=xstrcpy(inbound);
	p=xstrcat(p,"/tmp/");
	mkdirs(p);
	free(p);
	p=xstrcpy(listinbound);
	p=xstrcat(p,"/tmp/");
	mkdirs(p);
	free(p);
	p=xstrcpy(protinbound);
	p=xstrcat(p,"/tmp/");
	mkdirs(p);
	free(p);

	maxrc=0;
	if (master)
	{

#ifdef NEED_FORK
		if ((child=fork()))
		{
			if (child == -1)
			{
				logerr("$fork() error");
				exit(1);
			}
			while (((waitret=wait(&status)) != -1) &&
			       (waitret != child))
			{
				logerr("wait return %d, status %d,%d",
					waitret,status>>8,status&0xff);
			}
			if (status&0xff) kill(getpid(),status&0xff);
			else exit(status>>8);
		}
#endif

		if (callist == NULL) callist=callall();
		for (tmpl=&callist;*tmpl;tmpl=&((*tmpl)->next))
		{
			callno++;
			rc=call((*tmpl)->addr);
			loginf("call to %s %s (rc=%d)",
				ascfnode((*tmpl)->addr,0x1f),
				rc?"failed":"successful",rc);
			if (rc > maxrc) maxrc=rc;
			if (rc == 0) succno++;
		}
	}
	else /* slave */
	{
#ifdef HAS_TCP
		if (!answermode && tcp_mode==TCPMODE_BINKP)
			answermode=xstrcpy("binkp");
#endif
		setproctitle("-ifcico answering");
		maxrc=answer(answermode);
		callno=1;
		succno=(maxrc == 0);
	}

	if (callno)
		loginf("%d of %d calls, maxrc=%d",succno,callno,maxrc);

	return maxrc;
}