INN commit: trunk/nnrpd (perl.c)
INN Commit
Russ_Allbery at isc.org
Fri Sep 26 19:45:47 UTC 2008
Date: Friday, September 26, 2008 @ 12:45:47
Author: iulius
Revision: 8050
Fix indentation.
Modified:
trunk/nnrpd/perl.c
--------+
perl.c | 423 ++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 216 insertions(+), 207 deletions(-)
Modified: perl.c
===================================================================
--- perl.c 2008-09-26 19:45:03 UTC (rev 8049)
+++ perl.c 2008-09-26 19:45:47 UTC (rev 8050)
@@ -3,7 +3,7 @@
** Embedded Perl support for INN.
**
** Originally written by Christophe Wolfhugel <wolf at pasteur.fr> (although
-** he wouldn't recongize it any more, so don't blame him) and modified,
+** 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.
**
@@ -33,7 +33,7 @@
#include "innperl.h"
-extern HEADER Table[], *EndOfTable;
+extern HEADER Table[], *EndOfTable;
extern char PERMuser[];
extern char **OtherHeaders;
@@ -42,7 +42,7 @@
extern bool PerlLoaded;
-/* #define DEBUG_MODIFY only if you want to see verbose outout */
+/* #define DEBUG_MODIFY only if you want to see verbose output. */
#ifdef DEBUG_MODIFY
static FILE *flog;
void dumpTable(char *msg);
@@ -51,149 +51,152 @@
char *
HandleHeaders(char *article)
{
- dSP;
- HEADER *hp;
- 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 };
+ dSP;
+ HEADER *hp;
+ 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 };
- if(!PerlLoaded) {
- loadPerl();
- }
+ if(!PerlLoaded) {
+ loadPerl();
+ }
- if (!PerlFilterActive)
- return NULL; /* not really necessary */
+ 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");
- }
+ if ((flog = fopen("/var/news/log/nnrpdperlerrror", "a+")) == NULL) {
+ syslog(L_ERROR,"Whoops. Can't open error log: %m");
+ }
#endif /* DEBUG_MODIFY */
- ENTER ;
- SAVETMPS ;
+ ENTER;
+ SAVETMPS;
- /* Create the Perl Hash */
- hdr = perl_get_hv("hdr", true);
- for (hp = Table; hp < EndOfTable; hp++) {
- if (hp->Body)
- hv_store(hdr, (char *) hp->Name, strlen(hp->Name), newSVpv(hp->Body, 0), 0);
- }
+ /* Create the Perl Hash. */
+ hdr = perl_get_hv("hdr", true);
+ for (hp = Table; hp < EndOfTable; hp++) {
+ if (hp->Body)
+ 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];
+ /* 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;
+ syslog(L_ERROR, "Null header number %d copying headers for Perl", i);
+ continue;
}
- s = strchr(p,':');
+ s = strchr(p, ':');
if (s == NULL) {
- syslog (L_ERROR,"Bad header copying headers for Perl: '%s'",p);
- continue;
+ syslog(L_ERROR, "Bad header copying headers for Perl: '%s'", p);
+ continue;
}
s++;
t = (*s == ' ' ? s + 1 : s);
hv_store(hdr, p, (s - p) - 1, newSVpv(t, 0), 0);
- }
- /* Store user */
- sv_setpv(perl_get_sv("user",true), PERMuser);
+ }
+ /* Store user. */
+ sv_setpv(perl_get_sv("user", true), PERMuser);
- /* Store body */
- body = perl_get_sv("body", true);
- sv_setpv(body, article);
+ /* Store body. */
+ body = perl_get_sv("body", true);
+ sv_setpv(body, article);
- /* Call the filtering function */
- rc = perl_call_argv("filter_post", G_EVAL|G_SCALAR, argv);
+ /* Call the filtering function. */
+ rc = perl_call_argv("filter_post", G_EVAL|G_SCALAR, argv);
- SPAGAIN;
+ SPAGAIN;
- /* Restore headers */
- modswitch = perl_get_sv("modify_headers",false);
- HeadersModified = false;
- if (SvTRUE(modswitch)) {
- HeadersModified = true;
- i = 0;
+ /* Restore headers. */
+ modswitch = perl_get_sv("modify_headers", false);
+ HeadersModified = false;
+ if (SvTRUE(modswitch)) {
+ HeadersModified = true;
+ i = 0;
#ifdef DEBUG_MODIFY
- dumpTable("Before mod");
+ dumpTable("Before mod");
#endif /* DEBUG_MODIFY */
- hv_iterinit(hdr);
- while ((scan = hv_iternext(hdr)) != NULL) {
- /* Get the values */
- p = HePV(scan, len);
- s = SvPV(HeVAL(scan), PL_na);
+ hv_iterinit(hdr);
+ while ((scan = hv_iternext(hdr)) != NULL) {
+ /* Get the values. */
+ p = HePV(scan, len);
+ s = SvPV(HeVAL(scan), PL_na);
#ifdef DEBUG_MODIFY
- fprintf(flog,"Hash iter: '%s','%s'\n",p,s);
+ fprintf(flog,"Hash iter: '%s','%s'\n", p, s);
#endif /* DEBUG_MODIFY */
- /* See if it's a table header */
- for (hp = Table; hp < EndOfTable; hp++) {
- if (strncasecmp(p, hp->Name, hp->Size) == 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;
+ /* See if it is a table header. */
+ for (hp = Table; hp < EndOfTable; hp++) {
+ if (strncasecmp(p, hp->Name, hp->Size) == 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 (i >= OtherSize - 1) {
- OtherSize += 20;
- OtherHeaders = xrealloc(OtherHeaders, OtherSize * sizeof(char *));
- }
- t = concat(p, ": ", s, (char *) 0);
- OtherHeaders[i++] = t;
- }
- OtherCount = i;
+ /* Add to other headers. */
+ 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");
+ dumpTable("After mod");
#endif /* DEBUG_MODIFY */
- }
+ }
- hv_undef (hdr);
- sv_setsv (body, &PL_sv_undef);
+ hv_undef(hdr);
+ sv_setsv(body, &PL_sv_undef);
- buf [0] = '\0' ;
+ buf[0] = '\0';
- if (SvTRUE(ERRSV)) /* check $@ */ {
- syslog (L_ERROR,"Perl function filter_post died: %s",
- SvPV(ERRSV, PL_na)) ;
- (void)POPs ;
- PerlFilter (false) ;
- } else if (rc == 1) {
- p = POPp;
- if (p != NULL && *p != '\0')
- strlcpy(buf, p, sizeof(buf));
- }
+ if (SvTRUE(ERRSV)) { /* Check $@. */
+ syslog(L_ERROR, "Perl function filter_post died: %s",
+ SvPV(ERRSV, PL_na));
+ (void)POPs;
+ PerlFilter(false);
+ } else if (rc == 1) {
+ p = POPp;
+ if (p != NULL && *p != '\0')
+ strlcpy(buf, p, sizeof(buf));
+ }
- FREETMPS ;
- LEAVE ;
+ FREETMPS;
+ LEAVE;
- if (buf[0] != '\0')
- return buf ;
- return NULL;
+ if (buf[0] != '\0')
+ return buf;
+ return NULL;
}
+
void
loadPerl(void)
{
@@ -206,98 +209,99 @@
PerlLoaded = true;
}
+
void
perlAccess(char *user, struct vector *access_vec)
{
- dSP;
- HV *attribs;
- SV *sv;
- int rc, i;
- char *key, *val, *buffer;
+ dSP;
+ HV *attribs;
+ SV *sv;
+ int rc, i;
+ char *key, *val, *buffer;
- if (!PerlFilterActive)
- return;
+ if (!PerlFilterActive)
+ return;
- ENTER;
- SAVETMPS;
+ ENTER;
+ SAVETMPS;
- attribs = perl_get_hv("attributes", true);
- hv_store(attribs, "hostname", 8, newSVpv(Client.host, 0), 0);
- hv_store(attribs, "ipaddress", 9, newSVpv(Client.ip, 0), 0);
- hv_store(attribs, "port", 4, newSViv(Client.port), 0);
- hv_store(attribs, "interface", 9, newSVpv(Client.serverhost, 0), 0);
- hv_store(attribs, "intipaddr", 9, newSVpv(Client.serverip, 0), 0);
- hv_store(attribs, "intport", 7, newSViv(Client.serverport), 0);
- hv_store(attribs, "username", 8, newSVpv(user, 0), 0);
+ attribs = perl_get_hv("attributes", true);
+ hv_store(attribs, "hostname", 8, newSVpv(Client.host, 0), 0);
+ hv_store(attribs, "ipaddress", 9, newSVpv(Client.ip, 0), 0);
+ hv_store(attribs, "port", 4, newSViv(Client.port), 0);
+ hv_store(attribs, "interface", 9, newSVpv(Client.serverhost, 0), 0);
+ hv_store(attribs, "intipaddr", 9, newSVpv(Client.serverip, 0), 0);
+ hv_store(attribs, "intport", 7, newSViv(Client.serverport), 0);
+ hv_store(attribs, "username", 8, newSVpv(user, 0), 0);
- PUSHMARK(SP);
+ PUSHMARK(SP);
- if (perl_get_cv("access", 0) == NULL) {
- syslog(L_ERROR, "Perl function access not defined");
- Reply("%d Internal Error (3). Goodbye\r\n", NNTP_ERR_ACCESS);
- ExitWithStats(1, true);
- }
+ if (perl_get_cv("access", 0) == NULL) {
+ syslog(L_ERROR, "Perl function access not defined");
+ Reply("%d Internal Error (3). Goodbye!\r\n", NNTP_ERR_ACCESS);
+ ExitWithStats(1, true);
+ }
- rc = perl_call_pv("access", G_EVAL|G_ARRAY);
+ rc = perl_call_pv("access", G_EVAL|G_ARRAY);
- SPAGAIN;
+ SPAGAIN;
- if (rc == 0 ) { /* Error occured, same as checking $@ */
- syslog(L_ERROR, "Perl function access died: %s",
- SvPV(ERRSV, PL_na));
- Reply("%d Internal Error (1). Goodbye\r\n", NNTP_ERR_ACCESS);
- ExitWithStats(1, true);
- }
+ if (rc == 0) { /* Error occured, same as checking $@. */
+ syslog(L_ERROR, "Perl function access died: %s",
+ SvPV(ERRSV, PL_na));
+ Reply("%d Internal Error (1). Goodbye!\r\n", NNTP_ERR_ACCESS);
+ 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_ERR_ACCESS);
- 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_ERR_ACCESS);
+ ExitWithStats(1, true);
+ }
- vector_resize(access_vec, (rc / 2));
+ vector_resize(access_vec, (rc / 2));
- buffer = xmalloc(BIG_BUFFER);
+ 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);
+ 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);
+ strlcpy(buffer, key, BIG_BUFFER);
+ strlcat(buffer, ": \"", BIG_BUFFER);
+ strlcat(buffer, val, BIG_BUFFER);
+ strlcat(buffer, "\"\n", BIG_BUFFER);
- vector_add(access_vec, xstrdup(buffer));
- }
+ vector_add(access_vec, xstrdup(buffer));
+ }
- free(buffer);
+ free(buffer);
- PUTBACK;
- FREETMPS;
- LEAVE;
-
+ PUTBACK;
+ FREETMPS;
+ LEAVE;
}
+
void
perlAuthInit(void)
{
dSP;
- int rc;
+ int rc;
if (!PerlFilterActive)
- return;
+ return;
ENTER;
SAVETMPS;
PUSHMARK(SP);
if (perl_get_cv("auth_init", 0) == NULL) {
- syslog(L_ERROR, "Perl function auth_init not defined");
- Reply("%d Internal Error (3). Goodbye\r\n", NNTP_ERR_ACCESS);
- ExitWithStats(1, true);
+ syslog(L_ERROR, "Perl function auth_init not defined");
+ Reply("%d Internal Error (3). Goodbye!\r\n", NNTP_ERR_ACCESS);
+ ExitWithStats(1, true);
}
rc = perl_call_pv("auth_init", G_EVAL|G_DISCARD);
@@ -305,15 +309,15 @@
SPAGAIN;
- 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_ERR_ACCESS);
- ExitWithStats(1, true);
+ 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_ERR_ACCESS);
+ ExitWithStats(1, true);
}
while (rc--) {
- (void)POPs;
+ (void)POPs;
}
PUTBACK;
@@ -321,21 +325,22 @@
LEAVE;
}
+
int
perlAuthenticate(char *user, char *passwd, char *errorstring, char *newUser)
{
dSP;
- HV *attribs;
- int rc;
- char *p;
- int code;
+ HV *attribs;
+ int rc;
+ char *p;
+ int code;
if (!PerlFilterActive)
return NNTP_ERR_ACCESS;
if (perl_get_cv("authenticate", 0) == NULL) {
syslog(L_ERROR, "Perl function authenticate not defined");
- Reply("%d Internal Error (3). Goodbye\r\n", NNTP_ERR_ACCESS);
+ Reply("%d Internal Error (3). Goodbye!\r\n", NNTP_ERR_ACCESS);
ExitWithStats(1, true);
}
@@ -356,25 +361,25 @@
SPAGAIN;
- if (rc == 0 ) { /* Error occured, same as checking $@ */
- syslog(L_ERROR, "Perl function authenticate died: %s",
- SvPV(ERRSV, PL_na));
- Reply("%d Internal Error (1). Goodbye\r\n", NNTP_ERR_ACCESS);
- ExitWithStats(1, false);
+ if (rc == 0 ) { /* Error occurred, same as checking $@. */
+ syslog(L_ERROR, "Perl function authenticate died: %s",
+ SvPV(ERRSV, PL_na));
+ Reply("%d Internal Error (1). Goodbye!\r\n", NNTP_ERR_ACCESS);
+ 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_ERR_ACCESS);
- ExitWithStats(1, false);
+ syslog(L_ERROR, "Perl function authenticate returned wrong number of results: %d", rc);
+ Reply("%d Internal Error (2). Goodbye!\r\n", NNTP_ERR_ACCESS);
+ 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. */
+ * 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(newUser, p, BIG_BUFFER);
}
p = POPp;
@@ -383,11 +388,11 @@
code = POPi;
if ((code == NNTP_OK_BANNER_POST) || (code == NNTP_OK_BANNER_NOPOST))
- code = (PERMcanpost || (PERMcanauthenticate && PERMcanpostgreeting)) ?
- NNTP_OK_BANNER_POST : NNTP_OK_BANNER_NOPOST;
+ code = (PERMcanpost || (PERMcanauthenticate && PERMcanpostgreeting)) ?
+ NNTP_OK_BANNER_POST : NNTP_OK_BANNER_NOPOST;
if (code == NNTP_FAIL_AUTH_NEEDED)
- PERMneedauth = true;
+ PERMneedauth = true;
hv_undef(attribs);
@@ -398,25 +403,29 @@
return code;
}
+
#ifdef DEBUG_MODIFY
void
dumpTable (char *msg)
{
- HEADER *hp;
- int i;
+ HEADER *hp;
+ int i;
- fprintf(flog,"===BEGIN TABLE DUMP: %s\n",msg);
+ 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 (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);
+ 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 */
More information about the inn-committers
mailing list