home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / aix-rs6000 / elm2.3.11.AIX3.1.5.Z / elm2.3.11.AIX3.1.5 / src / fileio.c < prev    next >
C/C++ Source or Header  |  1990-10-07  |  12KB  |  433 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: fileio.c,v 4.1.1.1 90/10/07 19:48:08 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1.1.1 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    fileio.c,v $
  17.  * Revision 4.1.1.1  90/10/07  19:48:08  syd
  18.  * fix the bounce problem reported earlier when using MMDF submit as the MTA.
  19.  * From: Jim Clausing <jac%brahms.tinton.ccur.com@RELAY.CS.NET>
  20.  * 
  21.  * Revision 4.1  90/04/28  22:43:06  syd
  22.  * checkin of Elm 2.3 as of Release PL0
  23.  * 
  24.  *
  25.  ******************************************************************************/
  26.  
  27. /** File I/O routines, including deletion from the folder! 
  28.  
  29. **/
  30.  
  31. #include "headers.h"
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <ctype.h>
  35. #include <errno.h>
  36.  
  37. #ifdef BSD
  38. #undef tolower
  39. #endif
  40.  
  41. extern int errno;
  42.  
  43. char *error_name(), *index();
  44.  
  45. copy_message(prefix, 
  46.          dest_file, 
  47.          remove_header, 
  48.          remote, 
  49.          update_status, 
  50.          mmdf_head,
  51.          remail)
  52. char *prefix;
  53. FILE *dest_file;
  54. int  remove_header, remote, update_status, mmdf_head, remail;
  55. {
  56.     /** Copy current message to destination file, with optional 'prefix' 
  57.         as the prefix for each line.  If remove_header is true, it will 
  58.         skip lines in the message until it finds the end of header line...
  59.         then it will start copying into the file... If remote is true
  60.         then it will append "remote from <hostname>" at the end of the
  61.         very first line of the file (for remailing) 
  62.  
  63.         If "forwarding" is true then it'll do some nice things to
  64.         ensure that the forwarded message looks pleasant; e.g. remove
  65.         stuff like ">From " lines and "Received:" lines.
  66.  
  67.         If "update_status" is true then it will write a new Status:
  68.         line at the end of the headers.  It never copies an existing one.
  69.     **/
  70.  
  71.     char buffer[SLEN];
  72.     register struct header_rec *current_header = headers[current-1];
  73.     register int  lines, front_line, next_front,
  74.           in_header = 1, first_line = TRUE, ignoring = FALSE;
  75.     int    end_header = 0;
  76.     int sender_added = 0;
  77.  
  78.       /** get to the first line of the message desired **/
  79.  
  80.     if (fseek(mailfile, current_header->offset, 0) == -1) {
  81.        dprint(1, (debugfile, 
  82.         "ERROR: Attempt to seek %d bytes into file failed (%s)",
  83.         current_header->offset, "copy_message"));
  84.        error1("ELM [seek] failed trying to read %d bytes into file.",
  85.          current_header->offset);
  86.        return;
  87.     }
  88.  
  89.     /* how many lines in message? */
  90.  
  91.     lines = current_header->lines;
  92.  
  93.     /* set up for forwarding just in case... */
  94.  
  95.     if (forwarding)
  96.       remove_header = FALSE;
  97.  
  98.     /* now while not EOF & still in message... copy it! */
  99.  
  100.     next_front = TRUE;
  101.  
  102.     while (lines) {
  103.       if (fgets(buffer, SLEN, mailfile) == NULL)
  104.         break;
  105.  
  106.       front_line = next_front;
  107.  
  108.       if(buffer[strlen(buffer)-1] == '\n') {
  109.     lines--;    /* got a full line */
  110.     next_front = TRUE;
  111.       }
  112.       else
  113.     next_front = FALSE;
  114.       
  115.       if (front_line && ignoring)
  116.     ignoring = whitespace(buffer[0]);
  117.  
  118.       if (ignoring)
  119.     continue;
  120.  
  121. #ifdef MMDF
  122.       if (mmdf_head && strcmp(buffer, MSG_SEPERATOR) == 0)
  123.     continue;
  124. #endif /* MMDF */
  125.  
  126.       /* are we still in the header? */
  127.  
  128.       if (in_header && front_line) {
  129.     if (strlen(buffer) < 2) {
  130.       in_header = 0;
  131.       end_header = -1;
  132.       if (remail && !sender_added) {
  133.         if (fprintf(dest_file, "%sSender: %s\n", prefix, username) == EOF) {
  134.           Write_to_screen("\n\rWrite in copy_message failed\n\r", 0);
  135.           dprint(1, (debugfile,"\n*** Fprint failed on copy_message;\n"));
  136.           rm_temps_exit();
  137.         }
  138.       }
  139.     }
  140.     else if (!isspace(*buffer)
  141.           && index(buffer, ':') == NULL
  142. #ifdef MMDF
  143.           && strcmp(buffer, MSG_SEPERATOR) != 0
  144. #endif /* MMDF */
  145.         ) {
  146.       in_header = 0;
  147.       end_header = 1;
  148.       if (remail && !sender_added) {
  149.         if (fprintf(dest_file, "%sSender: %s\n", prefix, username) == EOF) {
  150.           Write_to_screen("\n\rWrite in copy_message failed\n\r", 0);
  151.           dprint(1, (debugfile,"\n*** Fprint failed on copy_message;\n"));
  152.           rm_temps_exit();
  153.         }
  154.       }
  155.     } else if (in_header && remote && first_word(buffer, "Sender:")) {
  156.       if (remail)
  157.         if (fprintf(dest_file, "%sSender: %s\n", prefix, username) == EOF) {
  158.           Write_to_screen("\n\rWrite in copy_message failed\n\r", 0);
  159.           dprint(1, (debugfile,"\n*** Fprint failed on copy_message;\n"));
  160.           rm_temps_exit();
  161.         }
  162.       sender_added = TRUE;
  163.       continue;
  164.     }
  165.     if (end_header) {
  166.       if (update_status) {
  167.           if (isoff(current_header->status, NEW)) {
  168.         if (ison(current_header->status, UNREAD)) {
  169.           if (fprintf(dest_file, "%sStatus: O\n", prefix) == EOF) {
  170.             Write_to_screen("\n\rWrite in copy_message failed\n\r", 0);
  171.             dprint(1, (debugfile,"\n*** Fprint failed on copy_message;\n"));
  172.             rm_temps_exit();
  173.           }
  174.         } else    /* read */
  175. #ifdef BSD
  176.           if (fprintf(dest_file, "%sStatus: OR\n", prefix) == EOF) {
  177. #else
  178.           if (fprintf(dest_file, "%sStatus: RO\n", prefix) == EOF) {
  179. #endif
  180.             Write_to_screen("\n\rWrite in copy_message failed\n\r", 0);
  181.             dprint(1, (debugfile,"\n*** Fprint failed on copy_message;\n"));
  182.             rm_temps_exit();
  183.           }
  184.         update_status = FALSE; /* do it only once */
  185.           }    /* else if NEW - indicate NEW with no Status: line. This is
  186.          * important if we resync a mailfile - we don't want
  187.          * NEW status lost when we copy each message out.
  188.          * It is the responsibility of the function that calls
  189.          * this function to unset NEW as appropriate to its
  190.          * reason for using this function to copy a message
  191.          */
  192.  
  193.         /*
  194.          * add the missing newline for RFC 822
  195.          */
  196.           if (end_header > 0) {
  197.         /* add the missing newline for RFC 822 */
  198.         if (fprintf(dest_file, "\n") == EOF) {
  199.           Write_to_screen("\n\rWrite in copy_message failed\n\r", 0);
  200.           dprint(1, (debugfile,"\n*** Fprint failed on copy_message;\n"));
  201.           rm_temps_exit();
  202.         }
  203.           }
  204.       }
  205.     }
  206.       }
  207.  
  208.       if (in_header) {
  209.     /* Process checks while in header area */
  210.  
  211.     if (remove_header) {
  212.       ignoring = TRUE;
  213.       continue;
  214.     }
  215.  
  216.     /* add remote on to front? */
  217.     if (first_line && remote) {
  218.       no_ret(buffer);
  219. #ifndef MMDF
  220.       if (fprintf(dest_file, "%s%s remote from %s\n",
  221.           prefix, buffer, hostname) == EOF) {
  222.         Write_to_screen("\n\rWrite in copy_message failed\n\r", 0);
  223.         dprint(1, (debugfile,"\n*** Fprint failed on copy_message;\n"));
  224.         rm_temps_exit();
  225.       }
  226. #else
  227.       if (first_word(buffer, "From ")) {
  228.         if (fprintf(dest_file, "%s%s remote from %s\n",
  229.             prefix, buffer, hostname) == EOF) {
  230.         Write_to_screen("\n\rWrite in copy_message failed\n\r", 0);
  231.         dprint(1, (debugfile,"\n*** Fprint failed on copy_message;\n"));
  232.         rm_temps_exit();
  233.         }
  234.       } else {
  235.         if (fprintf(dest_file, "%s%s\n", prefix, buffer) == EOF) {
  236.         Write_to_screen("\n\rWrite in copy_message failed\n\r", 0);
  237.         dprint(1, (debugfile,"\n*** Fprint failed on copy_message;\n"));
  238.         rm_temps_exit();
  239.         }
  240.       }
  241. #endif /* MMDF */
  242.       first_line = FALSE;
  243.       continue;
  244.     }
  245.  
  246.     if (!forwarding) {
  247.       if(! first_word(buffer, "Status:")) {
  248.         if (fprintf(dest_file, "%s%s", prefix, buffer) == EOF) {
  249.               dprint(1, (debugfile,"\n*** Fprint failed on copy_message;\n"));
  250.           rm_temps_exit();
  251.           }
  252.         continue;
  253.       } else {
  254.         ignoring = TRUE;
  255.         continue;    /* we will output a new Status: line later, if desired. */
  256.       }
  257.     }
  258.     else { /* forwarding */
  259.  
  260.       if (first_word(buffer, "Received:"   ) ||
  261.           first_word(buffer, ">From"       ) ||
  262.           first_word(buffer, "Status:"     ) ||
  263.           first_word(buffer, "Return-Path:"))
  264.           ignoring = TRUE;
  265.       else
  266.         if (remail && first_word(buffer, "To:")) {
  267.           if (fprintf(dest_file, "%sOrig-%s", prefix, buffer) == EOF) {
  268.                 dprint(1, (debugfile,"\n*** Fprint failed on copy_message;\n"));
  269.             rm_temps_exit();
  270.           }
  271.         } else {
  272.           if (fprintf(dest_file, "%s%s", prefix, buffer) == EOF) {
  273.                 dprint(1, (debugfile,"\n*** Fprint failed on copy_message;\n"));
  274.             rm_temps_exit();
  275.           }
  276.         }
  277.     }
  278.       }
  279.       else { /* not in header */
  280.         /* Process checks that occur after the header area */
  281.  
  282. #ifndef MMDF
  283.     if(first_word(buffer, "From ") && (real_from(buffer, NULL))) {
  284.       dprint(1, (debugfile,
  285.          "\n*** Internal Problem...Tried to add the following;\n"));
  286.       dprint(1, (debugfile,
  287.          "  '%s'\nto output file (copy_message) ***\n", buffer));
  288.       break;    /* STOP NOW! */
  289.     }
  290. #endif /* MMDF */
  291.  
  292.     if (fprintf(dest_file, "%s%s", prefix, buffer) == EOF) {
  293.       Write_to_screen("\n\rWrite in copy_message failed\n\r", 0);
  294.       dprint(1, (debugfile,"\n*** Fprint failed on copy_message;\n"));
  295.       rm_temps_exit();
  296.     }
  297.       }
  298.     }
  299. #ifndef MMDF
  300.     if (strlen(buffer) + strlen(prefix) > 1)
  301.     if (fprintf(dest_file, "\n") == EOF) {    /* blank line to keep mailx happy *sigh* */
  302.       Write_to_screen("\n\rWrite in copy_message failed\n\r", 0);
  303.       dprint(1, (debugfile,"\n*** Fprint failed on copy_message;\n"));
  304.       rm_temps_exit();
  305.     }
  306. #endif /* MMDF */
  307. }
  308.  
  309. static struct stat saved_buf;
  310. static char saved_fname[SLEN];
  311.  
  312. int
  313. save_file_stats(fname)
  314. char *fname;
  315. {
  316.     /* if fname exists, save the owner, group, mode and filename.
  317.      * otherwise flag nothing saved. Return 0 if saved, else -1.
  318.      */
  319.  
  320.     if(stat(fname, &saved_buf) != -1) {
  321.       (void)strcpy(saved_fname, fname);
  322.       dprint(2, (debugfile,
  323.         "** saved stats for file owner = %d group = %d mode = %o %s **\n",
  324.         saved_buf.st_uid, saved_buf.st_gid, saved_buf.st_mode, fname));
  325.       return(0);
  326.     }
  327.     dprint(2, (debugfile,
  328.       "** couldn't save stats for file %s [errno=%d] **\n",
  329.       fname, errno));
  330.     return(-1);
  331.  
  332. }
  333.  
  334. restore_file_stats(fname)
  335. char *fname;
  336. {
  337.     /* if fname matches the saved file name, set the owner and group
  338.      * of fname to the saved owner, group and mode,
  339.      * else to the userid and groupid of the user and to 700.
  340.      * Return    -1 if the  either mode or owner/group not set
  341.      *        0 if the default values were used
  342.      *        1 if the saved values were used
  343.      */
  344.  
  345.     int old_umask, i, new_mode, new_owner, new_group, ret_code;
  346.  
  347.  
  348.     new_mode = 0600;
  349.     new_owner = userid;
  350.     new_group = groupid;
  351.     ret_code = 0;
  352.  
  353.     if(strcmp(fname, saved_fname) == 0) {
  354.       new_mode = saved_buf.st_mode;
  355.       new_owner = saved_buf.st_uid;
  356.       new_group = saved_buf.st_gid;
  357.       ret_code = 1;
  358.     }
  359.     dprint(2, (debugfile, "** %s file stats for %s **\n",
  360.       (ret_code ? "restoring" : "setting"), fname));
  361.  
  362.     old_umask = umask(0);
  363.     if((i = chmod(fname, new_mode & 0777)) == -1)
  364.       ret_code = -1;
  365.  
  366.     dprint(2, (debugfile, "** chmod(%s, %.3o) returns %d [errno=%d] **\n",
  367.         fname, new_mode & 0777, i, errno));
  368.  
  369.     (void) umask(old_umask);
  370.  
  371. #ifdef    BSD
  372.     /*
  373.      * Chown is restricted to root on BSD unix
  374.      */
  375.     (void) chown(fname, new_owner, new_group);
  376. #else
  377.     if((i = chown(fname, new_owner, new_group)) == -1)
  378.       ret_code = -1;
  379.  
  380.     dprint(2, (debugfile, "** chown(%s, %d, %d) returns %d [errno=%d] **\n",
  381.            fname, new_owner, new_group, i, errno));
  382. #endif
  383.  
  384.     return(ret_code);
  385.  
  386. }
  387.  
  388. /** and finally, here's something for that evil trick: site hiding **/
  389.  
  390. #ifdef SITE_HIDING
  391.  
  392. int
  393. is_a_hidden_user(specific_username)
  394. char *specific_username;
  395. {
  396.     /** Returns true iff the username is present in the list of
  397.        'hidden users' on the system.
  398.     **/
  399.     
  400.     FILE *hidden_users;
  401.     char  buffer[SLEN];
  402.  
  403.         /* 
  404.     this line is deliberately inserted to ensure that you THINK
  405.     about what you're doing, and perhaps even contact the author
  406.     of Elm before you USE this option...
  407.         */
  408.  
  409.     if ((hidden_users = fopen (HIDDEN_SITE_USERS,"r")) == NULL) {
  410.       dprint(1, (debugfile,
  411.           "Couldn't open hidden site file %s [%s]\n",
  412.           HIDDEN_SITE_USERS, error_name(errno)));
  413.       return(FALSE);
  414.     }
  415.  
  416.     while (fscanf(hidden_users, "%s", buffer) != EOF)
  417.       if (strcmp(buffer, specific_username) == 0) {
  418.         dprint(3, (debugfile, "** Found user '%s' in hidden site file!\n",
  419.             specific_username));
  420.         fclose(hidden_users);
  421.         return(TRUE);
  422.       }
  423.  
  424.     fclose(hidden_users);
  425.     dprint(3, (debugfile, 
  426.         "** Couldn't find user '%s' in hidden site file!\n",
  427.         specific_username));
  428.  
  429.     return(FALSE);
  430. }
  431.  
  432. #endif
  433.