home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / pine3.07 / c-client / os_ult.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-14  |  15.5 KB  |  555 lines

  1. /*
  2.  * Program:    Operating-system dependent routines -- Ultrix version
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    1 August 1988
  13.  * Last Edited:    14 May 1992
  14.  *
  15.  * Copyright 1992 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36. /* TCP input buffer */
  37.  
  38. #define BUFLEN 8192
  39.  
  40.  
  41. /* TCP I/O stream (must be before osdep.h is included) */
  42.  
  43. #define TCPSTREAM struct tcp_stream
  44. TCPSTREAM {
  45.   char *host;            /* host name */
  46.   char *localhost;        /* local host name */
  47.   int tcpsi;            /* input socket */
  48.   int tcpso;            /* output socket */
  49.   int ictr;            /* input counter */
  50.   char *iptr;            /* input pointer */
  51.   char ibuf[BUFLEN];        /* input buffer */
  52. };
  53.  
  54.  
  55. #include <sys/types.h>
  56. #include <sys/time.h>
  57. #include <sys/socket.h>
  58. #include <netinet/in.h>
  59. #include <netdb.h>
  60. #include <ctype.h>
  61. #include <errno.h>
  62. extern int errno;        /* just in case */
  63. #include <pwd.h>
  64. #include <syslog.h>
  65. #include "osdep.h"
  66. #include "mail.h"
  67. #include "misc.h"
  68.  
  69. /* Write current time in RFC 822 format
  70.  * Accepts: destination string
  71.  */
  72.  
  73. char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  74. char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  75.         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  76.  
  77. void rfc822_date (date)
  78.     char *date;
  79. {
  80.   int zone;
  81.   char *zonename;
  82.   struct tm *t;
  83.   struct timeval tv;
  84.   struct timezone tz;
  85.   gettimeofday (&tv,&tz);    /* get time and timezone poop */
  86.   t = localtime (&tv.tv_sec);    /* convert to individual items */
  87.   zone = t->tm_gmtoff/60;    /* get timezone from TZ environment stuff */
  88.   zonename = t->tm_zone;
  89.                 /* and output it */
  90.   sprintf (date,"%s, %d %s %d %02d:%02d:%02d %+03d%02d (%s)",
  91.        days[t->tm_wday],t->tm_mday,months[t->tm_mon],t->tm_year+1900,
  92.        t->tm_hour,t->tm_min,t->tm_sec,zone/60,abs (zone) % 60,zonename);
  93. }
  94.  
  95. /* Get a block of free storage
  96.  * Accepts: size of desired block
  97.  * Returns: free storage block
  98.  */
  99.  
  100. void *fs_get (size)
  101.     size_t size;
  102. {
  103.   void *block = malloc (size);
  104.   if (!block) fatal ("Out of free storage");
  105.   return (block);
  106. }
  107.  
  108.  
  109. /* Resize a block of free storage
  110.  * Accepts: ** pointer to current block
  111.  *        new size
  112.  */
  113.  
  114. void fs_resize (block,size)
  115.     void **block;
  116.     size_t size;
  117. {
  118.   if (!(*block = realloc (*block,size))) fatal ("Can't resize free storage");
  119. }
  120.  
  121.  
  122. /* Return a block of free storage
  123.  * Accepts: ** pointer to free storage block
  124.  */
  125.  
  126. void fs_give (block)
  127.     void **block;
  128. {
  129.   free (*block);
  130.   *block = NIL;
  131. }
  132.  
  133.  
  134. /* Report a fatal error
  135.  * Accepts: string to output
  136.  */
  137.  
  138. void fatal (string)
  139.     char *string;
  140. {
  141.   mm_fatal (string);        /* output the string */
  142.   syslog (LOG_ALERT,"IMAP C-Client crash: %s",string);
  143.   abort ();            /* die horribly */
  144. }
  145.  
  146. /* Copy string with CRLF newlines
  147.  * Accepts: destination string
  148.  *        pointer to size of destination string
  149.  *        source string
  150.  *        length of source string
  151.  */
  152.  
  153. char *strcrlfcpy (dst,dstl,src,srcl)
  154.     char **dst;
  155.     unsigned long *dstl;
  156.     char *src;
  157.     unsigned long srcl;
  158. {
  159.   long i,j;
  160.   char *d = src;
  161.                 /* count number of LF's in source string(s) */
  162.   for (i = srcl,j = 0; j < srcl; j++) if (*d++ == '\012') i++;
  163.   if (i > *dstl) {        /* resize if not enough space */
  164.     fs_give ((void **) dst);    /* fs_resize does an unnecessary copy */
  165.     *dst = (char *) fs_get ((*dstl = i) + 1);
  166.   }
  167.   d = *dst;            /* destination string */
  168.                 /* copy strings, inserting CR's before LF's */
  169.   while (srcl--) switch (*src) {
  170.   case '\015':            /* unlikely carriage return */
  171.     *d++ = *src++;        /* copy it and any succeeding linefeed */
  172.     if (srcl && *src == '\012') {
  173.       *d++ = *src++;
  174.       srcl--;
  175.     }
  176.     break;
  177.   case '\012':            /* line feed? */
  178.     *d++ ='\015';        /* yes, prepend a CR, drop into default case */
  179.   default:            /* ordinary chararacter */
  180.     *d++ = *src++;        /* just copy character */
  181.     break;
  182.   }
  183.   *d = '\0';            /* tie off destination */
  184.   return *dst;            /* return destination */
  185. }
  186.  
  187.  
  188. /* Length of string after strcrlflen applied
  189.  * Accepts: source string
  190.  *        length of source string
  191.  */
  192.  
  193. unsigned long strcrlflen (src,srcl)
  194.     char *src;
  195.     unsigned long srcl;
  196. {
  197.   long i = srcl;        /* look for LF's */
  198.   while (srcl--) switch (*src++) {
  199.   case '\015':            /* unlikely carriage return */
  200.     if (srcl && *src == '\012') { src++; srcl--; }
  201.     break;
  202.   case '\012':            /* line feed? */
  203.     i++;
  204.   default:            /* ordinary chararacter */
  205.     break;
  206.   }
  207.   return i;
  208. }
  209.  
  210. /* Server log in
  211.  * Accepts: user name string
  212.  *        password string
  213.  *        optional place to return home directory
  214.  * Returns: T if password validated, NIL otherwise
  215.  */
  216.  
  217. long server_login (user,pass,home)
  218.     char *user;
  219.     char *pass;
  220.     char **home;
  221. {
  222.   struct passwd *pw = getpwnam (lcase (user));
  223.                 /* no entry for this user or root */
  224.   if (!(pw && pw->pw_uid)) return NIL;
  225.                 /* validate password */
  226.   if (strcmp (pw->pw_passwd,(char *) crypt (pass,pw->pw_passwd))) return NIL;
  227.   setgid (pw->pw_gid);        /* all OK, login in as that user */
  228.   setuid (pw->pw_uid);
  229.                 /* note home directory */
  230.   if (home) *home = cpystr (pw->pw_dir);
  231.   return T;
  232. }
  233.  
  234. /* TCP/IP open
  235.  * Accepts: host name
  236.  *        contact port number
  237.  * Returns: TCP/IP stream if success else NIL
  238.  */
  239.  
  240. TCPSTREAM *tcp_open (host,port)
  241.     char *host;
  242.     long port;
  243. {
  244.   TCPSTREAM *stream = NIL;
  245.   int sock;
  246.   char *s;
  247.   struct sockaddr_in sin;
  248.   struct hostent *host_name;
  249.   char hostname[MAILTMPLEN];
  250.   char tmp[MAILTMPLEN];
  251.   /* The domain literal form is used (rather than simply the dotted decimal
  252.      as with other Unix programs) because it has to be a valid "host name"
  253.      in mailsystem terminology. */
  254.                 /* look like domain literal? */
  255.   if (host[0] == '[' && host[(strlen (host))-1] == ']') {
  256.     strcpy (hostname,host+1);    /* yes, copy number part */
  257.     hostname[(strlen (hostname))-1] = '\0';
  258.     if ((sin.sin_addr.s_addr = inet_addr (hostname)) != -1) {
  259.       sin.sin_family = AF_INET;    /* family is always Internet */
  260.       strcpy (hostname,host);    /* hostname is user's argument */
  261.     }
  262.     else {
  263.       sprintf (tmp,"Bad format domain-literal: %.80s",host);
  264.       mm_log (tmp,ERROR);
  265.       return NIL;
  266.     }
  267.   }
  268.  
  269.   else {            /* lookup host name, note that brain-dead Unix
  270.                    requires lowercase! */
  271.     strcpy (hostname,host);    /* in case host is in write-protected memory */
  272.     if ((host_name = gethostbyname (lcase (hostname)))) {
  273.                 /* copy address type */
  274.       sin.sin_family = host_name->h_addrtype;
  275.                 /* copy host name */
  276.       strcpy (hostname,host_name->h_name);
  277.                 /* copy host addresses */
  278.       memcpy (&sin.sin_addr,host_name->h_addr,host_name->h_length);
  279.     }
  280.     else {
  281.       sprintf (tmp,"No such host as %.80s",host);
  282.       mm_log (tmp,ERROR);
  283.       return NIL;
  284.     }
  285.   }
  286.  
  287.                 /* copy port number in network format */
  288.   if (!(sin.sin_port = htons (port))) fatal ("Bad port argument to tcp_open");
  289.                 /* get a TCP stream */
  290.   if ((sock = socket (sin.sin_family,SOCK_STREAM,0)) < 0) {
  291.     sprintf (tmp,"Unable to create TCP socket: %s",strerror (errno));
  292.     mm_log (tmp,ERROR);
  293.     return NIL;
  294.   }
  295.                 /* open connection */
  296.   if (connect (sock,(struct sockaddr *)&sin,sizeof (sin)) < 0) {
  297.     sprintf (tmp,"Can't connect to %.80s,%d: %s",hostname,port,
  298.          strerror (errno));
  299.     mm_log (tmp,ERROR);
  300.     return NIL;
  301.   }
  302.                 /* create TCP/IP stream */
  303.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  304.                 /* copy official host name */
  305.   stream->host = cpystr (hostname);
  306.                 /* get local name */
  307.   gethostname (tmp,MAILTMPLEN-1);
  308.   stream->localhost = cpystr ((host_name = gethostbyname (tmp)) ?
  309.                   host_name->h_name : tmp);
  310.                 /* init sockets */
  311.   stream->tcpsi = stream->tcpso = sock;
  312.   stream->ictr = 0;        /* init input counter */
  313.   return stream;        /* return success */
  314. }
  315.  
  316. /* TCP/IP authenticated open
  317.  * Accepts: host name
  318.  *        service name
  319.  * Returns: TCP/IP stream if success else NIL
  320.  */
  321.  
  322. TCPSTREAM *tcp_aopen (host,service)
  323.     char *host;
  324.     char *service;
  325. {
  326.   TCPSTREAM *stream = NIL;
  327.   struct hostent *host_name;
  328.   char hostname[MAILTMPLEN];
  329.   int i;
  330.   int pipei[2],pipeo[2];
  331.   /* The domain literal form is used (rather than simply the dotted decimal
  332.      as with other Unix programs) because it has to be a valid "host name"
  333.      in mailsystem terminology. */
  334.                 /* look like domain literal? */
  335.   if (host[0] == '[' && host[i = (strlen (host))-1] == ']') {
  336.     strcpy (hostname,host+1);    /* yes, copy without brackets */
  337.     hostname[i-1] = '\0';
  338.   }
  339.                 /* note that Unix requires lowercase! */
  340.   else if (host_name = gethostbyname (lcase (strcpy (hostname,host))))
  341.     strcpy (hostname,host_name->h_name);
  342.                 /* make command pipes */
  343.   if (pipe (pipei) < 0) return NIL;
  344.   if (pipe (pipeo) < 0) {
  345.     close (pipei[0]); close (pipei[1]);
  346.     return NIL;
  347.   }
  348.   if ((i = fork ()) < 0) {    /* make inferior process */
  349.     close (pipei[0]); close (pipei[1]);
  350.     close (pipeo[0]); close (pipeo[1]);
  351.     return NIL;
  352.   }
  353.   if (i) {            /* parent? */
  354.     close (pipei[1]);        /* close child's side of the pipes */
  355.     close (pipeo[0]);
  356.   }
  357.   else {            /* child */
  358.     dup2 (pipei[1],1);        /* parent's input is my output */
  359.     dup2 (pipei[1],2);        /* parent's input is my error output too */
  360.     close (pipei[0]); close (pipei[1]);
  361.     dup2 (pipeo[0],0);        /* parent's output is my input */
  362.     close (pipeo[0]); close (pipeo[1]);
  363.                 /* now run it */
  364.     execl ("/usr/ucb/rsh","rsh",hostname,"exec",service,0);
  365.     _exit (1);            /* spazzed */
  366.   }
  367.  
  368.                 /* create TCP/IP stream */
  369.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  370.                 /* copy official host name */
  371.   stream->host = cpystr (hostname);
  372.                 /* get local name */
  373.   gethostname (hostname,MAILTMPLEN-1);
  374.   stream->localhost = cpystr ((host_name = gethostbyname (hostname)) ?
  375.                   host_name->h_name : hostname);
  376.   stream->tcpsi = pipei[0];    /* init sockets */
  377.   stream->tcpso = pipeo[1];
  378.   stream->ictr = 0;        /* init input counter */
  379.   return stream;        /* return success */
  380. }
  381.  
  382. /* TCP/IP receive line
  383.  * Accepts: TCP/IP stream
  384.  * Returns: text line string or NIL if failure
  385.  */
  386.  
  387. char *tcp_getline (stream)
  388.     TCPSTREAM *stream;
  389. {
  390.   int n,m;
  391.   char *st;
  392.   char *ret;
  393.   char *stp;
  394.   char tmp[2];
  395.   fd_set fds;
  396.   FD_ZERO (&fds);        /* initialize selection vector */
  397.   if (stream->tcpsi < 0) return NIL;
  398.   while (stream->ictr < 1) {    /* if nothing in the buffer */
  399.     FD_SET (stream->tcpsi,&fds);/* set bit in selection vector */
  400.                 /* block and read */
  401.     if ((select (stream->tcpsi+1,&fds,0,0,0) < 0) ||
  402.     ((stream->ictr = read (stream->tcpsi,stream->ibuf,BUFLEN)) < 1)) {
  403.       close (stream->tcpsi);    /* nuke the socket */
  404.       if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  405.       stream->tcpsi = stream->tcpso = -1;
  406.       return NIL;
  407.     }
  408.     stream->iptr = stream->ibuf;/* point at TCP buffer */
  409.   }
  410.   st = stream->iptr;        /* save start of string */
  411.   n = 0;            /* init string count */
  412.   while (stream->ictr--) {    /* look for end of line */
  413.                 /* saw the trailing CR? */
  414.     if (stream->iptr++[0] == '\015') {
  415.       ret = (char *) fs_get (n+1);
  416.       memcpy (ret,st,n);    /* copy into a free storage string */
  417.       ret[n] = '\0';        /* tie off string with null */
  418.                 /* eat the line feed */
  419.       tcp_getbuffer (stream,1,tmp);
  420.       return ret;        /* return it to caller */
  421.     }
  422.     ++n;            /* else count and try next character */
  423.   }
  424.   stp = (char *) fs_get (n);    /* copy first part of string */
  425.   memcpy (stp,st,n);
  426.                 /* recurse to get remainder */
  427.   if (st = tcp_getline (stream)) {
  428.                 /* build total string */
  429.     ret = (char *) fs_get (n+1+(m = strlen (st)));
  430.     memcpy (ret,stp,n);        /* copy first part */
  431.     memcpy (ret+n,st,m);    /* and second part */
  432.     ret[n+m] = '\0';        /* tie off string with null */
  433.     fs_give ((void **) &st);    /* flush partial string */
  434.     fs_give ((void **) &stp);    /* flush initial fragment */
  435.   }
  436.   else ret = stp;        /* return the fragment */
  437.   return ret;
  438. }
  439.  
  440. /* TCP/IP receive buffer
  441.  * Accepts: TCP/IP stream
  442.  *        size in bytes
  443.  *        buffer to read into
  444.  * Returns: T if success, NIL otherwise
  445.  */
  446.  
  447. long tcp_getbuffer (stream,size,buffer)
  448.     TCPSTREAM *stream;
  449.     unsigned long size;
  450.     char *buffer;
  451. {
  452.   unsigned long n;
  453.   char *bufptr = buffer;
  454.   fd_set fds;
  455.   FD_ZERO (&fds);        /* initialize selection vector */
  456.   if (stream->tcpsi < 0) return NIL;
  457.   while (size > 0) {        /* until request satisfied */
  458.     while (stream->ictr < 1) {    /* if nothing in the buffer */
  459.       FD_SET (stream->tcpsi,&fds);
  460.                 /* block and read */
  461.       if ((select (stream->tcpsi+1,&fds,0,0,0) < 0) ||
  462.       ((stream->ictr = read (stream->tcpsi,stream->ibuf,BUFLEN)) < 1)) {
  463.     close (stream->tcpsi);    /* nuke the socket */
  464.     if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  465.     stream->tcpsi = stream->tcpso = -1;
  466.     return NIL;
  467.       }
  468.                 /* point at TCP buffer */
  469.       stream->iptr = stream->ibuf;
  470.     }
  471.     n = min (size,stream->ictr);/* number of bytes to transfer */
  472.                 /* do the copy */
  473.     memcpy (bufptr,stream->iptr,n);
  474.     bufptr += n;        /* update pointer */
  475.     stream->iptr +=n;
  476.     size -= n;            /* update # of bytes to do */
  477.     stream->ictr -=n;
  478.   }
  479.   bufptr[0] = '\0';        /* tie off string */
  480.   return T;
  481. }
  482.  
  483. /* TCP/IP send string as record
  484.  * Accepts: TCP/IP stream
  485.  * Returns: T if success else NIL
  486.  */
  487.  
  488. long tcp_soutr (stream,string)
  489.     TCPSTREAM *stream;
  490.     char *string;
  491. {
  492.   int i;
  493.   unsigned long size = strlen (string);
  494.   fd_set fds;
  495.   FD_ZERO (&fds);        /* initialize selection vector */
  496.   if (stream->tcpso < 0) return NIL;
  497.   while (size > 0) {        /* until request satisfied */
  498.     FD_SET (stream->tcpso,&fds);/* set bit in selection vector */
  499.     if ((select (stream->tcpso+1,0,&fds,0,0) < 0) ||
  500.     ((i = write (stream->tcpso,string,size)) < 0)) {
  501.       puts (strerror (errno));
  502.       close (stream->tcpsi);    /* nuke the socket */
  503.       if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  504.       stream->tcpsi = stream->tcpso = -1;
  505.       return NIL;
  506.     }
  507.     size -= i;            /* count this size */
  508.     string += i;
  509.   }
  510.   return T;            /* all done */
  511. }
  512.  
  513.  
  514. /* TCP/IP close
  515.  * Accepts: TCP/IP stream
  516.  */
  517.  
  518. void tcp_close (stream)
  519.     TCPSTREAM *stream;
  520. {
  521.  
  522.   if (stream->tcpsi >= 0) {    /* no-op if no socket */
  523.     close (stream->tcpsi);    /* nuke the socket */
  524.     if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  525.     stream->tcpsi = stream->tcpso = -1;
  526.   }
  527.                 /* flush host names */
  528.   fs_give ((void **) &stream->host);
  529.   fs_give ((void **) &stream->localhost);
  530.   fs_give ((void **) &stream);    /* flush the stream */
  531. }
  532.  
  533. /* TCP/IP get host name
  534.  * Accepts: TCP/IP stream
  535.  * Returns: host name for this stream
  536.  */
  537.  
  538. char *tcp_host (stream)
  539.     TCPSTREAM *stream;
  540. {
  541.   return stream->host;        /* return host name */
  542. }
  543.  
  544.  
  545. /* TCP/IP get local host name
  546.  * Accepts: TCP/IP stream
  547.  * Returns: local host name
  548.  */
  549.  
  550. char *tcp_localhost (stream)
  551.     TCPSTREAM *stream;
  552. {
  553.   return stream->localhost;    /* return local host name */
  554. }
  555.