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 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
|
/* $Id: python.c 9024 2010-03-21 16:49:30Z iulius $
**
** Embed Python in the style of innd's Perl stuff.
**
** Written by G.J. Andruk <meowing@banet.net> patterned after Tcl/Perl work
** by Bob Heiney and Christophe Wolfhugel and a whole bunch of other people
** mentioned in the docs and sources for the other filters.
**
** The astute reader may notice the commission of blatant atrocities against
** Python's OO model here. Don't tell Guido.
**
** A quick note regarding Python exceptions: functions like
** PyObject_GetAttrString(PyObject *o, const char *attr_name)
** raise an exception when they fail, even though they return NULL.
** And as exceptions accumulate from caller to caller and so on,
** it generates weird issues with Python scripts afterwards. So such
** uses should be checked before. For instance with:
** PyObject_HasAttrString(PyObject *o, const char *attr_name).
*/
#include "config.h"
#include "clibrary.h"
#include "inn/innconf.h"
#include "inn/wire.h"
#include "innd.h"
#ifdef DO_PYTHON
/* Python redefines _POSIX_C_SOURCE, so undef it to suppress warnings. */
#undef _POSIX_C_SOURCE
#include "Python.h"
bool PythonFilterActive;
char *filterPath; /* This gets set in art.c. */
PyObject *PYFilterObject = NULL;
PyObject *PYFilterModule = NULL;
/* Article filter bits and pieces. */
PyObject *PYheaders = NULL;
PyObject **PYheaditem;
PyObject **PYheadkey;
PyObject *PYpathkey, *PYlineskey, *PYbodykey;
/* External functions. */
PyObject *msgid_method = NULL;
PyObject *art_method = NULL;
PyObject *mode_method = NULL;
PyObject *pre_reload_method = NULL;
PyObject *close_method = NULL;
/*
** Turn filtering on or off.
*/
void
PYfilter(bool value)
{
PythonFilterActive = value;
syslog(L_NOTICE, "%s Python filtering %s", LogName,
PythonFilterActive ? "enabled" : "disabled");
}
/*
** Front end for PYfilter().
*/
const char *
PYcontrol(char **av)
{
switch (av[0][0]) {
default:
return "1 Bad flag";
case 'y':
if (PythonFilterActive)
return "1 Python filter already enabled";
else if (PYFilterObject == NULL)
return "1 Python filter not defined" ;
PYfilter(true);
break;
case 'n':
if (!PythonFilterActive)
return "1 Python filter already disabled";
PYfilter(false);
break;
}
return NULL;
}
/*
** Reject articles we don't like.
*/
char *
PYartfilter(const ARTDATA *data, char *artBody, long artLen, int lines)
{
const ARTHEADER *hp;
const HDRCONTENT *hc = data->HdrContent;
int hdrnum;
int i;
static char buf[256];
PyObject *result;
if (!PythonFilterActive || PYFilterObject == NULL || art_method == NULL)
return NULL;
/* Add headers to the dictionary... */
hdrnum = 0;
for (i = 0 ; i < MAX_ARTHEADER ; i++) {
if (HDR_FOUND(i)) {
hp = &ARTheaders[i];
PYheaditem[hdrnum] = PyBuffer_FromMemory(HDR(i), HDR_LEN(i));
} else
PYheaditem[hdrnum] = Py_None;
PyDict_SetItem(PYheaders, PYheadkey[hdrnum], PYheaditem[hdrnum]);
hdrnum++;
}
/* ...then the body... */
if (artLen && artBody != NULL)
PYheaditem[hdrnum] = PyBuffer_FromMemory(artBody, --artLen);
else
PYheaditem[hdrnum] = Py_None;
PyDict_SetItem(PYheaders, PYbodykey, PYheaditem[hdrnum++]);
/* ...and finally, the line count. */
PYheaditem[hdrnum] = PyInt_FromLong((long) lines);
PyDict_SetItem(PYheaders, PYlineskey, PYheaditem[hdrnum++]);
/* Now see if the filter likes it. */
result = PyObject_CallFunction(art_method, (char *) "O", PYheaders);
if ((result != NULL) && PyObject_IsTrue(result))
strlcpy(buf, PyString_AS_STRING(result), sizeof(buf));
else
*buf = '\0';
Py_XDECREF(result);
/* Clean up after ourselves. */
PyDict_Clear(PYheaders);
for (i = 0; i < hdrnum; i++)
if (PYheaditem[i] != Py_None) {
Py_DECREF(PYheaditem[i]);
}
if (*buf != '\0')
return buf;
return NULL;
}
/*
** Refuse message-IDs offered through CHECK, IHAVE or TAKETHIS that
** we don't like.
*/
char *
PYmidfilter(char *messageID, int msglen)
{
static char buf[256];
PyObject *result;
if (!PythonFilterActive || PYFilterObject == NULL || msgid_method == NULL)
return NULL;
result = PyObject_CallFunction(msgid_method, (char *) "s#", messageID,
msglen);
if ((result != NULL) && PyObject_IsTrue(result))
strlcpy(buf, PyString_AS_STRING(result), sizeof(buf));
else
*buf = '\0';
Py_XDECREF(result);
if (*buf != '\0')
return buf;
return NULL;
}
/*
** Tell the external module about innd's state.
*/
void
PYmode(Mode, NewMode, reason)
OPERATINGMODE Mode, NewMode;
char *reason;
{
PyObject *result;
char oldmode[10], newmode[10];
if (!PythonFilterActive || PYFilterObject == NULL || mode_method == NULL)
return;
switch (Mode) {
default: strlcpy(oldmode, "unknown", 10); break;
case OMrunning: strlcpy(oldmode, "running", 10); break;
case OMpaused: strlcpy(oldmode, "paused", 10); break;
case OMthrottled: strlcpy(oldmode, "throttled", 10); break;
case OMshutdown: strlcpy(oldmode, "shutdown", 10); break;
}
switch (NewMode) {
default: strlcpy(newmode, "unknown", 10); break;
case OMrunning: strlcpy(newmode, "running", 10); break;
case OMpaused: strlcpy(newmode, "paused", 10); break;
case OMthrottled: strlcpy(newmode, "throttled", 10); break;
case OMshutdown: strlcpy(newmode, "shutdown", 10); break;
}
result = PyObject_CallFunction(mode_method, (char *) "sss",
oldmode, newmode, reason);
Py_XDECREF(result);
}
/*
** Called by the external module so it can register itself with innd.
*/
static PyObject *
PY_set_filter_hook(PyObject *dummy UNUSED, PyObject *args)
{
PyObject *result = NULL;
PyObject *temp;
/* set_filter_hook method should return a pointer to innd auth object. */
if (PyArg_ParseTuple(args, (char *) "O:set_filter_hook", &temp)) {
Py_XINCREF(temp);
Py_XDECREF(PYFilterObject);
PYFilterObject = temp;
Py_INCREF(Py_None);
result = Py_None;
}
/* Return a pointer to innd auth method. */
return result;
}
/*
** Allow external module to ask innd if an ID is in history.
*/
static PyObject *
PY_havehist(PyObject *self UNUSED, PyObject *args)
{
char *msgid;
int msgidlen;
if (!PyArg_ParseTuple(args, (char *) "s#", &msgid, &msgidlen))
return NULL;
if (HIScheck(History, msgid))
return PyInt_FromLong(1);
return PyInt_FromLong(0);
}
/*
** Allow external module to locally delete an article.
*/
static PyObject *
PY_cancel(PyObject *self UNUSED, PyObject *args)
{
char *msgid;
int msgidlen;
char *parambuf[2];
if (!PyArg_ParseTuple(args, (char *) "s#", &msgid, &msgidlen))
return NULL;
parambuf[0]= msgid;
parambuf[1]= 0;
if (!CCcancel(parambuf))
return PyInt_FromLong(1);
return PyInt_FromLong(0);
}
/*
** Stuff an ID into history so that it will be refused later.
*/
static PyObject *
PY_addhist(PyObject *self UNUSED, PyObject *args)
{
char *msgid;
int msgidlen;
char *articlepaths = (char *) "";
char tbuff[12];
char *parambuf[6];
if (!PyArg_ParseTuple(args, (char *) "s#", &msgid, &msgidlen))
return NULL;
snprintf(tbuff, sizeof(tbuff), "%lu", (unsigned long) time(NULL));
parambuf[0] = msgid;
parambuf[1] = parambuf[2] = parambuf[3] = tbuff;
parambuf[4] = articlepaths;
parambuf[5] = 0;
if (!CCaddhist(parambuf))
return PyInt_FromLong(1);
return PyInt_FromLong(0);
}
/*
** Get a newsgroup's status flag (j, m, n, x, y, =other.group).
*/
static PyObject *
PY_newsgroup(PyObject *self UNUSED, PyObject *args)
{
char *newsgroup;
int nglen;
NEWSGROUP *ngp;
char *end;
int size;
if (!PyArg_ParseTuple(args, (char *) "s#", &newsgroup, &nglen))
return NULL;
ngp = NGfind(newsgroup);
if (ngp == NULL)
return PyString_FromStringAndSize(NULL, 0);
/* ngp->Rest is newline-terminated; find the end. */
end = strchr(ngp->Rest, '\n');
if (end == NULL)
size = strlen(ngp->Rest);
else
size = end - ngp->Rest;
/* If an alias is longer than this, active is probably broken. */
if (size > MAXHEADERSIZE) {
syslog(L_ERROR, "too-long flag field in active for %s", newsgroup);
size = MAXHEADERSIZE;
}
return PyString_FromStringAndSize(ngp->Rest, size);
}
/*
** Return an article header to the external module as a string. We
** don't use a buffer object here because that would make it harder,
** for example, to compare two on-spool articles.
*/
static PyObject *
PY_head(PyObject *self UNUSED, PyObject *args)
{
char *msgid;
int msgidlen;
char *p;
TOKEN token;
ARTHANDLE *art;
PyObject *header;
size_t headerlen;
if (!PyArg_ParseTuple(args, (char *) "s#", &msgid, &msgidlen))
return NULL;
if (! HISlookup(History, msgid, NULL, NULL, NULL, &token))
return Py_BuildValue((char *) "s", "");
if ((art = SMretrieve(token, RETR_HEAD)) == NULL)
return Py_BuildValue((char *) "s", "");
p = wire_to_native(art->data, art->len, &headerlen);
SMfreearticle(art);
header = PyString_FromStringAndSize(p, headerlen);
free(p);
return header;
}
/*
** Return a whole article to the external module as a string.
*/
static PyObject *
PY_article(PyObject *self UNUSED, PyObject *args)
{
char *msgid;
int msgidlen;
char *p;
TOKEN token;
ARTHANDLE *arth;
PyObject *art;
size_t artlen;
if (!PyArg_ParseTuple(args, (char *) "s#", &msgid, &msgidlen))
return NULL;
if (! HISlookup(History, msgid, NULL, NULL, NULL, &token))
return Py_BuildValue((char *) "s", "");
if ((arth = SMretrieve(token, RETR_ALL)) == NULL)
return Py_BuildValue((char *) "s", "");
p = wire_to_native(arth->data, arth->len, &artlen);
SMfreearticle(arth);
art = PyString_FromStringAndSize(p, artlen);
free(p);
return art;
}
/*
** Python's syslog module isn't compiled in by default. It's easier
** to do it this way, and the switch block looks pretty in a color
** editor).
*/
static PyObject *
PY_syslog(PyObject *self UNUSED, PyObject *args)
{
char *loglevel;
int levellen;
char *logmsg;
int msglen;
int priority;
/* Get loglevel and message. */
if (!PyArg_ParseTuple(args, (char *) "s#s#",
&loglevel, &levellen, &logmsg, &msglen))
return NULL;
/* Assign syslog priority by abbreviated names. */
switch (*loglevel) {
default: priority = LOG_NOTICE ;
case 'd': case 'D': priority = LOG_DEBUG ; break;
case 'i': case 'I': priority = LOG_INFO ; break;
case 'n': case 'N': priority = LOG_NOTICE ; break;
case 'w': case 'W': priority = LOG_WARNING ; break;
case 'e': case 'E': priority = LOG_ERR ; break;
case 'c': case 'C': priority = LOG_CRIT ; break;
case 'a': case 'A': priority = LOG_ALERT ; break;
}
/* Log the message. */
syslog(priority, "python: %s", logmsg);
/* Return None. */
Py_INCREF(Py_None);
return Py_None;
}
/*
** Compute a hash digest for a string.
*/
static PyObject *
PY_hashstring(PyObject *self UNUSED, PyObject *args)
{
char *instring, *wpos, *p, *q;
char *workstring = NULL;
int insize, worksize, newsize, i, wasspace;
int lines = 0;
HASH myhash;
if (!PyArg_ParseTuple(args, (char *) "s#|i", &instring, &insize, &lines))
return NULL;
/* If a linecount is provided, munge before hashing. */
if (lines > 0) {
worksize = insize;
/* Chop leading whitespace. */
for (p=instring ; worksize>0 && isspace((unsigned char) *p) ; p++) {
if (*p == '\n')
lines--;
worksize--;
}
wpos = p;
/* And trailing. */
for (p=&wpos[worksize] ; worksize>0 && isspace((unsigned char) *p) ; p--) {
if (*p == '\n')
lines--;
worksize--;
}
/* Chop last 3 lines if we have >= 5. From above chop the
* last line which has no CR so we use 1 less here. */
if (lines >= 4) {
for (i=0, p=wpos+worksize ; i<2 ; p--)
if (*p == '\n')
i++;
worksize = p - wpos;
}
/* Compress out multiple whitespace in the trimmed string. We
* do a copy because this is probably an original art
* buffer. */
workstring = memcpy(xmalloc(worksize), wpos, worksize);
newsize = wasspace = 0;
p = wpos;
q = workstring;
for (i=0 ; i<worksize ; i++) {
if (isspace((unsigned char) *p)) {
if (!wasspace)
*q++ = ' ';
wasspace = 1;
}
else {
*q++ = tolower(*p);
wasspace = 0;
}
p++;
}
worksize = q - workstring;
myhash = Hash(workstring, worksize);
free(workstring);
}
else
myhash = Hash(instring, insize);
return PyString_FromStringAndSize((const char *)&myhash, sizeof(myhash));
}
/*
** Make the internal INN module's functions visible to Python. Python
** annoyingly doesn't use const where appropriate in its structure
** definitions, so we have to add casts for all of the string parameters that
** we're initializing with constant strings.
*/
#define METHOD(name, func, flags, help) \
{ (char *)(name), (func), (flags), (char *)(help) }
static PyMethodDef INNPyMethods[] = {
METHOD("set_filter_hook", PY_set_filter_hook, METH_VARARGS, ""),
METHOD("havehist", PY_havehist, METH_VARARGS, ""),
METHOD("addhist", PY_addhist, METH_VARARGS, ""),
METHOD("cancel", PY_cancel, METH_VARARGS, ""),
METHOD("newsgroup", PY_newsgroup, METH_VARARGS, ""),
METHOD("head", PY_head, METH_VARARGS, ""),
METHOD("article", PY_article, METH_VARARGS, ""),
METHOD("syslog", PY_syslog, METH_VARARGS, ""),
METHOD("hashstring", PY_hashstring, METH_VARARGS, ""),
METHOD(NULL, NULL, 0, "")
};
/*
** This runs when innd shuts down.
*/
void
PYclose(void)
{
PyObject *result;
if (close_method != NULL) {
result = PyObject_CallFunction(close_method, NULL);
Py_XDECREF(result);
}
}
/*
** Check that a method exists and is callable. Set a pointer to
** the corresponding PyObject, or NULL if not found.
*/
static void
PYdefonemethod(PyObject **methptr, const char *methname)
{
Py_XDECREF(*methptr);
/* We check with HasAttrString() the existence of the method because
* otherwise, in case it does not exist, an exception is raised by Python,
* although the result of the function is NULL. */
if (PyObject_HasAttrString(PYFilterObject, (char *) methname) == 1) {
/* Get a pointer to given method. */
*methptr = PyObject_GetAttrString(PYFilterObject, (char *) methname);
} else {
*methptr = NULL;
}
/* See if such method is defined. */
if (*methptr == NULL)
syslog(L_NOTICE, "python method %s not found", methname);
else {
/* See if it is callable. */
if (PyCallable_Check(*methptr) == 0) {
syslog(L_ERROR, "python object %s found but not a function", methname);
Py_DECREF(*methptr);
*methptr = NULL;
}
}
}
/*
** Look up the filter methods, so we will know what's available when
** innd wants to call them.
*/
static void
PYdefmethods(void)
{
PYdefonemethod(&msgid_method, "filter_messageid");
PYdefonemethod(&art_method, "filter_art");
PYdefonemethod(&mode_method, "filter_mode");
PYdefonemethod(&pre_reload_method, "filter_before_reload");
PYdefonemethod(&close_method, "filter_close");
}
/*
** Used by "ctlinnd reload filter.python 'reason'".
*/
int
PYreadfilter(void)
{
PyObject *newmodule = NULL;
PyObject *result;
if (!Py_IsInitialized()) {
syslog(L_ERROR, "python is not initialized");
return 0;
}
/* If there is a filter running, let it clean up first. */
if (pre_reload_method != NULL) {
result = PyObject_CallFunction(pre_reload_method, NULL);
Py_XDECREF(result);
}
/* We need to reimport the module before reloading it because otherwise,
* it might not be taken into account by Python.
* See Python API documentation:
* If a module is syntactically correct but its initialization fails,
* the first import statement for it does not bind its name locally,
* but does store a (partially initialized) module object in
* sys.modules. To reload the module, you must first import it again
* (this will bind the name to the partially initialized module object)
* before you can reload() it.
*/
PYFilterModule = PyImport_ImportModule((char *) INN_PATH_PYTHON_STARTUP_M);
if (PYFilterModule == NULL) {
syslog(L_ERROR, "failed to reimport external python module");
}
if ((newmodule = PyImport_ReloadModule(PYFilterModule)) == NULL) {
syslog(L_ERROR, "cant reload python filter module");
PYfilter(false);
return 0;
}
Py_XDECREF(PYFilterModule);
PYFilterModule = newmodule;
if (PYFilterObject == NULL) {
syslog(L_ERROR, "python reload error, filter object not defined");
PYfilter(false);
return 0;
}
PYfilter(true);
PYdefmethods();
return 1;
}
/*
** Called when innd first starts -- this gets the filters hooked in.
*/
void
PYsetup(void)
{
const ARTHEADER *hp;
size_t hdrcount;
/* Add path for nnrpd module. The environment variable PYTHONPATH
* does it; one can also append innconf->pathfilter to sys.path once
* Python has been initialized. */
setenv("PYTHONPATH", innconf->pathfilter, 1);
/* Load up the interpreter ;-O */
Py_Initialize();
/* It makes Python sad when its stdout or stderr are closed. */
if ((fileno(stdout) == -1) || (fileno(stderr) == -1))
PyRun_SimpleString
("import sys; sys.stdout=sys.stderr=open('/dev/null', 'a')");
/* See if Python initialized OK. */
if (!Py_IsInitialized ()) {
syslog(L_ERROR, "python interpreter NOT initialized");
return;
}
/* Build a module interface to certain INN functions. */
Py_InitModule((char *) "INN", INNPyMethods);
PYFilterModule = PyImport_ImportModule((char *) INN_PATH_PYTHON_STARTUP_M);
if (PYFilterModule == NULL)
syslog(L_ERROR, "failed to import external python module");
if (PYFilterObject == NULL) {
syslog(L_ERROR, "python filter object is not defined");
PYfilter(false);
} else {
PYfilter(true);
PYdefmethods();
syslog(L_NOTICE, "defined python methods");
}
/* Grab space for these so we aren't forever recreating them. We also
* cut the body and the line count into PYheaditem, so it needs to be
* two elements longer than the total number of headers. */
PYheaders = PyDict_New();
hdrcount = ARRAY_END(ARTheaders) - ARTheaders;
PYheaditem = xmalloc((hdrcount + 2) * sizeof(PyObject *));
PYheadkey = xmalloc(hdrcount * sizeof(PyObject *));
/* Preallocate keys for the article dictionary */
for (hp = ARTheaders; hp < ARRAY_END(ARTheaders); hp++)
PYheadkey[hp - ARTheaders] = PyString_InternFromString(hp->Name);
PYpathkey = PyString_InternFromString("Path");
PYlineskey = PyString_InternFromString("__LINES__");
PYbodykey = PyString_InternFromString("__BODY__");
syslog(L_NOTICE, "python interpreter initialized OK");
}
#endif /* defined(DO_PYTHON) */
|