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
|
/*
* 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 Rdman.
*/
/*
* Accept a post from the network.
* Does not check for .nopost, relies on POST 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 "snntpd.h"
#include "args.h"
#include "unfold.h"
#include <readln.h>
#include <b.h>
#include <format.h>
#include <out.h>
static const char rcsid[] = "$Id$";
static int tmpfd = -1;
static int have_newsgroups;
static int 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 = 1;
}
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 = 1;
}
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);
}
have_control = error = have_newsgroups = 0;
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 = "./.POST";
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("POST:%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:POST died with %d", s);
}
else if (WIFSIGNALED(s))
log("do_post:POST caught signal %d", WTERMSIG(s));
else
log("do_post:POST unknown wait status %d", s);
args_write(1, "441 %s\r\n", *buf ? buf : "Posting failed");
}
|