File: perl.c

package info (click to toggle)
inn2 2.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,228 kB
  • sloc: ansic: 96,526; sh: 15,562; perl: 13,281; makefile: 3,700; yacc: 842; python: 309; lex: 262
file content (461 lines) | stat: -rw-r--r-- 12,088 bytes parent folder | download | duplicates (3)
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*  $Id: perl.c 10305 2018-12-02 14:21:56Z iulius $
**
**  Embedded Perl support for INN.
**
**  Originally written by Christophe Wolfhugel <wolf@pasteur.fr> (although
**  he wouldn't recognize it any more, so don't blame him) and modified,
**  expanded, and tweaked by James Brister, Dave Hayes, Andrew Gierth, and
**  Russ Allbery among others.
**
**  This file should contain all innd-specific Perl linkage.  Linkage
**  applicable to both innd and nnrpd should go into lib/perl.c instead.
**
**  We are assuming Perl 5.004 or later.
*/

#include "config.h"
#include "clibrary.h"

#include "inn/innconf.h"
#include "nnrpd.h"
#include "inn/nntp.h"
#include "inn/paths.h"
#include "post.h"

/* Skip this entire file if DO_PERL (./configure --with-perl) isn't set. */
#ifdef DO_PERL

#include <EXTERN.h>
#pragma GCC diagnostic ignored "-Wredundant-decls"
#include <perl.h>
#pragma GCC diagnostic warning "-Wredundant-decls"
#include <XSUB.h>
#include "ppport.h"

#include "innperl.h"

extern HEADER Table[], *EndOfTable;

extern char **OtherHeaders;
extern int OtherCount;
extern bool HeadersModified;

extern bool PerlLoaded;

/* #define DEBUG_MODIFY only if you want to see verbose output. */
#ifdef DEBUG_MODIFY
static FILE *flog;
void dumpTable(const char *msg);
#endif /* DEBUG_MODIFY */

char *
HandleHeaders(char *article)
{
    dSP;
    HEADER *hp;
    HV *attribs;
    HV *hdr;
    SV *body;
    int rc;
    char *p, *q;
    static char buf[256];
    int i;
    size_t len;
    char *s,*t;
    HE *scan;
    SV *modswitch;
    int OtherSize;
    char *argv[] = { NULL };
    bool failure;
    SV *errsv;

    if(!PerlLoaded) {
        loadPerl();
    }

    if (!PerlFilterActive)
        return NULL; /* Not really necessary. */

#ifdef DEBUG_MODIFY
    if ((flog = fopen("/var/news/log/nnrpdperlerrror", "a+")) == NULL) {
        syslog(L_ERROR,"Whoops.  Can't open error log: %m");
    }
#endif /* DEBUG_MODIFY */

    ENTER;
    SAVETMPS;

    /* Create the Perl attributes hash. */
    attribs = perl_get_hv("attributes", true);
    (void) hv_store(attribs, "hostname", 8, newSVpv(Client.host, 0), 0);
    (void) hv_store(attribs, "ipaddress", 9, newSVpv(Client.ip, 0), 0);
    (void) hv_store(attribs, "port", 4, newSViv(Client.port), 0);
    (void) hv_store(attribs, "interface", 9, newSVpv(Client.serverhost, 0), 0);
    (void) hv_store(attribs, "intipaddr", 9, newSVpv(Client.serverip, 0), 0);
    (void) hv_store(attribs, "intport", 7, newSViv(Client.serverport), 0);

    /* Create the Perl header hash. */
    hdr = perl_get_hv("hdr", true);
    for (hp = Table; hp < EndOfTable; hp++) {
        if (hp->Body)
            (void) hv_store(hdr, (char *) hp->Name, strlen(hp->Name),
                     newSVpv(hp->Body, 0), 0);
    }

    /* Also store other headers. */
    OtherSize = OtherCount;
    for (i = 0; i < OtherCount; i++) {
        p = OtherHeaders[i];
        if (p == NULL) {
            syslog(L_ERROR, "Null header number %d copying headers for Perl", i);
            continue;
        }
        s = strchr(p, ':');
        if (s == NULL) {
            syslog(L_ERROR, "Bad header copying headers for Perl: '%s'", p);
            continue;
        }
        s++;
        t = (*s == ' ' ? s + 1 : s);
        (void) hv_store(hdr, p, (s - p) - 1, newSVpv(t, 0), 0);
    }

    /* Store user. */
    sv_setpv(perl_get_sv("user", true), PERMuser);

    /* Store body. */
    body = perl_get_sv("body", true);
    sv_setpv(body, article);

    /* Call the filtering function. */
    /* No need for PUSHMARK(SP) with call_argv(). */
    rc = perl_call_argv("filter_post", G_EVAL|G_SCALAR, argv);

    SPAGAIN;

    /* Restore headers if they have just been modified by the filter. */
    modswitch = perl_get_sv("modify_headers", false);
    HeadersModified = false;
    if (SvTRUE(modswitch)) {
        HeadersModified = true;
        i = 0;

#ifdef DEBUG_MODIFY
        dumpTable("Before mod");
#endif /* DEBUG_MODIFY */

        hv_iterinit(hdr);
        while ((scan = hv_iternext(hdr)) != NULL) {
            /* Get the values.  We replace the known ones with these
             * new values. */
            p = HePV(scan, len);
            s = SvPV(HeVAL(scan), PL_na);
#ifdef DEBUG_MODIFY
            fprintf(flog,"Hash iter: '%s','%s'\n", p, s);
#endif /* DEBUG_MODIFY */

            /* See if it is a table header. */
            for (hp = Table; hp < EndOfTable; hp++) {
                if (strcasecmp(p, hp->Name) == 0) {
                    char *copy = xstrdup(s);
                    HDR_SET(hp - Table, copy);
                    hp->Len = TrimSpaces(hp->Value);
                    for (q = hp->Value ; ISWHITE(*q) || *q == '\n' ; q++)
                        continue;
                    hp->Body = q;
                    if (hp->Len == 0) {
                        free(hp->Value);
                        hp->Value = hp->Body = NULL;
                    }
                    break;
                }
            }
            if (hp != EndOfTable)
                continue;

            /* Add to other headers if not empty. */
            if (TrimSpaces(s) > 0) {
                if (i >= OtherSize - 1) {
                    OtherSize += 20;
                    OtherHeaders = xrealloc(OtherHeaders, OtherSize * sizeof(char *));
                }
                t = concat(p, ": ", s, (char *) 0);
                OtherHeaders[i++] = t;
            }
        }
        OtherCount = i;
#ifdef DEBUG_MODIFY
        dumpTable("After mod");
#endif /* DEBUG_MODIFY */
    }

    hv_undef(attribs);
    hv_undef(hdr);
    sv_setsv(body, &PL_sv_undef);

    buf[0] = '\0';

    /* Check $@. */
    errsv = ERRSV;
    if (SvTRUE(errsv)) {
        failure = true;
        syslog(L_ERROR, "Perl function filter_post died: %s",
               SvPV(errsv, PL_na));
        (void)POPs;
    } else {
        failure = false;
        if (rc == 1) {
            p = POPp;
            if (p != NULL && *p != '\0')
                strlcpy(buf, p, sizeof(buf));
        }
    }

    PUTBACK;
    FREETMPS;
    LEAVE;

    if (failure)
        PerlFilter(false);

    if (buf[0] != '\0')
        return buf;

#ifdef DEBUG_MODIFY
    fclose(flog);
#endif /* DEBUG_MODIFY */

    return NULL;
}


void
loadPerl(void)
{
    char *path;

    path = concatpath(innconf->pathfilter, INN_PATH_PERL_FILTER_NNRPD);
    PERLsetup(NULL, path, "filter_post");
    free(path);
    PerlFilter(true);
    PerlLoaded = true;
}


void
perlAccess(char *user, struct vector *access_vec)
{
    dSP;
    HV *attribs;
    SV *sv;
    int rc, i;
    char *key, *val, *buffer;
    SV *errsv;

    if (!PerlFilterActive)
        return;

    ENTER;
    SAVETMPS;

    attribs = perl_get_hv("attributes", true);
    (void) hv_store(attribs, "hostname", 8, newSVpv(Client.host, 0), 0);
    (void) hv_store(attribs, "ipaddress", 9, newSVpv(Client.ip, 0), 0);
    (void) hv_store(attribs, "port", 4, newSViv(Client.port), 0);
    (void) hv_store(attribs, "interface", 9, newSVpv(Client.serverhost, 0), 0);
    (void) hv_store(attribs, "intipaddr", 9, newSVpv(Client.serverip, 0), 0);
    (void) hv_store(attribs, "intport", 7, newSViv(Client.serverport), 0);
    (void) hv_store(attribs, "username", 8, newSVpv(user, 0), 0);

    PUSHMARK(SP);
    PUTBACK;

    if (perl_get_cv("access", 0) == NULL) {
        syslog(L_ERROR, "Perl function access not defined");
        Reply("%d Internal error (3).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
        ExitWithStats(1, true);
    }

    rc = perl_call_pv("access", G_EVAL|G_ARRAY);

    SPAGAIN;

    if (rc == 0) { /* Error occured, same as checking $@. */
        errsv = ERRSV;
        syslog(L_ERROR, "Perl function access died: %s",
               SvPV(errsv, PL_na));
        Reply("%d Internal error (1).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
        ExitWithStats(1, true);
    }

    if ((rc % 2) != 0) {
        syslog(L_ERROR, "Perl function access returned an odd number of arguments: %i", rc);
        Reply("%d Internal error (2).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
        ExitWithStats(1, true);
    }

    vector_resize(access_vec, (rc / 2));

    buffer = xmalloc(BIG_BUFFER);

    for (i = (rc / 2); i >= 1; i--) {
        sv = POPs;
        val = SvPV(sv, PL_na);
        sv = POPs;
        key = SvPV(sv, PL_na);

        strlcpy(buffer, key, BIG_BUFFER);
        strlcat(buffer, ": \"", BIG_BUFFER);
        strlcat(buffer, val, BIG_BUFFER);
        strlcat(buffer, "\"\n", BIG_BUFFER);

        vector_add(access_vec, buffer);
    }

    free(buffer);
    hv_undef(attribs);

    PUTBACK;
    FREETMPS;
    LEAVE;
}


void
perlAuthInit(void)
{
    dSP;
    int rc;
    SV *errsv;

    if (!PerlFilterActive)
        return;

    if (perl_get_cv("auth_init", 0) == NULL) {
        return;
    }

    ENTER;
    SAVETMPS;
    PUSHMARK(SP);
    PUTBACK;

    rc = perl_call_pv("auth_init", G_EVAL|G_DISCARD);

    SPAGAIN;

    errsv = ERRSV;
    if (SvTRUE(errsv)) {    /* Check $@. */
        syslog(L_ERROR, "Perl function authenticate died: %s",
               SvPV(errsv, PL_na));
        Reply("%d Internal error (1).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
        ExitWithStats(1, true);
    }

    while (rc--) {
        (void)POPs;
    }

    PUTBACK;
    FREETMPS;
    LEAVE;
}


void
perlAuthenticate(char *user, char *passwd, int *code, char *errorstring, char *newUser)
{
    dSP;
    HV *attribs;
    int rc;
    char *p;
    SV *errsv;

    if (!PerlFilterActive)
        *code = NNTP_FAIL_AUTHINFO_BAD;

    if (perl_get_cv("authenticate", 0) == NULL) {
        syslog(L_ERROR, "Perl function authenticate not defined");
        Reply("%d Internal error (3).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
        ExitWithStats(1, true);
    }

    ENTER;
    SAVETMPS;
    attribs = perl_get_hv("attributes", true);
    (void) hv_store(attribs, "hostname", 8, newSVpv(Client.host, 0), 0);
    (void) hv_store(attribs, "ipaddress", 9, newSVpv(Client.ip, 0), 0);
    (void) hv_store(attribs, "port", 4, newSViv(Client.port), 0);
    (void) hv_store(attribs, "interface", 9, newSVpv(Client.serverhost, 0), 0);
    (void) hv_store(attribs, "intipaddr", 9, newSVpv(Client.serverip, 0), 0);
    (void) hv_store(attribs, "intport", 7, newSViv(Client.serverport), 0);
    (void) hv_store(attribs, "username", 8, newSVpv(user, 0), 0);
    (void) hv_store(attribs, "password", 8, newSVpv(passwd, 0), 0);

    PUSHMARK(SP);
    PUTBACK;
    rc = perl_call_pv("authenticate", G_EVAL|G_ARRAY);

    SPAGAIN;

    if (rc == 0 ) { /* Error occurred, same as checking $@. */
        errsv = ERRSV;
        syslog(L_ERROR, "Perl function authenticate died: %s",
               SvPV(errsv, PL_na));
        Reply("%d Internal error (1).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
        ExitWithStats(1, false);
    }

    if ((rc != 3) && (rc != 2)) {
        syslog(L_ERROR, "Perl function authenticate returned wrong number of results: %d", rc);
        Reply("%d Internal error (2).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
        ExitWithStats(1, false);
    }

    /* FIXME: Change the structure of the code so that we don't have to rely
     * on keeping these sizes in sync with the buffers that are passed into
     * this function. */
    if (rc == 3) {
        p = POPp;
        strlcpy(newUser, p, BIG_BUFFER);
    }

    p = POPp;
    strlcpy(errorstring, p, BIG_BUFFER);

    *code = POPi;

    hv_undef(attribs);

    PUTBACK;
    FREETMPS;
    LEAVE;
}


#ifdef DEBUG_MODIFY
void
dumpTable (const char *msg)
{
    HEADER *hp;
    int i;

    fprintf(flog, "===BEGIN TABLE DUMP: %s\n", msg);

    for (hp = Table; hp < EndOfTable; hp++) {
        fprintf(flog, " Name: '%s'",hp->Name);
        fflush(flog);
        fprintf(flog, " Size: '%d'", hp->Size);
        fflush(flog);
        fprintf(flog, " Value: '%s'\n", ((hp->Value == NULL) ? "(NULL)" : hp->Value));
        fflush(flog);
    }

    for (i=0; i<OtherCount; i++) {
        fprintf(flog, "Extra[%02d]: %s\n", i, OtherHeaders[i]);
    }
    fprintf(flog, "===END TABLE DUMP: %s\n", msg);
}
#endif /* DEBUG_MODIFY */

#endif /* DO_PERL */