home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / gopher / gopher1.01 / gopherd / serverutil.c < prev    next >
C/C++ Source or Header  |  1992-04-20  |  3KB  |  106 lines

  1. #include "gopherd.h"
  2.  
  3. /* 
  4.  * This routine cleans up an open file descriptor and sends out a bogus
  5.  * filename with the error message
  6.  */
  7.  
  8. void
  9. Abortoutput(sockfd, errmsg)
  10.   int sockfd;
  11.   char *errmsg;
  12. {
  13.      char outputline[256];
  14.      
  15.      sprintf(outputline, "0Server error: %s\t\terror.host\t1\r\n.\r\n", errmsg);
  16.      LOGGopher(sockfd, errmsg);
  17.  
  18.      if (writestring(sockfd, outputline)<0) {
  19.       LOGGopher(sockfd, "Client went away!");
  20.       exit(-1);
  21.      }
  22.      close(sockfd);
  23.  
  24.      return;
  25. }
  26.  
  27.  
  28.  
  29. /*
  30.  * is_mail_from_line - Is this a legal unix mail "From " line?
  31.  *
  32.  * Given a line of input will check to see if it matches the standard
  33.  * unix mail "from " header format. Returns 0 if it does and <0 if not.
  34.  *
  35.  * 2 - Very strict, also checks that each field contains a legal value.
  36.  *
  37.  * Assumptions: Not having the definitive unix mailbox reference I have
  38.  * assumed that unix mailbox headers follow this format:
  39.  *
  40.  * From <person> <date> <garbage>
  41.  *
  42.  * Where <person> is the address of the sender, being an ordinary
  43.  * string with no white space imbedded in it, and <date> is the date of
  44.  * posting, in ctime(3C) format.
  45.  *
  46.  * This would, on the face of it, seem valid. I (Bernd) have yet to find a
  47.  * unix mailbox header which doesn't follow this format.
  48.  *
  49.  * From: Bernd Wechner (bernd@bhpcpd.kembla.oz.au)
  50.  * Obfuscated by: KFS (as usual)
  51.  */
  52.  
  53. #define MAX_FIELDS 10
  54.  
  55. static char legal_day[]         = "SunMonTueWedThuFriSat";
  56. static char legal_month[]       = "JanFebMarAprMayJunJulAugSepOctNovDec";
  57. static int  legal_numbers[]     = { 1, 31, 0, 23, 0, 59, 0, 60, 1969, 2199 };
  58.  
  59. int is_mail_from_line(line)
  60. char *line;     /* Line of text to be checked */
  61. {
  62.     char *fields[MAX_FIELDS];
  63.     char *sender_tail;
  64.     register char *lp, **fp;
  65.     register int n, i;
  66.  
  67.     if (strncmp(line, "From ", 5)) return -100;
  68.  
  69.     lp = line + 5;
  70.     /* sender day mon dd hh:mm:ss year */
  71.     for (n = 0, fp = fields; n < MAX_FIELDS; n++) {
  72.         while (*lp && *lp != '\n' && isascii(*lp) && isspace(*lp)) lp++;
  73.         if (*lp == '\0' || *lp == '\n') break;
  74.         *fp++ = lp;
  75.         while (*lp && isascii(*lp) && !isspace(*lp))
  76.             if (*lp++ == ':' && (n == 4 || n == 5)) break;
  77.         if (n == 0) sender_tail = lp;
  78.    }
  79.  
  80.     if (n < 8) return -200-n;
  81.  
  82.     fp = fields;
  83.  
  84.     if (n > 8 && !isdigit(fp[7][0])) fp[7] = fp[8]; /* ... TZ year */
  85.     if (n > 9 && !isdigit(fp[7][0])) fp[7] = fp[9]; /* ... TZ DST year */
  86.  
  87.     fp++;
  88.     for (i = 0; i < 21; i += 3)
  89.         if (strncmp(*fp, &legal_day[i], 3) == 0) break;
  90.     if (i == 21) return -1;
  91.  
  92.     fp++;
  93.     for (i = 0; i < 36; i += 3)
  94.         if (strncmp(*fp, &legal_month[i], 3) == 0) break;
  95.     if (i == 36) return -2;
  96.  
  97.     for (i = 0; i < 10; i += 2) {
  98.         lp = *++fp;
  99.         if (!isdigit(*lp)) return -20-i;
  100.         n = atoi(lp);
  101.         if (n < legal_numbers[i] || legal_numbers[i+1] < n) return -10-i;
  102.    }
  103.     return 0;
  104. }
  105.  
  106.