File: python.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 (901 lines) | stat: -rw-r--r-- 27,599 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
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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
/*  $Id: python.c 10313 2018-12-17 08:46:18Z iulius $
**
**  python.c:  Embed Python in the style of nnrpd's Perl stuff
**             (authentication, authorization and dynamic hooks
**             only at this point).
**
**  Written by Ilya Etingof <ilya@glas.net>, 1999.
**
**  This code bases on Python work for innd filtering done by
**  G.J. Andruk <meowing@banet.net>.  Also it borrows some ideas from
**  TCL/Perl work done by Bob Heiney and Christophe Wolfhugel.
**
**  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"

#ifdef DO_PYTHON

/*  Python redefines _POSIX_C_SOURCE, so undef it to suppress warnings. */
#undef _POSIX_C_SOURCE

/*  Make "s#" use Py_ssize_t rather than int. */
#define PY_SSIZE_T_CLEAN

/*  Python.h must be included after having defined PY_SSIZE_T_CLEAN,
 *  and before any standard headers are included (because Python may
 *  define some pre-processor definitions which affect the standard
 *  headers on some systems). */
#pragma GCC diagnostic ignored "-Wredundant-decls"
#include "Python.h"
#pragma GCC diagnostic warning "-Wredundant-decls"

/*  Define Py_ssize_t when it does not exist (Python < 2.5.0). */
#if PY_VERSION_HEX < 0x02050000
  typedef int Py_ssize_t;
#endif

#if PY_MAJOR_VERSION >= 3
# define PyInt_AS_LONG PyLong_AS_LONG
# define PyInt_Check PyLong_Check
# define PyInt_FromLong PyLong_FromLong
# define PyString_AS_STRING PyUnicode_AsUTF8
# define PyString_AsString PyUnicode_AsUTF8
# define PyString_Check PyUnicode_Check
# define PYBUFF_FROMMEMORY(str, len) \
      PyMemoryView_FromMemory((str), (len), PyBUF_WRITE)
#else
# define PYBUFF_FROMMEMORY(str, len) \
      PyBuffer_FromMemory((str), (len))
#endif

#include "clibrary.h"

#include "inn/innconf.h"
#include "nnrpd.h"
#include "inn/hashtab.h"

/*  Values relate name of hook to array index. */
#define PYTHONauthen           1
#define PYTHONaccess           2
#define PYTHONdynamic          3

#define PYTHONtypes_max        4

/*  Values relate type of method to array index. */
#define PYTHONmain             1
#define PYTHONinit             2
#define PYTHONclose            3

#define PYTHONmethods_max      4

/*  Key names for attributes dictionary. */
#define PYTHONhostname         "hostname"
#define PYTHONipaddress        "ipaddress"
#define PYTHONport             "port"
#define PYTHONinterface        "interface"
#define PYTHONintipaddr        "intipaddr"
#define PYTHONintport          "intport"
#define PYTHONuser             "user"
#define PYTHONpass             "pass"
#define PYTHONtype             "type"
#define PYTHONnewsgroup        "newsgroup"

/*  Max number of items in dictionary to pass to auth methods. */
#define	_PY_MAX_AUTH_ITEM	10


/*  Pointers to external Python objects. */
PyObject	*PYAuthObject = NULL;

/*  Dictionary of params to pass to authentication methods. */
PyObject	*PYauthinfo = NULL;
PyObject	**PYauthitem = NULL;

/*  Forward declaration. */
static PyObject *PY_set_auth_hook(PyObject *dummy, PyObject *args);
void PY_load_python(void);
PyObject* PY_setup(int type, int method, char *file);
static const void *file_key(const void *p);
static bool file_equal(const void *k, const void *p);
static void file_free(void *p);
static void file_trav(void *data, void* null);

bool PythonLoaded = false;

/*  Structure for storage of attributes for a module file. */
typedef struct PyFile {
    char        *file;
    bool        loaded[PYTHONtypes_max];
    PyObject	*procs[PYTHONtypes_max][PYTHONmethods_max];
} PyFile;

/*  Hash for storing files. */
struct hash *files;

/*  For passing the dynamic module filename from perm.c. */
char* dynamic_file;



/*
**  Authenticate connecting host by username&password.
**
**  code contains the NNTP reply code as returned by Python method
**  or -1 if method is not defined.
*/
void
PY_authenticate(char* file, char *User, char *Password, int *code,
                char *errorstring, char *newUser)
{
    PyObject    *result, *item, *proc;
    int         authnum;
    int         i;
    const char  *temp;

    PY_load_python();
    proc = PY_setup(PYTHONauthen, PYTHONmain, file);

    /* Return if authentication method is not defined. */
    if (proc == NULL)
        *code = -1;

    /* Initialize PythonAuthObject with connect method specific items. */
    authnum = 0;

    /* Client hostname. */
    PYauthitem[authnum] = PYBUFF_FROMMEMORY(Client.host, strlen(Client.host));
    PyDict_SetItemString(PYauthinfo, PYTHONhostname, PYauthitem[authnum++]);

    /* Client IP number. */
    PYauthitem[authnum] =PYBUFF_FROMMEMORY(Client.ip, strlen(Client.ip));
    PyDict_SetItemString(PYauthinfo, PYTHONipaddress, PYauthitem[authnum++]);

    /* Client port number. */
    PYauthitem[authnum] = PyInt_FromLong(Client.port);
    PyDict_SetItemString(PYauthinfo, PYTHONport, PYauthitem[authnum++]);

    /* Server interface the connection comes to. */
    PYauthitem[authnum] = PYBUFF_FROMMEMORY(Client.serverhost,
                                            strlen(Client.serverhost));
    PyDict_SetItemString(PYauthinfo, PYTHONinterface, PYauthitem[authnum++]);

    /* Server IP number. */
    PYauthitem[authnum] = PYBUFF_FROMMEMORY(Client.serverip,
                                            strlen(Client.serverip));
    PyDict_SetItemString(PYauthinfo, PYTHONintipaddr, PYauthitem[authnum++]);

    /* Server port number. */
    PYauthitem[authnum] = PyInt_FromLong(Client.serverport);
    PyDict_SetItemString(PYauthinfo, PYTHONintport, PYauthitem[authnum++]);

    /* Username if known. */
    if (User == NULL) {
        PYauthitem[authnum] = Py_None;
    } else {
        PYauthitem[authnum] = PYBUFF_FROMMEMORY(User, strlen(User));
    }
    PyDict_SetItemString(PYauthinfo, PYTHONuser, PYauthitem[authnum++]);

    /* Password if known. */
    if (Password == NULL) {
        PYauthitem[authnum] = Py_None;
    } else {
        PYauthitem[authnum] = PYBUFF_FROMMEMORY(Password, strlen(Password));
    }
    PyDict_SetItemString(PYauthinfo, PYTHONpass, PYauthitem[authnum++]);

    /*
    ** Now invoke authenticate method and see if it likes this user.
    */
    result = PyObject_CallFunction(proc, (char *) "O", PYauthinfo);

    /* Check the response. */
    if (result == NULL || !PyTuple_Check(result)
        || ((PyTuple_Size(result) != 2) && (PyTuple_Size(result) != 3)))
    {
        syslog(L_ERROR, "python authenticate method returned wrong result");
	Reply("%d Internal error (2).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
	ExitWithStats(1, true);
    }

    /* Get the NNTP response code. */
    item = PyTuple_GetItem(result, 0);

    /* Check the item. */
    if (!PyInt_Check(item))
    {
        syslog(L_ERROR, "python authenticate method returned bad NNTP response code");
	Reply("%d Internal error (2).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
	ExitWithStats(1, true);
    }

    /* Store the code. */
    *code = PyInt_AS_LONG(item);

    /* Get the error string. */
    item = PyTuple_GetItem(result, 1);

    /* Check the item. */
    if (!PyString_Check(item))
    {
        syslog(L_ERROR, "python authenticate method returned bad error string");
	Reply("%d Internal error (2).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
	ExitWithStats(1, true);
    }

    /* Store error string. */
    temp = PyString_AS_STRING(item);
    strlcpy(errorstring, temp, BIG_BUFFER);
    
    if (PyTuple_Size(result) == 3) {
        
        /* Get the username string. */
        item = PyTuple_GetItem(result, 2);
        
        /* Check the item. */
        if (!PyString_Check(item)) {
            syslog(L_ERROR, "python authenticate method returned bad username string");
            Reply("%d Internal error (2).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
            ExitWithStats(1, true);
        }

        /* Store user string. */
        temp = PyString_AS_STRING(item);
        strlcpy(newUser, temp, BIG_BUFFER);
    }

    /* Clean up the dictionary object. */
    PyDict_Clear(PYauthinfo);

    /* Clean up dictionary items. */
    for (i = 0; i < authnum; i++) {
        if (PYauthitem[i] != Py_None) {
            Py_DECREF(PYauthitem[i]);
        }
    }

    /* Log auth result. */
    syslog(L_NOTICE, "python authenticate method succeeded, return code %d, error string %s", *code, errorstring);
}



/*
**  Create an access group based on the values returned by the script in file.
**
**  The access_vec vector is set.
**  If the access group cannot be generated, the connection is closed.
*/
void
PY_access(char* file, struct vector *access_vec, char *User)
{
    PyObject	*result, *key, *value, *proc;
    char        *buffer;
    int		authnum;
    int		i;
    Py_ssize_t  pos;

    PY_load_python();
    proc = PY_setup(PYTHONaccess, PYTHONmain, file);

    /* Exit if access method is not defined. */
    if (proc == NULL) {
        syslog(L_ERROR, "python access method not defined");
 	Reply("%d Internal error (3).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
 	ExitWithStats(1, true);
     }
 
    /* Initialize PythonAuthObject with group method specific items. */
    authnum = 0;

    /* Client hostname. */
    PYauthitem[authnum] = PYBUFF_FROMMEMORY(Client.host, strlen(Client.host));
    PyDict_SetItemString(PYauthinfo, PYTHONhostname, PYauthitem[authnum++]);

    /* Client IP number. */
    PYauthitem[authnum] = PYBUFF_FROMMEMORY(Client.ip, strlen(Client.ip));
    PyDict_SetItemString(PYauthinfo, PYTHONipaddress, PYauthitem[authnum++]);

    /* Client port number. */
    PYauthitem[authnum] = PyInt_FromLong(Client.port);
    PyDict_SetItemString(PYauthinfo, PYTHONport, PYauthitem[authnum++]);

    /* Server interface the connection comes to. */
    PYauthitem[authnum] = PYBUFF_FROMMEMORY(Client.serverhost,
                                            strlen(Client.serverhost));
    PyDict_SetItemString(PYauthinfo, PYTHONinterface, PYauthitem[authnum++]);

    /* Server IP number. */
    PYauthitem[authnum] = PYBUFF_FROMMEMORY(Client.serverip,
                                            strlen(Client.serverip));
    PyDict_SetItemString(PYauthinfo, PYTHONintipaddr, PYauthitem[authnum++]);

    /* Server port number. */
    PYauthitem[authnum] = PyInt_FromLong(Client.serverport);
    PyDict_SetItemString(PYauthinfo, PYTHONintport, PYauthitem[authnum++]);

    /* Username. */
    PYauthitem[authnum] = PYBUFF_FROMMEMORY(User, strlen(User));
    PyDict_SetItemString(PYauthinfo, PYTHONuser, PYauthitem[authnum++]);
 
    /* Password is not known. */
    PYauthitem[authnum] = Py_None;
    PyDict_SetItemString(PYauthinfo, PYTHONpass, PYauthitem[authnum++]);

    /*
    ** Now invoke newsgroup access method.
    */
    result = PyObject_CallFunction(proc, (char *) "O", PYauthinfo);

    /* Check the response. */
    if (result == NULL || result == Py_None || !PyDict_Check(result)) {
        syslog(L_ERROR, "python access method returned wrong result -- expected a dictionary");
	Reply("%d Internal error (2).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
	ExitWithStats(1, true);
     }
 
    /* Resize vector to dictionary length. */
    vector_resize(access_vec, PyDict_Size(result) - 1);

    /* Store dict values in proper format in access vector. */
    pos = 0;
    buffer = xmalloc(BIG_BUFFER);

    while(PyDict_Next(result, &pos, &key, &value)) {
        if (!PyString_Check(key)) {
            syslog(L_ERROR, "python access method return dictionary key %ld not a string",
                   (long int) pos);
            Reply("%d Internal error (2).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
            ExitWithStats(1, false);
        }
        if (!PyString_Check(value)) {
            syslog(L_ERROR, "python access method return dictionary value %ld not a string",
                   (long int) pos);
            Reply("%d Internal error (2).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
            ExitWithStats(1, false);
        }
        
        strlcpy(buffer, PyString_AsString(key), BIG_BUFFER);
        strlcat(buffer, ": \"", BIG_BUFFER);
        strlcat(buffer, PyString_AsString(value), BIG_BUFFER);
        strlcat(buffer, "\"\n", BIG_BUFFER);

        vector_add(access_vec, buffer);
    }

    free(buffer);

    /* Clean up the dictionary object. */
    PyDict_Clear(PYauthinfo);

    /* Clean up dictionary items. */
    for (i = 0; i < authnum; i++) {
        if (PYauthitem[i] != Py_None) {
            Py_DECREF(PYauthitem[i]);
	}
    }

    /* Log auth result. */
    syslog(L_NOTICE, "python access method succeeded");
}



/*
**  Initialize dynamic access control code.
*/

void
PY_dynamic_init (char* file)
{
  dynamic_file = xstrdup(file);
  PY_use_dynamic = true;
}



/*
**  Determine dynamic user access rights to a given newsgroup.
**
**  Return 0 if the requested privilege is granted or a positive value
**  and a reply_message pointer initialized with reply message.
**  Return negative value if dynamic method is not defined.
*/
int
PY_dynamic(char *User, char *NewsGroup, int PostFlag, char **reply_message)
{
    PyObject	*result, *proc;
    char        *string;
    const char  *temp;
    int		authnum;
    int		i;

    PY_load_python();
    proc = PY_setup(PYTHONdynamic, PYTHONmain, dynamic_file);

    /* Return if dynamic method is not defined. */
    if (proc == NULL)
        return -1;

    /* Initialize PythonAuthObject with group method specific items. */
    authnum = 0;

    /* Client hostname. */
    PYauthitem[authnum] = PYBUFF_FROMMEMORY(Client.host, strlen(Client.host));
    PyDict_SetItemString(PYauthinfo, PYTHONhostname, PYauthitem[authnum++]);

    /* Client IP number. */
    PYauthitem[authnum] = PYBUFF_FROMMEMORY(Client.ip, strlen(Client.ip));
    PyDict_SetItemString(PYauthinfo, PYTHONipaddress, PYauthitem[authnum++]);

    /* Client port number. */
    PYauthitem[authnum] = PyInt_FromLong(Client.port);
    PyDict_SetItemString(PYauthinfo, PYTHONport, PYauthitem[authnum++]);

    /* Server interface the connection comes to. */
    PYauthitem[authnum] = PYBUFF_FROMMEMORY(Client.serverhost,
                                            strlen(Client.serverhost));
    PyDict_SetItemString(PYauthinfo, PYTHONinterface, PYauthitem[authnum++]);

    /* Server IP number. */
    PYauthitem[authnum] = PYBUFF_FROMMEMORY(Client.serverip,
                                            strlen(Client.serverip));
    PyDict_SetItemString(PYauthinfo, PYTHONintipaddr, PYauthitem[authnum++]);

    /* Server port number. */
    PYauthitem[authnum] = PyInt_FromLong(Client.serverport);
    PyDict_SetItemString(PYauthinfo, PYTHONintport, PYauthitem[authnum++]);
    
    /* Username. */
    PYauthitem[authnum] = PYBUFF_FROMMEMORY(User, strlen(User));
    PyDict_SetItemString(PYauthinfo, PYTHONuser, PYauthitem[authnum++]);
    
    /* Password is not known. */
    PYauthitem[authnum] = Py_None;
    PyDict_SetItemString(PYauthinfo, PYTHONpass, PYauthitem[authnum++]);

    /* Assign authentication type. */
    PYauthitem[authnum] =
        PYBUFF_FROMMEMORY((char *)(PostFlag ? "post" : "read"), 4);
    PyDict_SetItemString(PYauthinfo, PYTHONtype, PYauthitem[authnum++]);
 
    /* Newsgroup user tries to access. */
    PYauthitem[authnum] = PYBUFF_FROMMEMORY(NewsGroup, strlen(NewsGroup));
    PyDict_SetItemString(PYauthinfo, PYTHONnewsgroup,  PYauthitem[authnum++]);
    
    /*
    ** Now invoke newsgroup dynamic access method and see if
    ** it likes this user to access this newsgroup.
    */
    result = PyObject_CallFunction(proc, (char *) "O", PYauthinfo);

    /* Check the response. */
    if (result == NULL || (result != Py_None && !PyString_Check(result)))
    {
        syslog(L_ERROR, "python dynamic method (%s access) returned wrong result", PostFlag ? "post" : "read");
	Reply("%d Internal error (2).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
	ExitWithStats(1, false);
    }

    /* Get the response string. */
    if (result == Py_None) {
        string = NULL;
    } else {
        temp = PyString_AS_STRING(result);
        string = xstrdup(temp);
    }

    /* Clean up the dictionary object. */
    PyDict_Clear(PYauthinfo);

    /* Clean up dictionary items. */
    for (i = 0; i < authnum; i++) {
        if (PYauthitem[i] != Py_None) {
            Py_DECREF(PYauthitem[i]);
        }
    }

    /* Log auth result. */
    syslog(L_NOTICE, "python dynamic method (%s access) succeeded, refuse string: %s", PostFlag ? "post" : "read", string == NULL ? "<empty>" : string);

    /* Initialize reply string. */
    if (reply_message != NULL)
        *reply_message = string;
    
    /* Return result. */
    return string == NULL ? 0 : 1;
}



/*
**  This runs when nnrpd shuts down.  If Python is closed and reopened
**  in the same process, files and dynamic_file are reused so they
**  must point to NULL.
*/
void
PY_close_python(void)
{
    if (files != NULL) {
	hash_traverse(files, file_trav, NULL);
	hash_free(files);
        files = NULL;
    }
    if (dynamic_file != NULL) {
	free(dynamic_file);
        dynamic_file = NULL;
    }
}



/*
**  Traversal function for PY_close_python.
*/
void
file_trav(void *data, void* null UNUSED)
{
    PyFile *fp = data;
    int j;
    PyObject *result, *func;

    for (j = 1; j < PYTHONtypes_max; j++) {
        if (fp->loaded[j] != false) {
            func = fp->procs[j][PYTHONclose];
              if (func != NULL) {
                  result = PyObject_CallFunction(func, NULL);
                  Py_XDECREF(result);
              }
        }
    }
}



/*
**  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;
    Py_ssize_t   levellen;
    char        *logmsg;
    Py_ssize_t   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) {
    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;
    default:            priority = LOG_NOTICE ;
    }

    /* Log the message. */
    syslog(priority, "python: %s", logmsg);

    /* Return None. */
    Py_INCREF(Py_None);
    return Py_None;
}



/*
**  Make the internal nnrpd 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 nnrpdPyMethods[] = {
    METHOD("set_auth_hook", PY_set_auth_hook, METH_VARARGS, ""),
    METHOD("syslog",        PY_syslog,        METH_VARARGS, ""),
    METHOD(NULL,            NULL,             0,            "")
};

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef nnrpdPyModule = {
    PyModuleDef_HEAD_INIT,                /* m_base */
    (char *) "nnrpd",                     /* m_name */
    (char *) "nnrpd Python filter hook",  /* m_doc */
    -1,                                   /* m_size */
    nnrpdPyMethods,                       /* m_methods */
    NULL,                                 /* m_slots */
    NULL,                                 /* m_traverse */
    NULL,                                 /* m_clear */
    NULL,                                 /* m_free */
};

PyMODINIT_FUNC
PyInit_nnrpd(void) {
    PyObject *module = PyModule_Create(&nnrpdPyModule);

    if (module == NULL)
        syslog(L_ERROR, "failed to create nnrpd python module");

    return module;
}
#else
void
PyInit_nnrpd(void) {
    if (Py_InitModule3((char *) "nnrpd", nnrpdPyMethods,
                       (char *) "nnrpd Python filter hook") == NULL)
        syslog(L_ERROR, "failed to initialize nnrpd python module");
}
#endif


/*
**  Called by the external module so it can register itself with nnrpd.
*/
static PyObject *
PY_set_auth_hook(PyObject *dummy UNUSED, PyObject *args)
{
    PyObject    *result = NULL;
    PyObject    *temp;

    /* set_auth_hook method should return a pointer to nnrpd auth object. */
    if (PyArg_ParseTuple(args, (char *) "O:set_auth_hook", &temp)) {
        Py_XINCREF(temp);
        Py_XDECREF(PYAuthObject);
        PYAuthObject = temp;
        Py_INCREF(Py_None);
        result = Py_None;
    }

    /* Return a pointer to nnrpd auth method. */
    return result;
}



/*
**  Load the Python interpreter.
*/
void
PY_load_python(void)
{
    if (!PythonLoaded) {
        /*
        ** 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);

        /* Build a module interface to certain nnrpd functions. */
        PyImport_AppendInittab("nnrpd", &PyInit_nnrpd);

        /* 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;
        }

        /*
        ** Grab space for authinfo dictionary so we aren't forever
        ** recreating them.
        */
        PYauthinfo = PyDict_New();
        PYauthitem = xcalloc(_PY_MAX_AUTH_ITEM, sizeof(PyObject *));

        /* Create hash to store file attributes. */
        files = hash_create(4, hash_string, file_key,
                            file_equal, file_free);

        PythonLoaded = true;

        syslog(L_NOTICE, "python interpreter initialized OK");
    }
}



/*
**  Check that a method exists and is callable.	 Set up a pointer to
**  the corresponding PyObject, or NULL if not found.
*/
static void
PYdefonemethod(PyFile *fp, int type, int method, const char *methname, int realtype)
{
    PyObject **methptr;

    methptr = &fp->procs[type][method];

    /* There is no need to check the existence of methods useless for our realtype. */
    if (type == realtype) {
        /*
        ** 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(PYAuthObject, (char *) methname) == 1) {
            /* Get a pointer to given method. */
            *methptr = PyObject_GetAttrString(PYAuthObject, (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;
            }
        }
    } else {
        *methptr = NULL;
    }
}



/*
**  Look up all the known python methods and set up
**  pointers to them so that we could call them from nnrpd.
*/
static void
PYdefmethods(PyFile *fp, int realtype)
{
    /* Get a reference to authenticate() method. */
    PYdefonemethod(fp, PYTHONauthen, PYTHONmain, "authenticate", realtype);

    /* Get a reference to authen_init() method. */
    PYdefonemethod(fp, PYTHONauthen, PYTHONinit, "authen_init", realtype);
    
    /* Get a reference to authen_close() method. */
    PYdefonemethod(fp, PYTHONauthen, PYTHONclose, "authen_close", realtype);

    /* Get a reference to access() method. */
    PYdefonemethod(fp, PYTHONaccess, PYTHONmain, "access", realtype);
    
    /* Get a reference to access_init() method. */
    PYdefonemethod(fp, PYTHONaccess, PYTHONinit, "access_init", realtype);
    
    /* Get a reference to access_close() method. */
    PYdefonemethod(fp, PYTHONaccess, PYTHONclose, "access_close", realtype);
    
    /* Get a reference to dynamic() method. */
    PYdefonemethod(fp, PYTHONdynamic, PYTHONmain, "dynamic", realtype);
    
    /* Get a reference to dynamic_init() method. */
    PYdefonemethod(fp, PYTHONdynamic, PYTHONinit, "dynamic_init", realtype);
    
    /* Get a reference to dynamic_close() method. */
    PYdefonemethod(fp, PYTHONdynamic, PYTHONclose, "dynamic_close", realtype);
}



/*
**  Called when a python hook is needed -- this gets the scripts hooked in.
*/
PyObject*
PY_setup(int type, int method, char *file)
{
    int i;
    PyFile *fp;
    PyObject *result;

    /* Check to see if this file is in files. */
    if ((fp = hash_lookup(files, file)) == NULL) {
        fp = xmalloc(sizeof(PyFile));
        fp->file = xstrdup(file);

        for (i = 1; i < PYTHONtypes_max; i++) {
            fp->loaded[i] = false;
        }
        
        /* Load up external module. */
        (void) PyImport_ImportModule(file);

        /* See if nnrpd auth object is defined in auth module. */
        if (PYAuthObject == NULL) {
            syslog(L_ERROR, "python auth object is not defined");
            Reply("%d Internal error (3).  Goodbye!\r\n", NNTP_FAIL_TERMINATING);
            PY_close_python();
            ExitWithStats(1, false);
        } else {
            /* Set up pointers to known Python methods. */
            PYdefmethods(fp, type);
        }
        hash_insert(files, file, fp);

        if ((!fp->loaded[type]) && (fp->procs[type][PYTHONinit] != NULL)) {
            result = PyObject_CallFunction(fp->procs[type][PYTHONinit], NULL);
            if (result != NULL) {
                Py_XDECREF(result);
            }
            fp->loaded[type] = true;
        }
    }
    return fp->procs[type][method];
}



/*
**  Return the key (filename) from a file struct, used by the hash table.
*/
static const void *
file_key(const void *p)
{
    const struct PyFile *f = p;

    return f->file;
}



/*
**  Check to see if a provided key matches the key of a PyFile struct,
**  used by the hash table.
*/
static bool
file_equal(const void *k, const void *p)
{
    const char *key = k;
    const struct PyFile *f = p;

    return strcmp(key, f->file) == 0;
}



/*
**  Free a file, used by the hash table.
*/
static void
file_free(void *p)
{
    struct PyFile *fp = p;
    int i, j;

    free(fp->file);

    for (i = 1; i < PYTHONtypes_max; i++) {
        for (j = 1; j < PYTHONmethods_max; j++) {
            if (fp->procs[i][j] != NULL) {
                Py_DECREF(fp->procs[i][j]);
            }
        }
    }

    free(fp);
}

#endif /* defined(DO_PYTHON) */