forked from virtadpt/eBBS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bbsmaild.c
284 lines (249 loc) · 7.34 KB
/
bbsmaild.c
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
#include "server.h"
#include <pwd.h>
/* Header types, so we can deal with continuations */
#define HEADER_NONE 0
#define HEADER_OTHER 1
#define HEADER_SUBJECT 2
#define HEADER_FROM 3
#define HEADER_REPLYTO 4
extern USERDATA user_params;
extern SERVERDATA server;
extern char *optarg;
extern int optind;
/* Are we bbsmail or bbsmaild? Assume bbsmaild */
int daemon = 1;
/* Information we need from the headers */
ADDR sender;
TITLE subject;
RNAME sendername;
/* sender, without the prefix */
char *sptr;
int ssize;
char * identify_header( char *str, int *hdr_type )
{
char *cp, *colon;
colon = strchr(str, ':');
if (colon == NULL) {
*hdr_type = HEADER_NONE;
return str;
}
*colon = '\0';
for (cp = str; cp != colon; cp++) {
if (*cp == ' ' || *cp == '\t') {
*hdr_type = HEADER_NONE;
return str;
}
}
if (!strcasecmp(str, "Subject")) *hdr_type = HEADER_SUBJECT;
else if (!strcasecmp(str, "From")) *hdr_type = HEADER_FROM;
else if (!strcasecmp(str, "Reply-To")) *hdr_type = HEADER_REPLYTO;
else *hdr_type = HEADER_OTHER;
for (cp = colon+1; *cp == ' ' || *cp == '\t'; cp++) ;
return cp;
}
split_sender( int type, char *str )
{
/*
This splits a From: or Reply-To: header into the address and
full name parts, according to RFC 822. Give the Reply-To precedence.
*/
char *cp, *sp;
cp = strchr(str, '<');
if (cp == NULL) {
/* Format should be "address (Real Name)" */
cp = strchr(str, ' ');
if (cp != NULL) *cp++ = '\0';
if (type == HEADER_REPLYTO || *sptr == '\0')
strncpy(sptr, str, ssize);
if (cp != NULL && *cp == '(') {
sp = strchr(cp, ')');
if (sp != NULL) {
*sp = '\0';
if (type == HEADER_REPLYTO || sendername[0] == '\0')
strncpy(sendername, cp, sizeof sendername);
}
}
}
else {
/* Format should be "Real Name <address>" */
if (cp > str) {
*(cp-1) = '\0';
if (type == HEADER_REPLYTO || sendername[0] == '\0')
strncpy(sendername, str, sizeof sendername);
}
sp = strchr(cp, '>');
if (sp != NULL) {
*sp = '\0';
if (type == HEADER_REPLYTO || *sptr == '\0')
strncpy(sptr, cp+1, ssize);
}
}
sender[strlen(sender)] = '\0';
sendername[strlen(sendername)] = '\0';
return 0;
}
eat_header(char *str)
{
/*
This attempts to determine if we are reading headers. It is
not foolproof -- if something looks like a header but really
isn't, it won't get into the delivered message. As long as the
message is in the standard format (headers, blank line, text)
it'll be ok.
*/
static int lastheader = HEADER_NONE;
int header, is_cont;
char *nl, *htext;
if (str[0] == '\n' || str[0] == '\0') return 0;
/* Chop newline */
nl = strchr(str, '\n');
if (nl != NULL) *nl = '\0';
if (str[0] == ' ' || str[0] == '\t') {
header = lastheader;
for (htext = str; *htext == ' ' || *htext == '\t'; htext++) ;
is_cont = 1;
}
else {
htext = identify_header(str, &header);
is_cont = 0;
}
lastheader = header;
switch (header) {
case HEADER_NONE: return 0;
case HEADER_OTHER: break;
case HEADER_SUBJECT:
if (!is_cont) strncpy(subject, htext, sizeof subject - 1);
break;
case HEADER_FROM:
case HEADER_REPLYTO:
if (!is_cont) split_sender(header, htext);
break;
}
return 1;
}
show_failures( int indx, char *userid, LONG *mask )
{
if ((*mask) & (LONG)(1<<indx)) {
fprintf(stderr, "%s: bbs_mail failed\n", userid);
}
return S_OK;
}
usage(char *prog)
{
if (daemon)
fprintf(stderr, "Usage: %s [-d bbs-dir] user ...\n", prog);
else
fprintf(stderr, "Usage: %s [-d bbs-dir] [-s subject] user ...\n",
prog);
}
main( int argc, char *argv[] )
{
char *bbshome = NULL, *prog;
char copybuf[256];
int c, rc;
int in_header = 1;
FILE *fp;
NAMELIST userid_list;
LONG failmask;
/* Prefix sender with INTERNET: */
strcpy(sender, MAILER_PREFIX);
strcat(sender, ":");
sptr = sender + strlen(sender);
ssize = sizeof(sender) - strlen(sender);
/* Find out if we are bbsmail or bbsmaild */
prog = strrchr(argv[0], '/');
if (prog == NULL) prog = argv[0];
else prog++;
if (!strcmp(prog, "bbsmail")) {
/* We do not expect headers. Get sender info from passwd file. */
struct passwd *pw = getpwuid(getuid());
if (pw != NULL) {
char *comma;
strncpy(sptr, pw->pw_name, ssize);
strncpy(sendername, pw->pw_gecos, sizeof(sendername)+1);
if ((comma = strchr(sendername, ',')) != NULL) *comma = '\0';
}
daemon = in_header = 0;
}
/* Scan argv for option flags */
while ((c = getopt(argc, argv, "d:s:?")) != -1)
{
switch (c)
{
case 'd':
bbshome = optarg;
break;
case 's':
if (daemon) {
usage(prog);
return 2;
}
strncpy(subject, optarg, sizeof(subject)-1);
break;
case '?':
usage(prog);
return 2;
}
}
/* Must have at least one recipient */
if (optind >= argc) {
usage(prog);
return 2;
}
/* Form recipients into namelist */
userid_list = NULL;
while (optind < argc) {
if (!is_in_namelist(userid_list, argv[optind]))
add_namelist(&userid_list, argv[optind], NULL);
optind++;
}
/* Home and initialize bbs library */
if (home_bbs(bbshome) == -1) {
fprintf(stderr, "%s: Cannot chdir to %s\n", prog, bbshome);
return 1;
}
if (local_bbs_initialize(NULL) != S_OK) {
fprintf(stderr, "%s: local_bbs_initialize failed\n", prog);
return 1;
}
/* Identify ourself for the log file */
strcpy(user_params.u.userid, "[bbsmail]");
user_params.perms = ~0;
/* Copy message to temporary file, eating headers first */
fp = fopen(server.tempfile, "w");
if (fp == NULL) {
free_namelist(&userid_list);
fprintf(stderr, "%s: ", prog);
perror("spool file open failed");
return 1;
}
while (fgets(copybuf, sizeof copybuf, stdin) != NULL) {
if (in_header) {
in_header = eat_header(copybuf);
}
if (!in_header) fputs(copybuf, fp);
}
fclose(fp);
/* Make sure we know who the sender is */
if (*sptr == '\0') {
free_namelist(&userid_list);
remove(server.tempfile);
fprintf(stderr, "%s: cannot determine sender\n", prog);
return 1;
}
/* Now mail it and let our caller know what happened */
rc = local_bbs_mail(sender, sendername, userid_list, subject,
server.tempfile, &failmask);
if (rc != S_OK) {
apply_namelist(userid_list, show_failures, &failmask);
if (rc == S_NOSUCHUSER)
fprintf(stderr, "bbs_mail: No such user\n");
else if (rc == S_CANNOTMAIL)
fprintf(stderr, "bbs_mail: User cannot receive Internet mail\n");
}
/* That's all, folks */
free_namelist(&userid_list);
remove(server.tempfile);
local_bbs_disconnect();
return (rc == S_OK ? 0 : 1);
}