File: post.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 (244 lines) | stat: -rw-r--r-- 5,743 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
/*
 * 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.
 */

/*
 * Accept a post from the network.
 * Does not check for .nopost, relies on SNPOST script for that.
 * Oddly, LIST checks .nopost, in group_info().  This is a bit
 * inconsistent, since if snntpd says can't post here, we should not
 * accept the article, but we do, then discard.  In a later version I
 * might just have LIST always have the posting flag set to 'y',
 * since according to RFC it is almost meaningless.
 */

#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "config.h"
#include "snntpd.h"
#include "args.h"
#include "unfold.h"
#include <readln.h>
#include <b.h>
#include <format.h>
#include <out.h>

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

static int tmpfd = -1;
static bool have_newsgroups;
static bool have_control;
static int error;

#define ER_READ 1
#define ER_WRITE 2
#define ER_FMT 3
#define ER_EOF 4
#define ER_MEM 5

static char *getval (char *field)
{
   while (*field && ':' != *field)
      field++;
   field++;
   while (' ' == *field || '\t' == *field)
      field++;
   return field;
}

static int write_tmp (char *line, int len)
{
   static char env_control[1000];
   static char env_newsgroups[1000];

   if (error)
      return 0;
   if (0 == strncasecmp(line, "X-sn-", 5))
      return 0;
   else if (!have_control && 0 == strncasecmp(line, "Control:", 8))
   {
      strcpy(env_control, "CONTROL=");
      strncat(env_control, getval(line), 991);
      if (putenv(env_control))
         error = ER_MEM;
      else
         have_control = TRUE;
   }
   if (-1 == write(tmpfd, line, len))
      error = ER_WRITE;
   if (-1 == write(tmpfd, "\r\n", 2))
      error = ER_WRITE;
   if (!have_newsgroups && 0 == strncasecmp(line, "Newsgroups:", 11))
   {
      char *p;

      for (line = p = getval(line); *p; p++)
         if (',' == *p)
            *p = ' ';
      strcpy(env_newsgroups, "NEWSGROUPS=");
      strncat(env_newsgroups, line, 988);
      if (putenv(env_newsgroups))
         error = ER_MEM;
      else
         have_newsgroups = TRUE;
   }
   return error;
}

void do_post (void)
{
   char buf[80];
   char *line;
   int len, p[2], pid, s;

   if (!posting_ok)
   {
      args_write(1, "440 Posting not allowed\r\n");
      return;
   }

   if (-1 == tmpfd)
   {
      formats(buf, sizeof (buf) - 1, "/tmp/.post.%d", getpid());
      tmpfd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0644);
      if (-1 == tmpfd)
      {
         LOG("do_post:open(%s):%m", buf);
         goto internal;
      }
      unlink(buf);
   }
   error = 0;
   have_control = have_newsgroups = FALSE;
   putenv("CONTROL=");
   putenv("NEWSGROUPS=");
   
   args_write(1, "340 Go ahead\r\n");
   
   /* Writes everything into the tmp file */
   
   lseek(tmpfd, 0, SEEK_SET);
   switch (unfold(&input, write_tmp))
   {
      case -3: break;
      case -2: error = ER_FMT; break;
      case -1: LOG("do_post:read error:%m");
      case 0: return;
   }

   if (!error)
   {
      if (client_ip && *client_ip)
         writef(tmpfd, "NNTP-Posting-Host: %s\r\n", client_ip);
      if (-1 == write(tmpfd, "\r\n", 2))
         error = ER_WRITE;
   }
   while ((len = readln(&input, &line, '\n')) > 0)
   {
      if (!error)
      {
         if (--len > 0)
            if ('\r' == line[len - 1])
               --len;
         if (-1 == write(tmpfd, line, len))
            error = ER_WRITE;
         if (-1 == write(tmpfd, "\r\n", 2))
            error = ER_WRITE;
      }
      if (1 == len && '.' == *line)
         break;
   }

   if (!have_newsgroups)
   {
      args_write(1, "441 No Newsgroups line\r\n");
      return;
   }
   if (error)
   {
      switch (error)
      {
         case ER_READ: LOG("do_post: read error:%m?"); break;
         case ER_WRITE: LOG("do_post: write(tmp):%m"); break;
         case ER_MEM: LOG("do_post: no memory"); break;
         case ER_FMT: args_write(1, "441 Bad article format\r\n"); return;
         case ER_EOF: return;
      }
internal:
      args_write(1, "441 Internal error\r\n");
      return;
   }
   ftruncate(tmpfd, lseek(tmpfd, 0, SEEK_CUR));

   if (-1 == (pid = pipefork(p)))
   {
      LOG("do_post:pipe/fork:%m");
      goto internal;
   }

   if (0 == pid)
   {
      char *av[2];

      close(p[0]);
      lseek(tmpfd, 0, SEEK_SET);
      dup2(tmpfd, 0);
      dup2(p[1], 1);
      av[1] = 0;
      *av = "./.SNPOST";
      execv(*av, av);
      if (ENOENT == errno)
      {
         *av += 3;
         execvp(*av, av);
      }
      fail(2, "do_post:exec(%s):%m", *av);
   }

   close(p[1]);
   *buf = '\0';
   do
   {
      struct readln prog;

      if (readln_ready(p[0], 0, &prog))
         break;
      while ((len = readln(&prog, &line, '\n')) > 0)
      {
         line[--len] = '\0';
         LOG("SNPOST:%s", line);
         if (len > 0)
            if ('\r' == line[len - 1])
               --len;
         if (len > sizeof (buf) - 1)
            len = sizeof (buf) - 1;
         strncpy(buf, line, len)[len] = '\0';
      }
      readln_done(&prog);
   }
   while (0);

   while (-1 == waitpid(pid, &s, 0) && EINTR == errno) ;
   if (WIFEXITED(s))
   {
      if (0 == (s = WEXITSTATUS(s)))
      {
         args_write(1, "240 %s\r\n", *buf ? buf : "Yummy");
         return;
      }
      LOG("do_post:SNPOST died with %d", s);
   }
   else if (WIFSIGNALED(s))
      LOG("do_post:SNPOST caught signal %d", WTERMSIG(s));
   else
      LOG("do_post:SNPOST unknown wait status %d", s);

   args_write(1, "441 %s\r\n", *buf ? buf : "Posting failed");
}