home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / imap-3.0 / non-ANSI / c-client / os_sgi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-27  |  16.5 KB  |  620 lines

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