home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher1.12 / gopherd / serverutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-21  |  4.0 KB  |  147 lines

  1. /********************************************************************
  2.  * $Author: lindner $
  3.  * $Revision: 1.1 $
  4.  * $Date: 1992/12/10 23:13:27 $
  5.  * $Source: /home/mudhoney/GopherSrc/release1.11/gopherd/RCS/serverutil.c,v $
  6.  * $Status: $
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: serverutil.c
  14.  * utilities for the server.
  15.  *********************************************************************
  16.  * Revision History:
  17.  * $Log: serverutil.c,v $
  18.  * Revision 1.1  1992/12/10  23:13:27  lindner
  19.  * gopher 1.1 release
  20.  *
  21.  *
  22.  *********************************************************************/
  23.  
  24.  
  25.  
  26. #include "gopherd.h"
  27.  
  28. /* 
  29.  * This routine cleans up an open file descriptor and sends out a bogus
  30.  * filename with the error message
  31.  */
  32.  
  33. void
  34. Abortoutput(sockfd, errmsg)
  35.   int sockfd;
  36.   char *errmsg;
  37. {
  38.      char outputline[256];
  39.      
  40.      sprintf(outputline, "0Server error: %s\t\terror.host\t1\r\n.\r\n", errmsg);
  41.      LOGGopher(sockfd, errmsg);
  42.  
  43.      if (writestring(sockfd, outputline)<0) {
  44.       LOGGopher(sockfd, "Client went away!");
  45.       exit(-1);
  46.      }
  47.      close(sockfd);
  48.  
  49.      return;
  50. }
  51.  
  52.  
  53.  
  54. /*
  55.  * is_mail_from_line - Is this a legal unix mail "From " line?
  56.  *
  57.  * Given a line of input will check to see if it matches the standard
  58.  * unix mail "from " header format. Returns 0 if it does and <0 if not.
  59.  *
  60.  * 2 - Very strict, also checks that each field contains a legal value.
  61.  *
  62.  * Assumptions: Not having the definitive unix mailbox reference I have
  63.  * assumed that unix mailbox headers follow this format:
  64.  *
  65.  * From <person> <date> <garbage>
  66.  *
  67.  * Where <person> is the address of the sender, being an ordinary
  68.  * string with no white space imbedded in it, and <date> is the date of
  69.  * posting, in ctime(3C) format.
  70.  *
  71.  * This would, on the face of it, seem valid. I (Bernd) have yet to find a
  72.  * unix mailbox header which doesn't follow this format.
  73.  *
  74.  * From: Bernd Wechner (bernd@bhpcpd.kembla.oz.au)
  75.  * Obfuscated by: KFS (as usual)
  76.  */
  77.  
  78. #define MAX_FIELDS 10
  79.  
  80. static char legal_day[]         = "SunMonTueWedThuFriSat";
  81. static char legal_month[]       = "JanFebMarAprMayJunJulAugSepOctNovDec";
  82. static int  legal_numbers[]     = { 1, 31, 0, 23, 0, 59, 0, 60, 1969, 2199 };
  83.  
  84. int is_mail_from_line(line)
  85. char *line;     /* Line of text to be checked */
  86. {
  87.     char *fields[MAX_FIELDS];
  88.     char *sender_tail;
  89.     register char *lp, **fp;
  90.     register int n, i;
  91.  
  92.     if (strncmp(line, "From ", 5)) return -100;
  93.  
  94.     lp = line + 5;
  95.     /* sender day mon dd hh:mm:ss year */
  96.     for (n = 0, fp = fields; n < MAX_FIELDS; n++) {
  97.         while (*lp && *lp != '\n' && isascii(*lp) && isspace(*lp)) lp++;
  98.         if (*lp == '\0' || *lp == '\n') break;
  99.         *fp++ = lp;
  100.         while (*lp && isascii(*lp) && !isspace(*lp))
  101.             if (*lp++ == ':' && (n == 4 || n == 5)) break;
  102.         if (n == 0) sender_tail = lp;
  103.    }
  104.  
  105.     if (n < 8) return -200-n;
  106.  
  107.     fp = fields;
  108.  
  109.     if (n > 8 && !isdigit(fp[7][0])) fp[7] = fp[8]; /* ... TZ year */
  110.     if (n > 9 && !isdigit(fp[7][0])) fp[7] = fp[9]; /* ... TZ DST year */
  111.  
  112.     fp++;
  113.     for (i = 0; i < 21; i += 3)
  114.         if (strncmp(*fp, &legal_day[i], 3) == 0) break;
  115.     if (i == 21) return -1;
  116.  
  117.     fp++;
  118.     for (i = 0; i < 36; i += 3)
  119.         if (strncmp(*fp, &legal_month[i], 3) == 0) break;
  120.     if (i == 36) return -2;
  121.  
  122.     for (i = 0; i < 10; i += 2) {
  123.         lp = *++fp;
  124.         if (!isdigit(*lp)) return -20-i;
  125.         n = atoi(lp);
  126.         if (n < legal_numbers[i] || legal_numbers[i+1] < n) return -10-i;
  127.    }
  128.     return 0;
  129. }
  130.  
  131.  
  132.  
  133. /* 
  134.  * Return the basename of a filename string, i.e. everything
  135.  * after the last "/" character. 
  136.  */
  137.  
  138. char *mtm_basename(string)
  139.     char *string;
  140. {
  141.   static   char *buff;
  142.   buff = string + strlen(string); /* start at last char */
  143.   while (*buff != '/' && buff > string)
  144.       buff--;
  145.   return( (char *) (*buff == '/'? ++buff : buff));
  146. }
  147.