root / nntpd.h @ 602

View | Annotate | Download

1
#ifndef NNTPD_H
2
# define NNTPD_H
3
#include <time.h>
4
#include <sys/time.h>
5
#include <sys/types.h>
6
#include <sys/socket.h>
7
#include <sys/syslog.h>
8
#include <sys/stat.h>
9
#include <sys/wait.h>
10
#include <netinet/in.h>
11
#include <arpa/inet.h>
12
#include <netdb.h>
13
#include <errno.h>
14
#include <fcntl.h>
15
#include <string.h>
16
#include <ctype.h>
17
#include <signal.h>
18
#include <syslog.h>
19
#include <pwd.h>
20
#include <dlfcn.h>
21
#include <unistd.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <stdarg.h>
25
#include <stddef.h>
26
#include <inttypes.h>
27
#include <openssl/ssl.h>
28
29
#ifndef false
30
#define false 0
31
#endif
32
33
#ifndef true
34
#define true 1
35
#endif
36
37
#ifndef ulong
38
#define ulong unsigned long
39
#endif
40
41
#ifndef bool
42
#define bool unsigned char
43
#endif
44
45
#ifndef ulong64
46
typedef unsigned long long ulong64;
47
#endif
48
49
#ifndef long64
50
typedef long long long64;
51
#endif
52
53
#define MAX_HOSTNAME 256        /* max hostname length */
54
#define MAX_USERPASS 64                /* max length of username or password */
55
#define MAX_ACLS 2048
56
#define MAX_ARGS 64
57
#define MAX_ALIASES 256
58
#define MAX_AUTHS 2048
59
#define MAX_PROFILES 256
60
#define MAX_SERVERS 128                /* let me know if you have more! */
61
#define MAX_GROUP 160                /* max groupname length */
62
#define MAX_SERVER 32                /* max servername length (hostname and short id name in servers.conf) */
63
#define MAX_SERVERRSP 512        /* max length of server reply */
64
#define MAX_GROUPS 128000
65
#define MAX_PROC 8192                /* max of the max, config via nntpswitch.conf */
66
#define MAX_USERS MAX_PROC
67
#define MAX_HEADER 1024                /* max size of single header */
68
#define MAX_FOOTER 1024                /* max length of appended footer */
69
#define MAX_WILDMATS 32                /* max N wildmat pattern (access.conf) */
70
#define MAX_OVERVIEWFMT 16        /* max lines */
71
#define MAX_LEVEL 1000                /* max Level in server config */
72
#define MAX_CFGLINE 16384        /* max line length in config files */
73
#define MAX_DAYLIMITS 16        /* max size of profile Limits array */
74
#define MAX_POST_AGE 86400          /* oldest (or newest) post we accept, max 1d old or 1d new */
75
#define MAX_PATH 128                /* max path+filename length */
76
77
/* The following might be hardcoded in sscanf's */
78
#define MAX_STRING 256                /* generally max string length */
79
#define MAX_NAME 32                /* general max length of a name for something */
80
81
#define MSG_OVERVIEWFMT                "215 Overview.fmt follows\r\n"
82
#define MSG_POST_RECEIVED        "240 Thanks!\r\n"
83
#define MSG_POST_MAILED                "240 Thanks, post mailed to moderator\r\n"
84
#define MSG_POST_OK                "340 Try Me!\r\n"
85
#define MSG_POST_WRONGHEADER        "441 Newsgroups, From or Subject missing\r\n"
86
#define MSG_POST_NOMAIL                "441 Couldn't mail your post to the moderator, please try again\r\n"
87
#define MSG_POST_CANCEL                "441 Post Cancelled\r\n"
88
#define MSG_POST_HDRTOOLONG        "441 Header too long\r\n"
89
#define MSG_POST_TOOOLD                "441 Post is too old, check your system clock\r\n"
90
#define MSG_POST_TOONEW                "441 Post is too new, check your system clock\r\n"
91
#define MSG_POST_WRONGEMAIL        "441 From address not in internet syntax\r\n"
92
#define MSG_POST_WRONGMSGID        "441 Incorrect Message-ID syntax\r\n"
93
#define MSG_AUTH_OK                "281 Authentication Accepted\r\n"
94
#define MSG_MOREAUTHREQ                "381 More Authentication Required\r\n"
95
#define MSG_SESSIONLIMIT        "400 Session Time Limit Reached (please reconnect)\r\n"
96
#define MSG_BYTELIMIT                "400 Session Byte Limit Reached (please reconnect)\r\n"
97
#define MSG_AUTHREQUIRED        "480 Authentication Required\r\n"
98
#define MSG_AUTH_REJ                "482 Authentication Rejected\r\n"
99
#define MSG_AUTH_ERR                "482 Authentication Temporarily Unavailable\r\n"
100
#define MSG_AUTH_TOOMANYCON        "482 Too many connections in your class\r\n"
101
#define MSG_AUTH_TOOMANYUSR        "482 Too many users in your class\r\n"
102
#define MSG_AUTH_USERFULL        "482 Too many connections for your user\r\n"
103
#define MSG_AUTH_HOSTFULL        "482 You are already connected from a different host\r\n"
104
#define MSG_AUTH_TWICE                "281 You are already authenticated (please disconnect first)\r\n"
105
#define MSG_NOSUCHGROUP                "411 No such group\r\n"
106
#define MSG_NOGROUP                "412 No group selected\r\n"
107
#define MSG_ARTICLECANT                "430 Cant retrieve article, try joining a newsgroup first\r\n"
108
#define MSG_ARTICLENOPERM        "430 Cant retrieve article, permission denied\r\n"
109
#define MSG_NOPOSTING                "440 Posting Denied\r\n"
110
#define MSG_SERVER_DOWN                "403 Remote Server Unavailable\r\n"
111
#define MSG_COMMAND_HTTP        "500 Disconnected due to HTTP command\r\n"
112
#define MSG_NONEWNEWS                "501 NEWNEWS only supports single newsgroup names\r\n"
113
#define MSG_NOPATINDEX                "501 Sorry, your search for this header type has been disabled\r\n"
114
#define MSG_SERVERFULL                "400 Sorry, server is full at the moment, try later\r\n"
115
#define MSG_TOOMANYUSERS        "400 Too many users\r\n"
116
#define MSG_TOOMANYUSERSPRF        "400 Too many users in your class\r\n"
117
#define MSG_TOOMANYCONNSPRF        "400 Too many connections in your class\r\n"
118
#define MSG_TOOMANY_CONCURRENT        "400 Too many concurrent connections, you are allowed %d\r\n"
119
#define MSG_GENERIC_ERROR        "502 %s\r\n"
120
#define MSG_UNSUPPORTED                "500 Unsupported\r\n"
121
#define MSG_NOTIMPLEMENTED        "500 Not Implemented\r\n"
122
#define MSG_NOACL                "500 Can't get ACL\r\n"
123
#define MSG_NOPERM                "500 Permission Denied\r\n"
124
#define MSG_AUTH_UNKNOWN        "501 Unsupported AUTHINFO command\r\n"
125
#define MSG_AUTH_NOMEM                "501 Out of memory for authentication\r\n"
126
127
#define MSG_CANNOTSSL           "500 Cannot setup SSL connection\r\n"
128
#define MSG_SSLDISABLED         "502 SSL connection is disabled for this user\r\n"
129
130
#define UMSG_SERVERTIMEOUT        "Remote Server Timeout"
131
132
#define DEFAULT_AUTH                "auth_default"
133
#define DEFAULT_ACCT                "acct_default"
134
#define DEFAULT_PROFILE                "default"
135
#define DEFAULT_WILDMAT                "*"
136
137
#define RL_INITVAL 100000        /* this much usec as start for rate-limits */
138
139
/* If this is defined, send accounting record after this many bytes
140
 * instead of waiting for end of connection
141
 */
142
#define INTERMEDIATE_ACCT 500000000
143
144
#ifdef NSE
145
#define MAX_PFXMAPS 4
146
#define PFX_MAPSIZE 0x3FFFFF
147
#endif
148
149
enum article_result 
150
{
151
        CHECK_ARTICLE_OK,
152
        CHECK_ARTICLE_FAIL,
153
        CHECK_ARTICLE_NOPERM
154
};
155
156
157
enum connect_result 
158
{
159
        CONNECT_OK,
160
        CONNECT_ERR,
161
        CONNECT_DOWN
162
};
163
164
165
#ifdef NSE
166
typedef struct
167
{
168
        char                name[32];
169
        char                map[PFX_MAPSIZE];
170
} PFXMAP;
171
#endif
172
173
174
struct _hlist
175
{
176
        char *hdr;
177
        char *val;
178
        struct _hlist *next;
179
};
180
#define HLIST struct _hlist
181
182
183
typedef struct 
184
{
185
        char                name[MAX_STRING];
186
        char                lib[MAX_STRING];
187
        char                args[MAX_STRING];
188
} ALIAS;
189
190
191
typedef struct 
192
{
193
        char                name[MAX_STRING];
194
        char                args[MAX_STRING];
195
} ARGS;
196
197
198
typedef struct
199
{
200
        int        gb;
201
        int        kbit;
202
} LIMITS;
203
204
205
struct _profile
206
{
207
        char                Name[MAX_NAME];
208
        char                ReadPat[MAX_STRING];
209
        char                PostPat[MAX_STRING];
210
        char                Banner[MAX_STRING];
211
        int                ReplaceBanner;
212
        int                MaxConnections;
213
        int                MaxUserConcurrent;
214
        int                MaxHostConcurrent;
215
        int                SingleHostUser;
216
        int                RemoteSingleHostUser;
217
        int                Slave;
218
        int                MaxUsers;
219
        int                MaxSessionTime;
220
        ulong64                MaxSessionBytes;
221
        uint                MaxUserBPS;
222
        uint                MaxProfileBPS;
223
        char                XComplaintsTo[MAX_STRING];
224
        char                Organization[MAX_STRING];
225
        int                ForceOrganization;
226
        int                AddNNTPPostingHost;
227
        char                Hostname[MAX_STRING];
228
        char                FooterFile[MAX_STRING];
229
        char                Footer[MAX_FOOTER];
230
        int                FooterSize;
231
        int                NoControl;
232
        int                ArticleSearch;
233
        int                ClientReadTimeout;
234
        ALIAS*                Accounting;
235
        LIMITS                Limits[MAX_DAYLIMITS];
236
        int                NumLimits;
237
        char                Disabled[MAX_STRING];
238
        int             AllowSSL;
239
240
        uint                connections;                /* current connection counter */
241
        uint                numusers;                /* current user counter */
242
        uint                realusers;                /* active users counter (i.e. after auth) */
243
        ulong64                bytes;                        /* for rrd */
244
        ulong                articles;
245
        ulong64                postbytes;
246
        ulong                postarticles;
247
248
        /* rate limiter */
249
        uint                rl_curbytes;
250
        uint                rl_lastbytes;
251
        double                rl_sleep;
252
        struct timeval        rl_start;
253
        double                rl_curbps;
254
255
        double                UserRLFactorU;
256
        double                UserRLFactorD;
257
        double                ProfileRLFactorU;
258
        double                ProfileRLFactorD;
259
260
        uint                Retention;
261
#ifdef NSE
262
        char            XHeaderSSLUpload[MAX_HEADER];
263
        uint                RetentionCache;                /* > 0 = put in cache, but interferes with othr profiles */
264
        PFXMAP *        PrefixMap;
265
        struct _profile        * FailProfile;
266
#endif
267
};
268
#define PROFILE struct _profile
269
270
271
typedef struct
272
{
273
        char                mask[128];
274
        ALIAS*                auth;
275
        PROFILE*        profile;
276
} AUTH;
277
278
279
typedef struct
280
{
281
        /* username and password are set by the main nntpswitch authenticator */
282
        char *                username;
283
        char *                password;                /* cleartext */
284
        char *                username_s;                /* username with matched pattern stripped */
285
286
        int                port;                        /* port (slot) id */
287
        char *                hostname;                /* client hostname */
288
        struct in_addr        in_addr;                /* client ip adres */
289
290
        /* Those are copied from the MASTER struct */
291
        ARGS *                args;
292
        int                numargs;
293
294
        /* 
295
         * the following fields should be set by the authentication module 
296
         * if left blank (untouched by the module), the default from the 
297
         * currently selected ACL is used. The struct including strings are
298
         * free()'d after authentication.
299
         */
300
        bool                authenticated;                /* succeeded or not */
301
        char *                message;                /* txt message like '482 no access' or '281 welcome' */
302
        char *                profile;                /* profile name */
303
        bool                posting;                /* posting allowed */
304
        ulong64                bytes;                        /* bytes remaining */
305
        int                userkbit;                /* max user kbit */
306
        char *                logname;                /* username for accounting, eg stripped username */
307
} AUTHRESULT;
308
309
310
typedef struct
311
{
312
        ulong                start;
313
        ulong                end;
314
        ulong                size;
315
316
        PROFILE *        profile;
317
318
        uint                read:1;
319
        uint                post:1;
320
        uint                apost:1;
321
        uint                deny:1;
322
        uint                auth:1;
323
        uint                stats:1;
324
        uint                unlimit:1;
325
} ACL;
326
327
328
typedef struct
329
{
330
        char                pattern[1024];
331
        char                name[MAX_NAME];
332
} WILDMAT;
333
334
335
typedef struct
336
{
337
        ulong                hi;
338
        ulong                lo;
339
        ulong                times;
340
        ulong                id;
341
        char                mode;
342
        char                newsgroup[MAX_GROUP];
343
        char                server[32];
344
} ACTIVE;
345
346
347
enum _cmdnum
348
{
349
        cmd_mode,  cmd_slave, cmd_authinfo,
350
        cmd_help,  cmd_date,  cmd_quit,
351
        cmd_list,  cmd_group, cmd_listgroup, cmd_newgroups, cmd_xgtitle,
352
        cmd_over,  cmd_xover, cmd_xzver,     cmd_xhdr, cmd_xpat, cmd_newnews, cmd_post,
353
        cmd_stat,  cmd_head,  cmd_body,  cmd_article,  cmd_next,  cmd_last,
354
        cmd_ihave, cmd_http,  cmd_statistics, cmd_unsupported
355
};
356
357
358
typedef struct
359
{
360
        char*                command;
361
        enum _cmdnum         num;
362
        uint                auth:1;                        /* auth needed for command */
363
        uint                acct:1;                        /* account this command */
364
        uint                limit:1;                /* rate-limit this command */
365
        uint                usecount;
366
} CMDINFO;
367
368
369
enum servertype
370
{
371
        type_spool, type_xover, type_post
372
};
373
374
375
enum serverpol
376
{
377
        policy_single, policy_backup, policy_balance
378
};
379
380
381
typedef struct
382
{
383
        char                Name[MAX_NAME];
384
        char                Hostname[MAX_HOSTNAME];
385
        char                Groups[MAX_CFGLINE];
386
        char                Username[MAX_USERPASS];
387
        char                Password[MAX_USERPASS];
388
        uint                Port;
389
        uint                Timeout;
390
        uint                Level;
391
        enum servertype        ServerType;
392
        enum serverpol        Policy;
393
        bool                ActiveTimes;
394
        bool                Descriptions;
395
        bool                SplitList;
396
        uint                MaxConnections;
397
398
        uint                connections;
399
} SERVER;
400
401
402
typedef struct
403
{
404
        char                key[128];
405
        uint                id;                        /* this is the position in memory */
406
407
        uint                MaxUserBPS;                /* copied from profile or authresult */
408
        long64                bytesleft;                /* initialized on connect or authenticate */
409
        uint                connections;                /* number of client structs refering to us */
410
        uint                realuser;                /* set if client is connected, ready for commands etc */
411
412
        /* rate limiter */
413
        uint                rl_curbytes;
414
        uint                rl_lastbytes;
415
        double                rl_sleep;
416
        struct timeval        rl_start;
417
        double                rl_curbps;
418
} USER;
419
420
421
typedef struct
422
{
423
        int                socket;
424
        int             useSSL;
425
        SSL*            ssl;                
426
        time_t                connectsince;
427
        pid_t                pid;
428
        int                numcore;                /* proceccor core we're bound to */
429
430
        int                serversock;                /* primary server socket */
431
        int                postsock;                /* post server socket */
432
        SERVER *        groupserver;
433
        ACTIVE *        group;
434
        SERVER *        currserver;                /* replacement for lastserver */
435
436
        char                hostname[MAX_HOSTNAME];
437
        ulong                ip4addr;
438
        struct sockaddr_in addr;
439
440
        ACL *                acl;
441
        AUTH *                auth;
442
        CMDINFO *        command;
443
        PROFILE *        profile;
444
        USER *                user;
445
446
        char                username[MAX_USERPASS];
447
        char                logname[MAX_USERPASS];        /* for postfix stripped usernames */
448
        char                password[MAX_USERPASS];
449
        uint                id;
450
        uint                concur;                        /* this is the clients N'th session */
451
        char *                bbuf;                        /* client buffer */
452
453
        int                error;                        /* set if we got error and have to exit */
454
        int                timeout;                /* set if timeout was caught */
455
        char *                errstr;
456
457
        /* rate limiter */
458
        int                RateIntervalUs;                /* 1/cfg.Period * 100000 */
459
460
        /* statistics */
461
        ulong64                bytes;
462
        uint                groups;
463
        uint                articles;
464
        uint                posts;
465
        ulong64                postbytes;
466
        uint                grouparts;
467
        ulong64                groupbytes;
468
        uint                serverarts;
469
        ulong64                serverbytes;
470
        uint                starttime;
471
        long64                initbytes;                /* copied to user after auth */
472
473
        uint                connected:1;
474
        uint                authenticated:1;
475
        uint                inuse:1;
476
        uint                posting:1;                /* copied form acl and/or authres */
477
} CLIENT;
478
479
480
typedef struct
481
{
482
        int                numgroups;                /* total groups in active file */
483
        int                numaliases;
484
        int                numargs;
485
        int                numauths;
486
        int                numprofiles;
487
        int                numacls;                /* number of access.conf entries */
488
        int                numwildmats;                /* number of wildmat entries */
489
        int                numservers;
490
        time_t                laststatactive;                /* remember filedate using stat() */
491
        time_t                laststatserver;
492
        uint                serverstart;                /* uptime */
493
        ulong64                nrforks;
494
        int                numcores;                /* Number of cores in system */
495
        int                currcore;                /* Current core counter */
496
497
        int                connections;                /* total connections, also key for *clients */
498
        CLIENT                clients[MAX_PROC];
499
500
        int                numusers;                /* total users, also key for *users */
501
//        ulong                userid_id;                /* just incremental counter */
502
        USER                users[MAX_USERS];
503
504
        SERVER *        lservers[MAX_SERVERS];        /* servers sorted by level */
505
        SERVER                servers[MAX_SERVERS];
506
507
        AUTH                auths[MAX_AUTHS];
508
        PROFILE                profiles[MAX_PROFILES];
509
        ALIAS                aliases[MAX_ALIASES];
510
        WILDMAT                wildmats[MAX_WILDMATS];
511
        ACL                acls[MAX_ACLS];
512
        ARGS                args[MAX_ARGS];
513
514
        /* The BalanceID is a counter from 0 to N where N is the number of servers 
515
         * with the same level. This is used for the load balancing policy. */
516
        int                balance_pos[MAX_LEVEL];
517
518
        /* ipc semaphore */
519
        int                semid;
520
521
        uint                dllock;                        /* used for daylimits accounting lock */
522
523
#ifdef NSE
524
        uint                num_pfxmaps;
525
        PFXMAP                prefixmap[MAX_PFXMAPS] __attribute__ ((aligned(4)));
526
#endif
527
} MASTER;
528
529
530
extern CLIENT *client;
531
extern MASTER *master;
532
extern char* config_file;
533
534
time_t parsedate(char *);
535
536
#endif