File: commands.c

package info (click to toggle)
sn 0.3.8-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 976 kB
  • sloc: ansic: 9,255; sh: 467; makefile: 210
file content (506 lines) | stat: -rw-r--r-- 12,854 bytes parent folder | download | duplicates (6)
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
/*
 * This file is part of the sn package.
 * Distribution of sn is covered by the GNU GPL. See file COPYING.
 * Copyright � 1998-2000 Harold Tay.
 * Copyright � 2000- Patrik R�dman.
 */

/*
 * NNTP commands here.  Each command is a function of
 * type void foo (void).
 */

#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <limits.h>
#include "config.h"
#include "times.h"
#include "dhash.h"
#include "art.h"
#include "group.h"
#include "args.h"
#include "body.h"
#include "snntpd.h"
#include "key.h"
#include <b.h>
#include <tokensep.h>
#include <wildmat.h>
#include <out.h>
#include <format.h>

static const char ver_ctrl_id[] = "$Id: commands.c 29 2004-04-24 23:02:38Z patrik $";

#define putdot() args_write(1, ".\r\n")

void do_ihave (void) { args_write(1, "435 I'm happy for you\r\n"); }
void do_sendme (void) { args_write(1, "500 No thanks\r\n"); }
void do_slave (void) { args_write(1, "202 Whatever\r\n"); }
void do_help (void) { args_write(1, "100 Help yourself\r\n.\r\n"); }

void do_mode (void)
{
   if (posting_ok)
      args_write(1, "200 Hi, you can post (sn version " VERSION ")\r\n");
   else
      args_write(1, "201 Hi, you can't post (sn version " VERSION ")\r\n");
}

/* Several ways to specify an article.  The group may be the current
   one, or it may be a different one */

static char *specgroup;
static int speclo, spechi;

/* ...but with just one of two results */

static struct article article;
static char *specerror;

/*
 * For nntp commands that refer to an article, or a range of them,
 * these always appear in the form of a number, or range, or a mssage
 * ID.  readspec() understands these and sets the low and high end of
 * the range, and the group, and any error.
 */

static int readspec (char *spec)
{
   char *p;

   if (!spec)
   {
      if ((specgroup = currentgroup))
         if ((spechi = speclo = currentserial) > 0)
            return 0;
         else
            specerror = "420 No article selected\r\n";
      else
         specerror = "412 No group selected\r\n";
      return -1;
   }

   if ('<' == *spec)
   {
      struct data d;

      d.messageid = spec + 1;
      if ((p = strchr(d.messageid, '>')))
      {
         *p = '\0';
         if (0 == dh_find(&d, FALSE))
         {
            spechi = speclo = d.serial;
            specgroup = d.newsgroup; /* is static */
            return 0;
         }
         else
            specerror = "430 No such article\r\n";
      }
      else
         specerror = "430 Invalid message ID\r\n";
      return -1;
   }

   if ((specgroup = currentgroup))
   {
      char *end;

      speclo = strtoul(spec, &end, 10);
      spec = end;
      if ('-' == *end)
      {
         spec++;
         if (*spec)
         {
            spechi = strtoul(spec, &end, 10);
            if (*end)
               goto invalid;
            if (spechi < speclo)
               goto invalid;
         }
         else
            spechi = INT_MAX;
         if (spechi > currentinfo.last)
         {
            if (speclo <= currentinfo.last)
               spechi = currentinfo.last;
            else
               spechi = speclo;
         }
      }
      else if (!*spec)
         spechi = speclo;
      else
         goto invalid;
   }
   return 0;

invalid:
   specerror = "501 Invalid range\r\n";
   return -1;
}

static char *findid (void)
{
   char *id;

   if ((id = art_findfield(article.head, "Message-ID")) && *id)
      return id;
   return "<0>";
}

/*
 * Each time writefifo is called, the degree of interest in this
 * group increases until the fifo is written.
 */

static void writefifo (void)
{
   static char oldgroup[GROUPNAMELEN + 1] = "";
   static int interested = 0;

   if (-1 == fifo)
      return;
   if (strcasecmp(oldgroup, currentgroup) == 0)
   {
      interested++;
      if (3 == interested % 10)
      {
         int len;

         len = strlen(currentgroup);
         currentgroup[len] = '\n';
         (void) write(fifo, currentgroup, len + 1);
         currentgroup[len] = '\0';
      }
   }
   else
   {
      interested = 0;
      strcpy(oldgroup, currentgroup);
   }
}

#define PUTHEAD 0x1
#define PUTBODY 0x2

static void putarticle (int flag, int code, char *msg)
{
   char *p;

   if (-1 == readspec(args[1]))
   {
      args_write(1, specerror);
      return;
   }
   if (-1 == art_gimme(specgroup, speclo, &article))
   {
      args_write(1, "430 No such article\r\n");
      return;
   }
   if (speclo != spechi)
   {
      args_write(1, "512 Invalid argument\r\n");
      return;
   }

   if (flag & PUTBODY)
      do
      {
         /*
          * Bit dangerous.  To test the validity of the body we are about
          * to display, check these 2 things: the bytes -before- and
          * -after- the body buffer must be '\0'.  This is not normally
          * checked for in art_gimme() because it will fault in the page,
          * but now we know we will be accessing it.
          */

         if (!art_bodyiscorrupt(article.body, article.blen))
            if (0 == body(&article.body, &article.blen))
               if (!article.body[article.blen])
                  break;
               else
                  p = "Internal error, uncompressed to corrupt body";
            else
               p = "Internal error, can't uncompress body";
         else
            p = "Article is corrupt";
         args_write(1, "423 %s\r\n", p);
         return;
      }
      while (0);

   if (!args[1] || '<' != *args[1])
   {
      args_write(1, "%d %d %s %s\r\n", code, speclo, findid(), msg);
      currentserial = speclo;
      writefifo();
   }
   else
      args_write(1, "%d %d <%s> %s\r\n", code, speclo, args[1] + 1, msg);

   switch (flag)
   {
      case 0:
         return;
      case PUTHEAD:
      case PUTBODY | PUTHEAD:
         /* should never have let Xref into header, now have to remove it */
         p = strstr(article.head, "Xref:");
         if (p && (p == article.head || '\n' == p[-1]))
            writef(1, "%SXref: %s %s:%d%s",
                   p - article.head, article.head, me, specgroup, speclo, p + 5);
         else
            writef(1, "%sXref: %s %s:%d\r\n", article.head, me, specgroup, speclo);
         if (PUTHEAD == flag)
            break;
         write(1, "\r\n", 2);
      case PUTBODY:
         write(1, article.body, article.blen);
         if ('\n' != article.body[article.blen - 1])
         {
            LOG("putarticle:%s:%d does not end in newline", specgroup, speclo);
            write(1, "\r\n", 2);
         }
   }
   putdot();
}

void do_article (void) { putarticle(PUTHEAD | PUTBODY, 220, "Article follows"); }
void do_body (void) { putarticle(PUTBODY, 222, "Body follows"); }
void do_head (void) { putarticle(PUTHEAD, 221, "Head follows"); }
void do_stat (void) { putarticle(0, 223, "Request text separately"); }

void do_group (void)
{
   if (alltolower(args[1]) >= GROUPNAMELEN || make_current(args[1]))
      args_write(1, "411 No such group here as %s\r\n", args[1]);
   else
   {
      args_write(1, "211 %d %d %d %s\r\n", currentinfo.nr_articles,
                 currentinfo.first, currentinfo.last, args[1]);
      specgroup = 0;
      
      writefifo();
   }
}

static void nextlast (int inc, char *ermsg)
{
   int serial;
   int count = 2 * ARTSPERFILE;

   for (serial = currentserial + inc; serial && count; serial += inc, count--)
      if (0 == art_gimme(currentgroup, serial, &article))
      {
         args_write(1, "223 %d %s request text separately\r\n", serial, findid());
         currentserial = serial;
         return;
      }
   args_write(1, ermsg);
}

void do_last (void) { nextlast(-1, "422 No previous article\r\n"); }
void do_next (void) { nextlast(+1, "421 No next article\r\n"); }

/* date: YYMMDD; clock: HHMMSS */

#define DIG(c) (c - '0')

static time_t converttime (char *date, char *clock, char *gmt)
{
   struct tm tm = { 0, };
   time_t t = 0;
   int difference = 0;
   
   if (strspn(date, "0123456789") != 6 || strspn(clock, "0123456789") != 6)
      return ((time_t) -1);
   
   tm.tm_year = DIG(*date) * 10 + DIG(date[1]);
   date += 2;
   if (tm.tm_year < 70)
      tm.tm_year += 100;  /* 00 - 69 => 2000 - 2069 */
   tm.tm_mon = DIG(*date) * 10 + DIG(date[1]);
   date += 2;
   tm.tm_mon -= 1;
   tm.tm_mday = DIG(*date) * 10 + DIG(date[1]);
   
   /* FvL was here */
   tm.tm_hour = DIG(*clock) * 10 + DIG(clock[1]);
   clock += 2;
   tm.tm_min = DIG(*clock) * 10 + DIG(clock[1]);
   clock += 2;
   tm.tm_sec = DIG(*clock) * 10 + DIG(clock[1]);
   
   tm.tm_isdst = -1;   /* Avoid having the hour adjusted +1 by mktime() during DST */
   
   if (gmt && 0 == strcasecmp(gmt, "GMT"))
   {
      /* Convert to local time, all our stuff is in local time. */
      time_t then = mktime(&tm);
      struct tm *then_tm = gmtime(&then);
      
      then_tm->tm_isdst = -1;   /* Avoid having the hour adjusted +1 by mktime() during DST */
      difference = then - mktime(then_tm);
   }
   
   t = mktime(&tm);
   if (t == ((time_t) -1))
      return ((time_t) -1);
   return (t + difference);
}

void do_newgroups (void)
{
   time_t cutoff;

   cutoff = converttime(args[1], args[2], args[3]);
   if ((long) cutoff > 0)
   {
      /* XXX Do we -have- to always reply 231? */
      args_write(1, "231 new newsgroups follows\r\n");
      if (nr_keys > 0)
      {
         int n, i;
         struct key *kp;
         char *group;
         char buf[GROUPNAMELEN + 32];
         struct stat st;

         for_each_key(i, n, kp)
         {
            group = KEY(kp);
            formats(buf, sizeof (buf) - 1, "%s/.created", group);
            if (-1 == stat(buf, &st))
            {
               LOG("do_newsgroups:stat(%s/.created):%m", group);
               continue;
            }
            if (st.st_mtime >= cutoff)
            {
               struct group g;

               if (-1 == group_info(group, &g))
                  continue;
               args_write(1, "%s %d %d y\r\n", group, g.first, g.last);
            }
         }
      }
      else
         LOG2("do_newgroups:we have no groups");
   }
   else
      args_write(1, "231 Bad date or time\r\n");
   putdot();
}

void do_newnews (void)
{
   time_t cutoff;
   extern int nr_keys;
   int i, j, serial;
   char *pat;

   cutoff = converttime(args[2], args[3], args[4]);

   if (cutoff > 0)
   {
      struct key *kp;
      struct group g;

      args_write(1, "230 list of new articles follows\r\n");
      alltolower(args[1]);
      while ((pat = tokensep(args + 1, ",")))
         for_each_key(i, j, kp)
      {
         char *group;

         group = KEY(kp);
         if (wildmat(group, pat))
            if (0 == group_info(group, &g))
               if ((serial = times_since(group, cutoff)) > 0)
                  for (; serial < g.last; serial++)
                     if (0 == art_gimmenoderef(group, serial, &article))
                        args_write(1, "%s\r\n", findid());
      }
   }
   else
      args_write(1, "230 Bad date or time\r\n");
   args_write(1, ".\r\n");
}

static void xlooper (char *spec, void (*f) (void), char *msg)
{
   if (-1 == readspec(spec))
   {
      args_write(1, specerror);
      return;
   }
   args_write(1, msg);
   for (; speclo <= spechi; speclo++)
      if (0 == art_gimme(specgroup, speclo, &article))
         f();
   putdot();
   if (spec && '<' != *spec)
      writefifo();
}

static void putxover (void)
{
   struct xover x;

   art_makexover(&article, &x);
   writef(1, "%d\t%S\t%S\t%S\t%S\t%S\t%S\t%S\tXref: %s %s:%d\r\n",
          speclo,
          x.subject.len, x.subject.pointer,
          x.from.len, x.from.pointer,
          x.date.len, x.date.pointer,
          x.messageid.len, x.messageid.pointer,
          x.references.len, x.references.pointer,
          x.bytes.len, x.bytes.pointer,
	  x.lines.len, x.lines.pointer,
	  me, specgroup, speclo);
}

void do_xover (void) { xlooper(args[1], putxover, "224 XOVER follows\r\n"); }

static void putxhdr (void)
{
   char *id;

   id = art_findfield(article.head, args[1]);
   if (*id)
      args_write(1, "%d %s\r\n", speclo, id);
   else if (strcasecmp(args[1], "xref") == 0)   /* Xref needs to be generated on the fly */
      args_write(1, "%d %s %s:%d\r\n", speclo, me, specgroup, speclo);
}

void do_xhdr (void) { xlooper(args[2], putxhdr, "221 XHDR follows\r\n"); }

static char *field;

static void putxpat (void)
{
   char **globs;
   char *line;

   globs = args + 3;
   if ((line = art_findfield(article.head, field)) && *line)
      for (; *globs; globs++) /* XXX ORed? */
         if (wildmat(line, *globs))
         {
            args_write(1, "%d %s: %s\r\n", speclo, field, line);
            break;
         }
}

void do_xpat (void)
{
   field = args[1];
   xlooper(args[2], putxpat, "221 Headers follows\r\n");
}