-
Notifications
You must be signed in to change notification settings - Fork 24
/
srif.c
357 lines (333 loc) · 8.43 KB
/
srif.c
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* srif.c -- Create flags or run external programs on mail events
*
* srif.c is a part of binkd project
*
* Copyright (C) 1996-2002 Dima Maloff, 5047/13 and others
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. See COPYING.
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "sys.h"
#include "readcfg.h"
#include "protoco2.h"
#include "srif.h"
#include "ftnaddr.h"
#include "tools.h"
#include "run.h"
#include "ftnq.h"
#include "prothlp.h"
#include "protocol.h"
#include "rfc2553.h"
static EVTQ *evt_queue(EVTQ *eq, char evt_type, char *path)
{
EVTQ *tmp;
for (tmp=eq; tmp; tmp=tmp->next)
{ if (tmp->evt_type == evt_type && strcmp(tmp->path, path) == 0)
return eq; /* already exists */
}
tmp = xalloc(sizeof(EVTQ));
tmp->evt_type = evt_type;
tmp->path = xstrdup(path);
tmp->next = eq;
return tmp;
}
/*
* Tests if filename matches any of EVT_FLAG's patterns.
*/
int evt_test (EVTQ **eq, char *filename, EVT_FLAG *evt_flags)
{
EVT_FLAG *curr;
int rc=0;
for (curr = evt_flags; curr; curr = curr->next)
{
if (pmatch_ncase(curr->pattern, filename))
{
if (curr->path)
{
Log (4, "got %s, %screating %s", curr->pattern, curr->imm ? "" : "delayed ", curr->path);
if (curr->imm)
{
if (create_empty_sem_file (curr->path) == 0)
touch (curr->path, time(NULL));
}
else
*eq = evt_queue(*eq, 'f', curr->path);
}
else if (curr->command)
{
Log (4, "got %s, %sstarting %s", curr->pattern, curr->imm ? "" : "delayed ", curr->command);
rc=1;
}
}
}
return rc;
}
/*
* Sets flags for all matched with evt_test events
*/
void evt_set (EVTQ *eq)
{
EVTQ *curr;
while (eq)
{
if (eq->evt_type == 'e')
{
Log (4, "Running %s", eq->path);
run(eq->path);
}
else
{
Log (4, "Creating %s", eq->path);
if (create_empty_sem_file(eq->path) == 0)
touch(eq->path, time(NULL));
}
curr = eq->next;
free(eq->path);
free(eq);
eq = curr;
}
}
/* Makes SRIF request and SRIF response names from FTN address of a node */
static int mksrifpaths (FTN_ADDR *fa, char *srf, char *rsp, BINKD_CONFIG *config)
{
ftnaddress_to_filename (srf, fa, config);
if (*srf)
{
strnzcpy (rsp, srf, MAXPATHLEN);
strnzcat (srf, ".srf", MAXPATHLEN);
strnzcat (rsp, ".rsp", MAXPATHLEN);
return 1;
}
else
return 0;
}
static int srif_fill (char *path, FTN_ADDR *fa, int nfa,
char *req, char *rsp, int prot, int listed,
char *peer_name, char *peer_sysop)
{
FILE *out;
int i;
if ((out = fopen (path, "w")) != 0)
{
fprintf (out, "Sysop %s\n", peer_sysop);
for (i = 0; i < nfa; ++i)
{
char adr[FTN_ADDR_SZ + 1];
ftnaddress_to_str (adr, fa + i);
fprintf (out, "AKA %s\n", adr);
}
fprintf (out, "Baud 115200\n");
fprintf (out, "Time -1\n");
fprintf (out, "RequestList %s\n", req);
fprintf (out, "ResponseList %s\n", rsp);
fprintf (out, "RemoteStatus %s\n", prot ? "PROTECTED" : "UNPROTECTED");
fprintf (out, "SystemStatus %s\n", listed ? "LISTED" : "UNLISTED");
fprintf (out, "CallerID %s\n", peer_name);
fprintf (out, "SessionType OTHER\n");
fclose (out);
return 1;
}
return 0;
}
static FTNQ *parse_response (FTNQ *q, char *rsp, FTN_ADDR *fa, BINKD_CONFIG *config)
{
FILE *in = fopen (rsp, "r");
if (in)
{
char buf[MAXPATHLEN + 1];
int i;
int haveSomething = 0;
while (!feof (in))
{
if (!fgets (buf, MAXPATHLEN, in))
break;
haveSomething = 1;
for (i = 0; i < sizeof (buf) - 1 && !isspace (buf[i]); ++i);
buf[i] = 0;
Log (4, "parse_response: add file `%s' to queue", buf + 1);
switch (*buf)
{
case '=':
q = q_add_last_file (q, buf + 1, fa, 'h', 'd', 0, config);
break;
case '+':
q = q_add_last_file (q, buf + 1, fa, 'h', 0, 0, config);
break;
case '-':
q = q_add_last_file (q, buf + 1, fa, 'h', 'a', 0, config);
break;
default:
Log (2, "parse_response: unknown predictor `%c', ignored response file `%s'", *buf, buf + 1);
break;
}
}
if (!haveSomething)
Log (3, "SRIF response file is empty");
fclose (in);
}
else
Log (1, "parse_response: %s: %s", rsp, strerror (errno));
return q;
}
static char valid_filename_chars[]=".\\-_:/@";
static EVTQ *run_args(EVTQ *eq, char *cmd, char *filename0, STATE *st, int imm)
{
char *sp, *w;
char *fn=filename0;
char *pn=st->peer_name ? st->peer_name : "";
char aka[4];
char adr[FTN_ADDR_SZ + 2];
int i;
unsigned int l;
size_t sw=1024;
int use_fn=0;
for (i=0; fn[i]; i++)
if ((!isalnum(fn[i])) && (!strchr(valid_filename_chars, fn[i])))
{
fn="-";
Log(4, "Invalid filename (%s) received!", filename0);
break;
}
for (i=0; pn[i]; i++)
if ((!isalnum(pn[i])) && (!strchr(valid_filename_chars, pn[i])))
{
i=0;
break;
}
if (!i)
pn = st->ipaddr;
if (sw<=strlen(cmd)) sw=strlen(cmd)+1;
w=(char*)xalloc(sw);
strcpy(w, cmd);
strcpy(aka, "*A0");
for (l=0; (l<strlen(w)) && ((sp=strchr(w+l, '*'))!=NULL); l++)
{
l=sp-w;
switch(toupper(sp[1]))
{
case 'N': /* short filename for win32 */
#ifdef WIN32
{
char ts[MAXPATHLEN+1];
i=GetShortPathName(filename0, ts, sizeof(ts));
if ((i<1) || (i>MAXPATHLEN))
{
Log(2, "GetShortPathName() fails %d", GetLastError());
use_fn=1;
strcpy(ts, fn);
}
w = ed (w, "*N", ts, &sw);
break;
}
#endif
use_fn=1;
w = ed (w, "*N", fn, &sw);
break;
case 'F': /* filename */
use_fn=1;
w = ed (w, "*F", fn, &sw);
break;
case 'A': /* AKA */
if (sp[2] == '@' || sp[2] == '*')
{
char *pa, *addrlist;
pa = addrlist = malloc(st->nfa*(FTN_ADDR_SZ+1l));
if (addrlist == NULL)
{ Log(1, "Not enough memory, %ld bytes needed", st->nfa*(FTN_ADDR_SZ+1l));
break;
}
for (i=0; i<st->nfa; i++)
{ if (pa != addrlist) *pa++=' ';
ftnaddress_to_str (pa, st->fa+i);
pa += strlen(pa);
}
aka[2]=sp[2];
w = ed (w, aka, addrlist, &sw);
free(addrlist);
break;
}
i=isdigit(sp[2])?(sp[2]-'0'):0;
if (i<st->nfa)
ftnaddress_to_str (adr, st->fa+i);
else
strcpy(adr, "-");
aka[2]=sp[2];
if (!isdigit(sp[2])) /* If you will remove it, *A *A1 may not work */
{
i=strlen(adr);
adr[i++]=sp[2];
adr[i]=0;
}
w = ed (w, aka, adr, &sw);
break;
case 'P': /* protected ? */
w = ed (w, "*P", st->state == P_SECURE ? "1" : "0", &sw);
break;
case 'L': /* listed ? */
w = ed (w, "*L", st->listed_flag ? "1" : "0", &sw);
break;
case 'H': /* hostname or IP */
w = ed (w, "*H", pn, &sw);
break;
}
}
if ((fn!=filename0) && (use_fn))
Log (1, "Security problem. Execution aborted...");
else if (imm) /* immediate event */
run(w);
else
eq = evt_queue(eq, 'e', w);
free(w);
return eq;
}
/*
* Runs external programs using S.R.I.F. interface
* if the name matches one of our "exec"'s
*/
FTNQ *evt_run (FTNQ *q, char *filename, int imm_freq,
STATE *st, BINKD_CONFIG *config)
{
EVT_FLAG *curr;
for (curr = config->evt_flags.first; curr; curr = curr->next)
{
if (curr->command && pmatch_ncase(curr->pattern, filename))
{
if (strstr (curr->command, "*S") || strstr (curr->command, "*s"))
{
if (imm_freq)
{
char srf[MAXPATHLEN + 1], rsp[MAXPATHLEN + 1];
if (mksrifpaths (st->fa, srf, rsp, config))
{
char *w = ed (curr->command, "*S", srf, NULL);
if (srif_fill (srf, st->fa, st->nfa, filename, rsp,
st->state == P_SECURE, st->listed_flag,
st->peer_name, st->sysop))
{
if (!run_args (NULL, w, filename, st, 1))
delete(filename);
}
else
Log (1, "srif_fill: error");
free (w);
q = parse_response (q, rsp, st->fa, config);
delete (srf);
delete (rsp);
}
else
Log (1, "mksrifpaths: error");
} else
add_to_rcvdlist (&st->rcvdlist, &st->n_rcvdlist, filename);
}
else
st->evt_queue = run_args(st->evt_queue, curr->command, filename, st, curr->imm);
}
}
return q;
}