1 /* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef lint 31 #if 0 32 static char sccsid[] = "@(#)fio.c 8.2 (Berkeley) 4/20/95"; 33 #endif 34 #endif /* not lint */ 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include "rcv.h" 39 #include <sys/file.h> 40 #include <sys/wait.h> 41 42 #include <unistd.h> 43 #include <paths.h> 44 #include <errno.h> 45 #include "extern.h" 46 47 /* 48 * Mail -- a mail program 49 * 50 * File I/O. 51 */ 52 53 extern int wait_status; 54 55 /* 56 * Set up the input pointers while copying the mail file into /tmp. 57 */ 58 void 59 setptr(FILE *ibuf, off_t offset) 60 { 61 int c, count; 62 char *cp, *cp2; 63 struct message this; 64 FILE *mestmp; 65 int maybe, inhead; 66 char linebuf[LINESIZE], pathbuf[PATHSIZE]; 67 int omsgCount; 68 69 /* Get temporary file. */ 70 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/mail.XXXXXXXXXX", tmpdir); 71 if ((c = mkstemp(pathbuf)) == -1 || (mestmp = Fdopen(c, "r+")) == NULL) 72 err(1, "can't open %s", pathbuf); 73 (void)rm(pathbuf); 74 75 if (offset == 0) { 76 msgCount = 0; 77 } else { 78 /* Seek into the file to get to the new messages */ 79 (void)fseeko(ibuf, offset, SEEK_SET); 80 /* 81 * We need to make "offset" a pointer to the end of 82 * the temp file that has the copy of the mail file. 83 * If any messages have been edited, this will be 84 * different from the offset into the mail file. 85 */ 86 (void)fseeko(otf, (off_t)0, SEEK_END); 87 offset = ftello(otf); 88 } 89 omsgCount = msgCount; 90 maybe = 1; 91 inhead = 0; 92 this.m_flag = MUSED|MNEW; 93 this.m_size = 0; 94 this.m_lines = 0; 95 this.m_block = 0; 96 this.m_offset = 0; 97 for (;;) { 98 if (fgets(linebuf, sizeof(linebuf), ibuf) == NULL) { 99 if (append(&this, mestmp)) 100 errx(1, "temporary file"); 101 makemessage(mestmp, omsgCount); 102 return; 103 } 104 count = strlen(linebuf); 105 /* 106 * Transforms lines ending in <CR><LF> to just <LF>. 107 * This allows mail to be able to read Eudora mailboxes. 108 */ 109 if (count >= 2 && linebuf[count - 1] == '\n' && 110 linebuf[count - 2] == '\r') { 111 count--; 112 linebuf[count - 1] = '\n'; 113 } 114 115 (void)fwrite(linebuf, sizeof(*linebuf), count, otf); 116 if (ferror(otf)) 117 errx(1, "/tmp"); 118 if (count) 119 linebuf[count - 1] = '\0'; 120 if (maybe && linebuf[0] == 'F' && ishead(linebuf)) { 121 msgCount++; 122 if (append(&this, mestmp)) 123 errx(1, "temporary file"); 124 this.m_flag = MUSED|MNEW; 125 this.m_size = 0; 126 this.m_lines = 0; 127 this.m_block = blockof(offset); 128 this.m_offset = boffsetof(offset); 129 inhead = 1; 130 } else if (linebuf[0] == 0) { 131 inhead = 0; 132 } else if (inhead) { 133 for (cp = linebuf, cp2 = "status";; cp++) { 134 if ((c = *cp2++) == '\0') { 135 while (isspace((unsigned char)*cp++)) 136 ; 137 if (cp[-1] != ':') 138 break; 139 while ((c = *cp++) != '\0') 140 if (c == 'R') 141 this.m_flag |= MREAD; 142 else if (c == 'O') 143 this.m_flag &= ~MNEW; 144 inhead = 0; 145 break; 146 } 147 if (*cp != c && *cp != toupper((unsigned char)c)) 148 break; 149 } 150 } 151 offset += count; 152 this.m_size += count; 153 this.m_lines++; 154 maybe = linebuf[0] == 0; 155 } 156 } 157 158 /* 159 * Drop the passed line onto the passed output buffer. 160 * If a write error occurs, return -1, else the count of 161 * characters written, including the newline if requested. 162 */ 163 int 164 putline(FILE *obuf, char *linebuf, int outlf) 165 { 166 int c; 167 168 c = strlen(linebuf); 169 (void)fwrite(linebuf, sizeof(*linebuf), c, obuf); 170 if (outlf) { 171 fprintf(obuf, "\n"); 172 c++; 173 } 174 if (ferror(obuf)) 175 return (-1); 176 return (c); 177 } 178 179 /* 180 * Read up a line from the specified input into the line 181 * buffer. Return the number of characters read. Do not 182 * include the newline (or carriage return) at the end. 183 */ 184 int 185 readline(FILE *ibuf, char *linebuf, int linesize) 186 { 187 int n; 188 189 clearerr(ibuf); 190 if (fgets(linebuf, linesize, ibuf) == NULL) 191 return (-1); 192 n = strlen(linebuf); 193 if (n > 0 && linebuf[n - 1] == '\n') 194 linebuf[--n] = '\0'; 195 if (n > 0 && linebuf[n - 1] == '\r') 196 linebuf[--n] = '\0'; 197 return (n); 198 } 199 200 /* 201 * Return a file buffer all ready to read up the 202 * passed message pointer. 203 */ 204 FILE * 205 setinput(struct message *mp) 206 { 207 208 (void)fflush(otf); 209 if (fseeko(itf, 210 positionof(mp->m_block, mp->m_offset), SEEK_SET) < 0) 211 err(1, "fseeko"); 212 return (itf); 213 } 214 215 /* 216 * Take the data out of the passed ghost file and toss it into 217 * a dynamically allocated message structure. 218 */ 219 void 220 makemessage(FILE *f, int omsgCount) 221 { 222 size_t size; 223 struct message *nmessage; 224 225 size = (msgCount + 1) * sizeof(struct message); 226 nmessage = (struct message *)realloc(message, size); 227 if (nmessage == NULL) 228 errx(1, "Insufficient memory for %d messages\n", 229 msgCount); 230 if (omsgCount == 0 || message == NULL) 231 dot = nmessage; 232 else 233 dot = nmessage + (dot - message); 234 message = nmessage; 235 size -= (omsgCount + 1) * sizeof(struct message); 236 (void)fflush(f); 237 (void)lseek(fileno(f), (off_t)sizeof(*message), 0); 238 if (read(fileno(f), (char *)&message[omsgCount], size) != size) 239 errx(1, "Message temporary file corrupted"); 240 message[msgCount].m_size = 0; 241 message[msgCount].m_lines = 0; 242 (void)Fclose(f); 243 } 244 245 /* 246 * Append the passed message descriptor onto the temp file. 247 * If the write fails, return 1, else 0 248 */ 249 int 250 append(struct message *mp, FILE *f) 251 { 252 return (fwrite((char *)mp, sizeof(*mp), 1, f) != 1); 253 } 254 255 /* 256 * Delete a file, but only if the file is a plain file. 257 */ 258 int 259 rm(char *name) 260 { 261 struct stat sb; 262 263 if (stat(name, &sb) < 0) 264 return (-1); 265 if (!S_ISREG(sb.st_mode)) { 266 errno = EISDIR; 267 return (-1); 268 } 269 return (unlink(name)); 270 } 271 272 static int sigdepth; /* depth of holdsigs() */ 273 static sigset_t nset, oset; 274 /* 275 * Hold signals SIGHUP, SIGINT, and SIGQUIT. 276 */ 277 void 278 holdsigs(void) 279 { 280 281 if (sigdepth++ == 0) { 282 (void)sigemptyset(&nset); 283 (void)sigaddset(&nset, SIGHUP); 284 (void)sigaddset(&nset, SIGINT); 285 (void)sigaddset(&nset, SIGQUIT); 286 (void)sigprocmask(SIG_BLOCK, &nset, &oset); 287 } 288 } 289 290 /* 291 * Release signals SIGHUP, SIGINT, and SIGQUIT. 292 */ 293 void 294 relsesigs(void) 295 { 296 297 if (--sigdepth == 0) 298 (void)sigprocmask(SIG_SETMASK, &oset, NULL); 299 } 300 301 /* 302 * Determine the size of the file possessed by 303 * the passed buffer. 304 */ 305 off_t 306 fsize(FILE *iob) 307 { 308 struct stat sbuf; 309 310 if (fstat(fileno(iob), &sbuf) < 0) 311 return (0); 312 return (sbuf.st_size); 313 } 314 315 /* 316 * Evaluate the string given as a new mailbox name. 317 * Supported meta characters: 318 * % for my system mail box 319 * %user for user's system mail box 320 * # for previous file 321 * & invoker's mbox file 322 * +file file in folder directory 323 * any shell meta character 324 * Return the file name as a dynamic string. 325 */ 326 char * 327 expand(char *name) 328 { 329 char xname[PATHSIZE]; 330 char cmdbuf[PATHSIZE]; /* also used for file names */ 331 int pid, l; 332 char *cp, *sh; 333 int pivec[2]; 334 struct stat sbuf; 335 336 /* 337 * The order of evaluation is "%" and "#" expand into constants. 338 * "&" can expand into "+". "+" can expand into shell meta characters. 339 * Shell meta characters expand into constants. 340 * This way, we make no recursive expansion. 341 */ 342 switch (*name) { 343 case '%': 344 findmail(name[1] ? name + 1 : myname, xname, sizeof(xname)); 345 return (savestr(xname)); 346 case '#': 347 if (name[1] != 0) 348 break; 349 if (prevfile[0] == 0) { 350 printf("No previous file\n"); 351 return (NULL); 352 } 353 return (savestr(prevfile)); 354 case '&': 355 if (name[1] == 0 && (name = value("MBOX")) == NULL) 356 name = "~/mbox"; 357 /* fall through */ 358 } 359 if (name[0] == '+' && getfold(cmdbuf, sizeof(cmdbuf)) >= 0) { 360 (void)snprintf(xname, sizeof(xname), "%s/%s", cmdbuf, name + 1); 361 name = savestr(xname); 362 } 363 /* catch the most common shell meta character */ 364 if (name[0] == '~' && homedir != NULL && 365 (name[1] == '/' || name[1] == '\0')) { 366 (void)snprintf(xname, sizeof(xname), "%s%s", homedir, name + 1); 367 name = savestr(xname); 368 } 369 if (!strpbrk(name, "~{[*?$`'\"\\")) 370 return (savestr(name)); 371 if (pipe(pivec) < 0) { 372 warn("pipe"); 373 return (NULL); 374 } 375 (void)snprintf(cmdbuf, sizeof(cmdbuf), "echo %s", name); 376 if ((sh = value("SHELL")) == NULL) 377 sh = _PATH_CSHELL; 378 pid = start_command(sh, 0, -1, pivec[1], "-c", cmdbuf, NULL); 379 if (pid < 0) { 380 (void)close(pivec[0]); 381 (void)close(pivec[1]); 382 return (NULL); 383 } 384 (void)close(pivec[1]); 385 l = read(pivec[0], xname, BUFSIZ); 386 (void)close(pivec[0]); 387 if (wait_child(pid) < 0 && WIFSIGNALED(wait_status) && 388 WTERMSIG(wait_status) != SIGPIPE) { 389 fprintf(stderr, "\"%s\": Expansion failed.\n", name); 390 return (NULL); 391 } 392 if (l < 0) { 393 warn("read"); 394 return (NULL); 395 } 396 if (l == 0) { 397 fprintf(stderr, "\"%s\": No match.\n", name); 398 return (NULL); 399 } 400 if (l == BUFSIZ) { 401 fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name); 402 return (NULL); 403 } 404 xname[l] = '\0'; 405 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--) 406 ; 407 cp[1] = '\0'; 408 if (strchr(xname, ' ') && stat(xname, &sbuf) < 0) { 409 fprintf(stderr, "\"%s\": Ambiguous.\n", name); 410 return (NULL); 411 } 412 return (savestr(xname)); 413 } 414 415 /* 416 * Determine the current folder directory name. 417 */ 418 int 419 getfold(char *name, int namelen) 420 { 421 char *folder; 422 int copylen; 423 424 if ((folder = value("folder")) == NULL) 425 return (-1); 426 if (*folder == '/') 427 copylen = strlcpy(name, folder, namelen); 428 else 429 copylen = snprintf(name, namelen, "%s/%s", 430 homedir ? homedir : ".", folder); 431 return (copylen < 0 || copylen >= namelen ? (-1) : (0)); 432 } 433 434 /* 435 * Return the name of the dead.letter file. 436 */ 437 char * 438 getdeadletter(void) 439 { 440 char *cp; 441 442 if ((cp = value("DEAD")) == NULL || (cp = expand(cp)) == NULL) 443 cp = expand("~/dead.letter"); 444 else if (*cp != '/') { 445 char buf[PATHSIZE]; 446 447 (void)snprintf(buf, sizeof(buf), "~/%s", cp); 448 cp = expand(buf); 449 } 450 return (cp); 451 } 452