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

  1. /*
  2.  * Program:    Operating-system dependent routines -- SUN-OS 4.0 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. extern int sys_nerr;
  70. extern char *sys_errlist[];
  71.  
  72. /* Write current time in RFC 822 format
  73.  * Accepts: destination string
  74.  */
  75.  
  76. char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  77. char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  78.         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  79.  
  80. void rfc822_date (date)
  81.     char *date;
  82. {
  83.   int zone;
  84.   char *zonename;
  85.   struct tm *t;
  86.   struct timeval tv;
  87.   struct timezone tz;
  88.   gettimeofday (&tv,&tz);    /* get time and timezone poop */
  89.   t = localtime (&tv.tv_sec);    /* convert to individual items */
  90.   zone = t->tm_gmtoff/60;    /* get timezone from TZ environment stuff */
  91.   zonename = t->tm_zone;
  92.                 /* and output it */
  93.   sprintf (date,"%s, %d %s %d %02d:%02d:%02d %+03d%02d (%s)",
  94.        days[t->tm_wday],t->tm_mday,months[t->tm_mon],t->tm_year+1900,
  95.        t->tm_hour,t->tm_min,t->tm_sec,zone/60,abs (zone) % 60,zonename);
  96. }
  97.  
  98. /* Get a block of free storage
  99.  * Accepts: size of desired block
  100.  * Returns: free storage block
  101.  */
  102.  
  103. void *fs_get (size)
  104.     size_t size;
  105. {
  106.   void *block = malloc (size);
  107.   if (!block) fatal ("Out of free storage");
  108.   return (block);
  109. }
  110.  
  111.  
  112. /* Resize a block of free storage
  113.  * Accepts: ** pointer to current block
  114.  *        new size
  115.  */
  116.  
  117. void fs_resize (block,size)
  118.     void **block;
  119.     size_t size;
  120. {
  121.   if (!(*block = realloc (*block,size))) fatal ("Can't resize free storage");
  122. }
  123.  
  124.  
  125. /* Return a block of free storage
  126.  * Accepts: ** pointer to free storage block
  127.  */
  128.  
  129. void fs_give (block)
  130.     void **block;
  131. {
  132.   free (*block);
  133.   *block = NIL;
  134. }
  135.  
  136.  
  137. /* Report a fatal error
  138.  * Accepts: string to output
  139.  */
  140.  
  141. void fatal (string)
  142.     char *string;
  143. {
  144.   mm_fatal (string);        /* output the string */
  145.   syslog (LOG_ALERT,"IMAP C-Client crash: %s",string);
  146.   abort ();            /* die horribly */
  147. }
  148.  
  149. /* Copy string with CRLF newlines
  150.  * Accepts: destination string
  151.  *        pointer to size of destination string
  152.  *        source string
  153.  *        length of source string
  154.  */
  155.  
  156. char *strcrlfcpy (dst,dstl,src,srcl)
  157.     char **dst;
  158.     unsigned long *dstl;
  159.     char *src;
  160.     unsigned long srcl;
  161. {
  162.   long i,j;
  163.   char *d = src;
  164.                 /* count number of LF's in source string(s) */
  165.   for (i = srcl,j = 0; j < srcl; j++) if (*d++ == '\012') i++;
  166.   if (i > *dstl) {        /* resize if not enough space */
  167.     fs_give ((void **) dst);    /* fs_resize does an unnecessary copy */
  168.     *dst = (char *) fs_get ((*dstl = i) + 1);
  169.   }
  170.   d = *dst;            /* destination string */
  171.                 /* copy strings, inserting CR's before LF's */
  172.   while (srcl--) switch (*src) {
  173.   case '\015':            /* unlikely carriage return */
  174.     *d++ = *src++;        /* copy it and any succeeding linefeed */
  175.     if (srcl && *src == '\012') {
  176.       *d++ = *src++;
  177.       srcl--;
  178.     }
  179.     break;
  180.   case '\012':            /* line feed? */
  181.     *d++ ='\015';        /* yes, prepend a CR, drop into default case */
  182.   default:            /* ordinary chararacter */
  183.     *d++ = *src++;        /* just copy character */
  184.     break;
  185.   }
  186.   *d = '\0';            /* tie off destination */
  187.   return *dst;            /* return destination */
  188. }
  189.  
  190.  
  191. /* Length of string after strcrlflen applied
  192.  * Accepts: source string
  193.  *        length of source string
  194.  */
  195.  
  196. unsigned long strcrlflen (src,srcl)
  197.     char *src;
  198.     unsigned long srcl;
  199. {
  200.   long i = srcl;        /* look for LF's */
  201.   while (srcl--) switch (*src++) {
  202.   case '\015':            /* unlikely carriage return */
  203.     if (srcl && *src == '\012') { src++; srcl--; }
  204.     break;
  205.   case '\012':            /* line feed? */
  206.     i++;
  207.   default:            /* ordinary chararacter */
  208.     break;
  209.   }
  210.   return i;
  211. }
  212.  
  213. /* Server log in
  214.  * Accepts: user name string
  215.  *        password string
  216.  *        optional place to return home directory
  217.  * Returns: T if password validated, NIL otherwise
  218.  */
  219.  
  220. long server_login (user,pass,home)
  221.     char *user;
  222.     char *pass;
  223.     char **home;
  224. {
  225.   struct passwd *pw = getpwnam (lcase (user));
  226.                 /* no entry for this user or root */
  227.   if (!(pw && pw->pw_uid)) return NIL;
  228.                 /* validate password */
  229.   if (strcmp (pw->pw_passwd,(char *) crypt (pass,pw->pw_passwd))) return NIL;
  230.   setgid (pw->pw_gid);        /* all OK, login in as that user */
  231.   setuid (pw->pw_uid);
  232.                 /* note home directory */
  233.   if (home) *home = cpystr (pw->pw_dir);
  234.   return T;
  235. }
  236.  
  237. /* TCP/IP open
  238.  * Accepts: host name
  239.  *        contact port number
  240.  * Returns: TCP/IP stream if success else NIL
  241.  */
  242.  
  243. TCPSTREAM *tcp_open (host,port)
  244.     char *host;
  245.     long port;
  246. {
  247.   TCPSTREAM *stream = NIL;
  248.   int sock;
  249.   char *s;
  250.   struct sockaddr_in sin;
  251.   struct hostent *host_name;
  252.   char hostname[MAILTMPLEN];
  253.   char tmp[MAILTMPLEN];
  254.   /* The domain literal form is used (rather than simply the dotted decimal
  255.      as with other Unix programs) because it has to be a valid "host name"
  256.      in mailsystem terminology. */
  257.                 /* look like domain literal? */
  258.   if (host[0] == '[' && host[(strlen (host))-1] == ']') {
  259.     strcpy (hostname,host+1);    /* yes, copy number part */
  260.     hostname[(strlen (hostname))-1] = '\0';
  261.     if ((sin.sin_addr.s_addr = inet_addr (hostname)) != -1) {
  262.       sin.sin_family = AF_INET;    /* family is always Internet */
  263.       strcpy (hostname,host);    /* hostname is user's argument */
  264.     }
  265.     else {
  266.       sprintf (tmp,"Bad format domain-literal: %.80s",host);
  267.       mm_log (tmp,ERROR);
  268.       return NIL;
  269.     }
  270.   }
  271.  
  272.   else {            /* lookup host name, note that brain-dead Unix
  273.                    requires lowercase! */
  274.     strcpy (hostname,host);    /* in case host is in write-protected memory */
  275.     if ((host_name = gethostbyname (lcase (hostname)))) {
  276.                 /* copy address type */
  277.       sin.sin_family = host_name->h_addrtype;
  278.                 /* copy host name */
  279.       strcpy (hostname,host_name->h_name);
  280.                 /* copy host addresses */
  281.       memcpy (&sin.sin_addr,host_name->h_addr,host_name->h_length);
  282.     }
  283.     else {
  284.       sprintf (tmp,"No such host as %.80s",host);
  285.       mm_log (tmp,ERROR);
  286.       return NIL;
  287.     }
  288.   }
  289.  
  290.                 /* copy port number in network format */
  291.   if (!(sin.sin_port = htons (port))) fatal ("Bad port argument to tcp_open");
  292.                 /* get a TCP stream */
  293.   if ((sock = socket (sin.sin_family,SOCK_STREAM,0)) < 0) {
  294.     sprintf (tmp,"Unable to create TCP socket: %s",strerror (errno));
  295.     mm_log (tmp,ERROR);
  296.     return NIL;
  297.   }
  298.                 /* open connection */
  299.   if (connect (sock,(struct sockaddr *)&sin,sizeof (sin)) < 0) {
  300.     sprintf (tmp,"Can't connect to %.80s,%d: %s",hostname,port,
  301.          strerror (errno));
  302.     mm_log (tmp,ERROR);
  303.     return NIL;
  304.   }
  305.                 /* create TCP/IP stream */
  306.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  307.                 /* copy official host name */
  308.   stream->host = cpystr (hostname);
  309.                 /* get local name */
  310.   gethostname (tmp,MAILTMPLEN-1);
  311.   stream->localhost = cpystr ((host_name = gethostbyname (tmp)) ?
  312.                   host_name->h_name : tmp);
  313.                 /* init sockets */
  314.   stream->tcpsi = stream->tcpso = sock;
  315.   stream->ictr = 0;        /* init input counter */
  316.   return stream;        /* return success */
  317. }
  318.  
  319. /* TCP/IP authenticated open
  320.  * Accepts: host name
  321.  *        service name
  322.  * Returns: TCP/IP stream if success else NIL
  323.  */
  324.  
  325. TCPSTREAM *tcp_aopen (host,service)
  326.     char *host;
  327.     char *service;
  328. {
  329.   TCPSTREAM *stream = NIL;
  330.   struct hostent *host_name;
  331.   char hostname[MAILTMPLEN];
  332.   int i;
  333.   int pipei[2],pipeo[2];
  334.   /* The domain literal form is used (rather than simply the dotted decimal
  335.      as with other Unix programs) because it has to be a valid "host name"
  336.      in mailsystem terminology. */
  337.                 /* look like domain literal? */
  338.   if (host[0] == '[' && host[i = (strlen (host))-1] == ']') {
  339.     strcpy (hostname,host+1);    /* yes, copy without brackets */
  340.     hostname[i-1] = '\0';
  341.   }
  342.                 /* note that Unix requires lowercase! */
  343.   else if (host_name = gethostbyname (lcase (strcpy (hostname,host))))
  344.     strcpy (hostname,host_name->h_name);
  345.                 /* make command pipes */
  346.   if (pipe (pipei) < 0) return NIL;
  347.   if (pipe (pipeo) < 0) {
  348.     close (pipei[0]); close (pipei[1]);
  349.     return NIL;
  350.   }
  351.   if ((i = fork ()) < 0) {    /* make inferior process */
  352.     close (pipei[0]); close (pipei[1]);
  353.     close (pipeo[0]); close (pipeo[1]);
  354.     return NIL;
  355.   }
  356.   if (i) {            /* parent? */
  357.     close (pipei[1]);        /* close child's side of the pipes */
  358.     close (pipeo[0]);
  359.   }
  360.   else {            /* child */
  361.     dup2 (pipei[1],1);        /* parent's input is my output */
  362.     dup2 (pipei[1],2);        /* parent's input is my error output too */
  363.     close (pipei[0]); close (pipei[1]);
  364.     dup2 (pipeo[0],0);        /* parent's output is my input */
  365.     close (pipeo[0]); close (pipeo[1]);
  366.                 /* now run it */
  367.     execl ("/usr/ucb/rsh","rsh",hostname,"exec",service,0);
  368.     _exit (1);            /* spazzed */
  369.   }
  370.  
  371.                 /* create TCP/IP stream */
  372.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  373.                 /* copy official host name */
  374.   stream->host = cpystr (hostname);
  375.                 /* get local name */
  376.   gethostname (hostname,MAILTMPLEN-1);
  377.   stream->localhost = cpystr ((host_name = gethostbyname (hostname)) ?
  378.                   host_name->h_name : hostname);
  379.   stream->tcpsi = pipei[0];    /* init sockets */
  380.   stream->tcpso = pipeo[1];
  381.   stream->ictr = 0;        /* init input counter */
  382.   return stream;        /* return success */
  383. }
  384.  
  385. /* TCP/IP receive line
  386.  * Accepts: TCP/IP stream
  387.  * Returns: text line string or NIL if failure
  388.  */
  389.  
  390. char *tcp_getline (stream)
  391.     TCPSTREAM *stream;
  392. {
  393.   int n,m;
  394.   char *st;
  395.   char *ret;
  396.   char *stp;
  397.   char tmp[2];
  398.   fd_set fds;
  399.   FD_ZERO (&fds);        /* initialize selection vector */
  400.   if (stream->tcpsi < 0) return NIL;
  401.   while (stream->ictr < 1) {    /* if nothing in the buffer */
  402.     FD_SET (stream->tcpsi,&fds);/* set bit in selection vector */
  403.                 /* block and read */
  404.     if ((select (stream->tcpsi+1,&fds,0,0,0) < 0) ||
  405.     ((stream->ictr = read (stream->tcpsi,stream->ibuf,BUFLEN)) < 1)) {
  406.       close (stream->tcpsi);    /* nuke the socket */
  407.       if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  408.       stream->tcpsi = stream->tcpso = -1;
  409.       return NIL;
  410.     }
  411.     stream->iptr = stream->ibuf;/* point at TCP buffer */
  412.   }
  413.   st = stream->iptr;        /* save start of string */
  414.   n = 0;            /* init string count */
  415.   while (stream->ictr--) {    /* look for end of line */
  416.                 /* saw the trailing CR? */
  417.     if (stream->iptr++[0] == '\015') {
  418.       ret = (char *) fs_get (n+1);
  419.       memcpy (ret,st,n);    /* copy into a free storage string */
  420.       ret[n] = '\0';        /* tie off string with null */
  421.                 /* eat the line feed */
  422.       tcp_getbuffer (stream,1,tmp);
  423.       return ret;        /* return it to caller */
  424.     }
  425.     ++n;            /* else count and try next character */
  426.   }
  427.   stp = (char *) fs_get (n);    /* copy first part of string */
  428.   memcpy (stp,st,n);
  429.                 /* recurse to get remainder */
  430.   if (st = tcp_getline (stream)) {
  431.                 /* build total string */
  432.     ret = (char *) fs_get (n+1+(m = strlen (st)));
  433.     memcpy (ret,stp,n);        /* copy first part */
  434.     memcpy (ret+n,st,m);    /* and second part */
  435.     ret[n+m] = '\0';        /* tie off string with null */
  436.     fs_give ((void **) &st);    /* flush partial string */
  437.     fs_give ((void **) &stp);    /* flush initial fragment */
  438.   }
  439.   else ret = stp;        /* return the fragment */
  440.   return ret;
  441. }
  442.  
  443. /* TCP/IP receive buffer
  444.  * Accepts: TCP/IP stream
  445.  *        size in bytes
  446.  *        buffer to read into
  447.  * Returns: T if success, NIL otherwise
  448.  */
  449.  
  450. long tcp_getbuffer (stream,size,buffer)
  451.     TCPSTREAM *stream;
  452.     unsigned long size;
  453.     char *buffer;
  454. {
  455.   unsigned long n;
  456.   char *bufptr = buffer;
  457.   fd_set fds;
  458.   FD_ZERO (&fds);        /* initialize selection vector */
  459.   if (stream->tcpsi < 0) return NIL;
  460.   while (size > 0) {        /* until request satisfied */
  461.     while (stream->ictr < 1) {    /* if nothing in the buffer */
  462.       FD_SET (stream->tcpsi,&fds);
  463.                 /* block and read */
  464.       if ((select (stream->tcpsi+1,&fds,0,0,0) < 0) ||
  465.       ((stream->ictr = read (stream->tcpsi,stream->ibuf,BUFLEN)) < 1)) {
  466.     close (stream->tcpsi);    /* nuke the socket */
  467.     if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  468.     stream->tcpsi = stream->tcpso = -1;
  469.     return NIL;
  470.       }
  471.                 /* point at TCP buffer */
  472.       stream->iptr = stream->ibuf;
  473.     }
  474.     n = min (size,stream->ictr);/* number of bytes to transfer */
  475.                 /* do the copy */
  476.     memcpy (bufptr,stream->iptr,n);
  477.     bufptr += n;        /* update pointer */
  478.     stream->iptr +=n;
  479.     size -= n;            /* update # of bytes to do */
  480.     stream->ictr -=n;
  481.   }
  482.   bufptr[0] = '\0';        /* tie off string */
  483.   return T;
  484. }
  485.  
  486. /* TCP/IP send string as record
  487.  * Accepts: TCP/IP stream
  488.  * Returns: T if success else NIL
  489.  */
  490.  
  491. long tcp_soutr (stream,string)
  492.     TCPSTREAM *stream;
  493.     char *string;
  494. {
  495.   int i;
  496.   unsigned long size = strlen (string);
  497.   fd_set fds;
  498.   FD_ZERO (&fds);        /* initialize selection vector */
  499.   if (stream->tcpso < 0) return NIL;
  500.   while (size > 0) {        /* until request satisfied */
  501.     FD_SET (stream->tcpso,&fds);/* set bit in selection vector */
  502.     if ((select (stream->tcpso+1,0,&fds,0,0) < 0) ||
  503.     ((i = write (stream->tcpso,string,size)) < 0)) {
  504.       puts (strerror (errno));
  505.       close (stream->tcpsi);    /* nuke the socket */
  506.       if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  507.       stream->tcpsi = stream->tcpso = -1;
  508.       return NIL;
  509.     }
  510.     size -= i;            /* count this size */
  511.     string += i;
  512.   }
  513.   return T;            /* all done */
  514. }
  515.  
  516.  
  517. /* TCP/IP close
  518.  * Accepts: TCP/IP stream
  519.  */
  520.  
  521. void tcp_close (stream)
  522.     TCPSTREAM *stream;
  523. {
  524.  
  525.   if (stream->tcpsi >= 0) {    /* no-op if no socket */
  526.     close (stream->tcpsi);    /* nuke the socket */
  527.     if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  528.     stream->tcpsi = stream->tcpso = -1;
  529.   }
  530.                 /* flush host names */
  531.   fs_give ((void **) &stream->host);
  532.   fs_give ((void **) &stream->localhost);
  533.   fs_give ((void **) &stream);    /* flush the stream */
  534. }
  535.  
  536. /* TCP/IP get host name
  537.  * Accepts: TCP/IP stream
  538.  * Returns: host name for this stream
  539.  */
  540.  
  541. char *tcp_host (stream)
  542.     TCPSTREAM *stream;
  543. {
  544.   return stream->host;        /* return host name */
  545. }
  546.  
  547.  
  548. /* TCP/IP get local host name
  549.  * Accepts: TCP/IP stream
  550.  * Returns: local host name
  551.  */
  552.  
  553. char *tcp_localhost (stream)
  554.     TCPSTREAM *stream;
  555. {
  556.   return stream->localhost;    /* return local host name */
  557. }
  558.  
  559. /* Return pointer to first occurance in string of a substring
  560.  * Accepts: source pointer
  561.  *        substring pointer
  562.  * Returns: pointer to substring in source or NIL if not found
  563.  */
  564.  
  565. char *strstr (cs,ct)
  566.      char *cs;
  567.      char *ct;
  568. {
  569.   char *s;
  570.   char *t;
  571.   while (cs = index (cs,*ct)) {    /* for each occurance of the first character */
  572.                 /* see if remainder of string matches */
  573.     for (s = cs + 1, t = ct + 1; *t && *s == *t; s++, t++);
  574.     if (!*t) return cs;        /* if ran out of substring then have match */
  575.     cs++;            /* try from next character */
  576.   }
  577.   return NIL;            /* not found */
  578. }
  579.  
  580.  
  581. /* Copy memory block
  582.  * Accepts: destination pointer
  583.  *        source pointer
  584.  *        length
  585.  * Returns: destination pointer
  586.  */
  587.  
  588. char *memmove (s,ct,n)
  589.      char *s;
  590.      char *ct;
  591.      int n;
  592. {
  593.   bcopy (ct,s,n);        /* they should have this one */
  594.   return ct;
  595. }
  596.  
  597.  
  598. /* Return implementation-defined string corresponding to error
  599.  * Accepts: error number
  600.  * Returns: string for that error
  601.  */
  602.  
  603. char *strerror (n)
  604.      int n;
  605. {
  606.   return (n >= 0 && n < sys_nerr) ? sys_errlist[n] : NIL;
  607. }
  608.