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
|
/*
* Copyright (c) 1989 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
/*
* code for mh compatibility (tom)
*/
#ifndef lint
static char copyright[] = "Copyright (c) 1990 Regents of the University of California.\nAll rights reserved.\n";
static char SccsId[] = "@(#)pop_updt.c 1.9 8/16/90";
#endif not lint
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/file.h>
#include "popper.h"
extern int errno;
static char standard_error[] =
"Error updating primary drop. Mailbox unchanged.";
/*
* updt: Apply changes to a user's POP maildrop
*/
int pop_updt (p)
POP * p;
{
FILE * md; /* Stream pointer for
the user's maildrop */
int mfd; /* File descriptor for
above */
char buffer[BUFSIZ]; /* Read buffer */
MsgInfoList * mp; /* Pointer to message
info list */
register int msg_num; /* Current message
counter */
register int status_written; /* Status header field
written */
int nchar; /* Bytes read/written */
long offset; /* New mail offset */
int begun; /* Sanity check */
#ifdef DEBUG
if (p->debug) {
pop_log(p,POP_DEBUG,"Performing maildrop update...");
pop_log(p,POP_DEBUG,"Checking to see if all messages were deleted");
}
#endif DEBUG
if (p->msgs_deleted == p->msg_count) {
/* Truncate before close, to avoid race condition, DO NOT UNLINK!
Another process may have opened, and not yet tried to lock */
(void)ftruncate ((int)fileno(p->drop),0);
(void)fclose(p->drop) ;
return (POP_SUCCESS);
}
#ifdef DEBUG
if (p->debug)
pop_log(p,POP_DEBUG,"Opening mail drop \"%s\"",p->drop_name);
#endif DEBUG
/* Open the user's real maildrop */
if (((mfd = open(p->drop_name,O_RDWR|O_CREAT,0666)) == -1 ) ||
((md = fdopen(mfd,"r+")) == NULL)) {
return pop_msg(p,POP_FAILURE,"%s %s",
p->drop_name, sys_errlist[errno]);
}
/* Lock the user's real mail drop */
if ( flock(mfd,LOCK_EX) == -1 ) {
(void)fclose(md) ;
return pop_msg(p,POP_FAILURE, "flock: '%s': %s", p->temp_drop,
(errno < sys_nerr) ? sys_errlist[errno] : "");
}
/* Go to the right places */
offset = lseek((int)fileno(p->drop),0,L_XTND) ;
/* Append any messages that may have arrived during the session
to the temporary maildrop */
while ((nchar=read(mfd,buffer,BUFSIZ)) > 0)
if ( nchar != write((int)fileno(p->drop),buffer,nchar) ) {
nchar = -1;
break ;
}
if ( nchar != 0 ) {
(void)fclose(md) ;
(void)ftruncate((int)fileno(p->drop),(int)offset) ;
(void)fclose(p->drop) ;
return pop_msg(p,POP_FAILURE,standard_error);
}
rewind(md);
(void)ftruncate(mfd,0) ;
/* Synch stdio and the kernel for the POP drop */
rewind(p->drop);
(void)lseek((int)fileno(p->drop),0,L_SET);
/* Transfer messages not flagged for deletion from the temporary
maildrop to the new maildrop */
#ifdef DEBUG
if (p->debug)
pop_log(p,POP_DEBUG,"Creating new maildrop \"%s\" from \"%s\"",
p->drop_name,p->temp_drop);
#endif DEBUG
for (msg_num = 0; msg_num < p->msg_count; ++msg_num) {
int doing_body;
/* Get a pointer to the message information list */
mp = &p->mlp[msg_num];
if (mp->del_flag) {
#ifdef DEBUG
if(p->debug)
pop_log(p,POP_DEBUG,
"Message %d flagged for deletion.",mp->number);
#endif DEBUG
continue;
}
(void)fseek(p->drop,mp->offset,0);
#ifdef DEBUG
if(p->debug)
pop_log(p,POP_DEBUG,"Copying message %d.",mp->number);
#endif DEBUG
begun = 0;
for(status_written = doing_body = 0 ;
fgets(buffer,MAXMSGLINELEN,p->drop);) {
if (doing_body == 0) { /* Header */
/* panic, I'm tired and can't think contorted. */
if(is_msg_boundary(buffer) && begun) {
pop_log(p, POP_ERROR,
"%s: mailbox detonation has begun!", p->user);
(void)ftruncate(mfd,0);
(void)fclose(md);
(void)fclose(p->drop);
return(pop_msg(p, POP_FAILURE, "Unable to close mailbox door. Contact the postmaster to repair it."));
}
begun = 1;
/* Update the message status */
if (strncasecmp(buffer,"Status:",7) == 0) {
if (mp->retr_flag)
(void)fputs("Status: RO\n",md);
else
(void)fputs(buffer, md);
status_written++;
continue;
}
/* A blank line signals the end of the header. */
if (*buffer == '\n') {
doing_body = 1;
#ifndef NOSTATUS
if (status_written == 0) {
if (mp->retr_flag)
(void)fputs("Status: RO\n\n",md);
else
(void)fputs("Status: U\n\n",md);
}
else
#endif /* NOSTATUS */
(void)fputs ("\n", md);
continue;
}
/* Save another header line */
(void)fputs (buffer, md);
}
else { /* Body */
if (strncmp(buffer,"\001\001\001\001",4) == 0) {
(void)fputs (buffer, md);
break;
}
if(is_msg_boundary(buffer))
break;
(void)fputs (buffer, md);
}
}
}
/* flush and check for errors now! The new mail will writen
without stdio, since we need not separate messages */
(void)fflush(md) ;
if (ferror(md)) {
(void)ftruncate(mfd,0) ;
(void)fclose(md) ;
(void)fclose(p->drop) ;
return pop_msg(p,POP_FAILURE,standard_error);
}
/* Go to start of new mail if any */
(void)lseek((int)fileno(p->drop),offset,L_SET);
while((nchar=read((int)fileno(p->drop),buffer,BUFSIZ)) > 0)
if ( nchar != write(mfd,buffer,nchar) ) {
nchar = -1;
break ;
}
if ( nchar != 0 ) {
(void)ftruncate(mfd,0) ;
(void)fclose(md) ;
(void)fclose(p->drop) ;
return pop_msg(p,POP_FAILURE,standard_error);
}
/* Close the maildrop and empty temporary maildrop */
(void)fclose(md);
(void)ftruncate((int)fileno(p->drop),0);
(void)fclose(p->drop);
return(pop_quit(p));
}
is_msg_boundary(line)
char *line;
{
if(strncmp(line, "\001\001\001\001", 4) == 0)
return(1);
if(strncmp(line, "From ", 5) != 0)
return(0);
line += 5;
while((*line != ' ') && (*line != '\0'))
++line;
if(*line++ != ' ')
return(0);
if(strlen(line) < 28)
return(0);
/* Tue */
line += 3;
if(*line++ != ' ')
return(0);
/* Jan */
line += 3;
if(*line++ != ' ')
return(0);
/* 22 */
line += 2;
if(*line++ != ' ')
return(0);
/* 18:21:34 */
line += 2;
if(*line++ != ':')
return(0);
line += 2;
if(*line++ != ':')
return(0);
line += 2;
if(*line++ != ' ')
return(0);
return(1);
}
|