home *** CD-ROM | disk | FTP | other *** search
/ vsiftp.vmssoftware.com / VSIPUBLIC@vsiftp.vmssoftware.com.tar / FREEWARE / FREEWARE40.ZIP / pine / c-client / os_sgi.c < prev    next >
C/C++ Source or Header  |  1994-01-09  |  17KB  |  621 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:    16 August 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.  * Returns: length of copied string
  150.  */
  151.  
  152. unsigned long strcrlfcpy (dst,dstl,src,srcl)
  153.     char **dst;
  154.     unsigned long *dstl;
  155.     char *src;
  156.     unsigned long srcl;
  157. {
  158.   long i,j;
  159.   char *d = src;
  160.                 /* count number of LF's in source string(s) */
  161.   for (i = srcl,j = 0; j < srcl; j++) if (*d++ == '\012') i++;
  162.   if (i > *dstl) {        /* resize if not enough space */
  163.     fs_give ((void **) dst);    /* fs_resize does an unnecessary copy */
  164.     *dst = (char *) fs_get ((*dstl = i) + 1);
  165.   }
  166.   d = *dst;            /* destination string */
  167.                 /* copy strings, inserting CR's before LF's */
  168.   while (srcl--) switch (*src) {
  169.   case '\015':            /* unlikely carriage return */
  170.     *d++ = *src++;        /* copy it and any succeeding linefeed */
  171.     if (srcl && *src == '\012') {
  172.       *d++ = *src++;
  173.       srcl--;
  174.     }
  175.     break;
  176.   case '\012':            /* line feed? */
  177.     *d++ ='\015';        /* yes, prepend a CR, drop into default case */
  178.   default:            /* ordinary chararacter */
  179.     *d++ = *src++;        /* just copy character */
  180.     break;
  181.   }
  182.   *d = '\0';            /* tie off destination */
  183.   return d- *dst;        /* return length */
  184. }
  185.  
  186.  
  187. /* Length of string after strcrlfcpy applied
  188.  * Accepts: source string
  189.  * Returns: length of string
  190.  */
  191.  
  192. unsigned long strcrlflen (s)
  193.     STRING *s;
  194. {
  195.   unsigned long pos = GETPOS (s);
  196.   unsigned long i = SIZE (s);
  197.   unsigned long j = i;
  198.   while (j--) switch (SNX (s)) {/* search for newlines */
  199.   case '\015':            /* unlikely carriage return */
  200.     if (j && (CHR (s) == '\012')) {
  201.       SNX (s);            /* eat the line feed */
  202.       j--;
  203.     }
  204.     break;
  205.   case '\012':            /* line feed? */
  206.     i++;
  207.   default:            /* ordinary chararacter */
  208.     break;
  209.   }
  210.   SETPOS (s,pos);        /* restore old position */
  211.   return i;
  212. }
  213.  
  214. /* Server log in
  215.  * Accepts: user name string
  216.  *        password string
  217.  *        optional place to return home directory
  218.  * Returns: T if password validated, NIL otherwise
  219.  */
  220.  
  221. long server_login (user,pass,home,argc,argv)
  222.     char *user;
  223.     char *pass;
  224.     char **home;
  225.     int argc;
  226.     char *argv[];
  227. {
  228.   struct passwd *pw = getpwnam (lcase (user));
  229.                 /* no entry for this user or root */
  230.   if (!(pw && pw->pw_uid)) return NIL;
  231.                 /* validate password */
  232.   if (strcmp (pw->pw_passwd,(char *) crypt (pass,pw->pw_passwd))) return NIL;
  233.   setgid (pw->pw_gid);        /* all OK, login in as that user */
  234.   initgroups (user,pw->pw_gid);    /* initialize groups */
  235.   setuid (pw->pw_uid);
  236.                 /* note home directory */
  237.   if (home) *home = cpystr (pw->pw_dir);
  238.   return T;
  239. }
  240.  
  241. /* Return my user name
  242.  * Returns: my user name
  243.  */
  244.  
  245. char *myuname = NIL;
  246.  
  247. char *myusername ()
  248. {
  249.   return myuname ? myuname : (myuname = cpystr(getpwuid(geteuid ())->pw_name));
  250. }
  251.  
  252.  
  253. /* Return my home directory name
  254.  * Returns: my home directory name
  255.  */
  256.  
  257. char *hdname = NIL;
  258.  
  259. char *myhomedir ()
  260. {
  261.   return hdname ? hdname : (hdname = cpystr (getpwuid (geteuid ())->pw_dir));
  262. }
  263.  
  264.  
  265. /* Build status lock file name
  266.  * Accepts: scratch buffer
  267.  *        file name
  268.  * Returns: name of file to lock
  269.  */
  270.  
  271. char *lockname (tmp,fname)
  272.     char *tmp;
  273.     char *fname;
  274. {
  275.   int i;
  276.   sprintf (tmp,"/tmp/.%s",fname);
  277.   for (i = 6; i < strlen (tmp); ++i) if (tmp[i] == '/') tmp[i] = '\\';
  278.   return tmp;            /* note name for later */
  279. }
  280.  
  281. /* TCP/IP open
  282.  * Accepts: host name
  283.  *        contact port number
  284.  * Returns: TCP/IP stream if success else NIL
  285.  */
  286.  
  287. TCPSTREAM *tcp_open (host,port)
  288.     char *host;
  289.     long port;
  290. {
  291.   TCPSTREAM *stream = NIL;
  292.   int sock;
  293.   char *s;
  294.   struct sockaddr_in sin;
  295.   struct hostent *host_name;
  296.   char hostname[MAILTMPLEN];
  297.   char tmp[MAILTMPLEN];
  298.   /* The domain literal form is used (rather than simply the dotted decimal
  299.      as with other Unix programs) because it has to be a valid "host name"
  300.      in mailsystem terminology. */
  301.                 /* look like domain literal? */
  302.   if (host[0] == '[' && host[(strlen (host))-1] == ']') {
  303.     strcpy (hostname,host+1);    /* yes, copy number part */
  304.     hostname[(strlen (hostname))-1] = '\0';
  305.     if ((sin.sin_addr.s_addr = inet_addr (hostname)) != -1) {
  306.       sin.sin_family = AF_INET;    /* family is always Internet */
  307.       strcpy (hostname,host);    /* hostname is user's argument */
  308.     }
  309.     else {
  310.       sprintf (tmp,"Bad format domain-literal: %.80s",host);
  311.       mm_log (tmp,ERROR);
  312.       return NIL;
  313.     }
  314.   }
  315.  
  316.   else {            /* lookup host name, note that brain-dead Unix
  317.                    requires lowercase! */
  318.     strcpy (hostname,host);    /* in case host is in write-protected memory */
  319.     if ((host_name = gethostbyname (lcase (hostname)))) {
  320.                 /* copy address type */
  321.       sin.sin_family = host_name->h_addrtype;
  322.                 /* copy host name */
  323.       strcpy (hostname,host_name->h_name);
  324.                 /* copy host addresses */
  325.       memcpy (&sin.sin_addr,host_name->h_addr,host_name->h_length);
  326.     }
  327.     else {
  328.       sprintf (tmp,"No such host as %.80s",host);
  329.       mm_log (tmp,ERROR);
  330.       return NIL;
  331.     }
  332.   }
  333.  
  334.                 /* copy port number in network format */
  335.   if (!(sin.sin_port = htons (port))) fatal ("Bad port argument to tcp_open");
  336.                 /* get a TCP stream */
  337.   if ((sock = socket (sin.sin_family,SOCK_STREAM,0)) < 0) {
  338.     sprintf (tmp,"Unable to create TCP socket: %s",strerror (errno));
  339.     mm_log (tmp,ERROR);
  340.     return NIL;
  341.   }
  342.                 /* open connection */
  343.   if (connect (sock,(struct sockaddr *)&sin,sizeof (sin)) < 0) {
  344.     sprintf (tmp,"Can't connect to %.80s,%d: %s",hostname,port,
  345.          strerror (errno));
  346.     mm_log (tmp,ERROR);
  347.     return NIL;
  348.   }
  349.                 /* create TCP/IP stream */
  350.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  351.                 /* copy official host name */
  352.   stream->host = cpystr (hostname);
  353.                 /* get local name */
  354.   gethostname (tmp,MAILTMPLEN-1);
  355.   stream->localhost = cpystr ((host_name = gethostbyname (tmp)) ?
  356.                   host_name->h_name : tmp);
  357.                 /* init sockets */
  358.   stream->tcpsi = stream->tcpso = sock;
  359.   stream->ictr = 0;        /* init input counter */
  360.   return stream;        /* return success */
  361. }
  362.  
  363. /* TCP/IP authenticated open
  364.  * Accepts: host name
  365.  *        service name
  366.  * Returns: TCP/IP stream if success else NIL
  367.  */
  368.  
  369. TCPSTREAM *tcp_aopen (host,service)
  370.     char *host;
  371.     char *service;
  372. {
  373.   TCPSTREAM *stream = NIL;
  374.   struct hostent *host_name;
  375.   char hostname[MAILTMPLEN];
  376.   int i;
  377.   int pipei[2],pipeo[2];
  378.   /* The domain literal form is used (rather than simply the dotted decimal
  379.      as with other Unix programs) because it has to be a valid "host name"
  380.      in mailsystem terminology. */
  381.                 /* look like domain literal? */
  382.   if (host[0] == '[' && host[i = (strlen (host))-1] == ']') {
  383.     strcpy (hostname,host+1);    /* yes, copy without brackets */
  384.     hostname[i-1] = '\0';
  385.   }
  386.                 /* note that Unix requires lowercase! */
  387.   else if (host_name = gethostbyname (lcase (strcpy (hostname,host))))
  388.     strcpy (hostname,host_name->h_name);
  389.                 /* make command pipes */
  390.   if (pipe (pipei) < 0) return NIL;
  391.   if (pipe (pipeo) < 0) {
  392.     close (pipei[0]); close (pipei[1]);
  393.     return NIL;
  394.   }
  395.   if ((i = fork ()) < 0) {    /* make inferior process */
  396.     close (pipei[0]); close (pipei[1]);
  397.     close (pipeo[0]); close (pipeo[1]);
  398.     return NIL;
  399.   }
  400.   if (i) {            /* parent? */
  401.     close (pipei[1]);        /* close child's side of the pipes */
  402.     close (pipeo[0]);
  403.   }
  404.   else {            /* child */
  405.     dup2 (pipei[1],1);        /* parent's input is my output */
  406.     dup2 (pipei[1],2);        /* parent's input is my error output too */
  407.     close (pipei[0]); close (pipei[1]);
  408.     dup2 (pipeo[0],0);        /* parent's output is my input */
  409.     close (pipeo[0]); close (pipeo[1]);
  410.                 /* now run it */
  411.     execl ("/usr/ucb/rsh","rsh",hostname,"exec",service,0);
  412.     _exit (1);            /* spazzed */
  413.   }
  414.  
  415.                 /* create TCP/IP stream */
  416.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  417.                 /* copy official host name */
  418.   stream->host = cpystr (hostname);
  419.                 /* get local name */
  420.   gethostname (hostname,MAILTMPLEN-1);
  421.   stream->localhost = cpystr ((host_name = gethostbyname (hostname)) ?
  422.                   host_name->h_name : hostname);
  423.   stream->tcpsi = pipei[0];    /* init sockets */
  424.   stream->tcpso = pipeo[1];
  425.   stream->ictr = 0;        /* init input counter */
  426.   return stream;        /* return success */
  427. }
  428.  
  429. /* TCP/IP receive line
  430.  * Accepts: TCP/IP stream
  431.  * Returns: text line string or NIL if failure
  432.  */
  433.  
  434. char *tcp_getline (stream)
  435.     TCPSTREAM *stream;
  436. {
  437.   int n,m;
  438.   char *st,*ret,*stp;
  439.   char c = '\0';
  440.   char d;
  441.                 /* make sure have data */
  442.   if (!tcp_getdata (stream)) return NIL;
  443.   st = stream->iptr;        /* save start of string */
  444.   n = 0;            /* init string count */
  445.   while (stream->ictr--) {    /* look for end of line */
  446.     d = *stream->iptr++;    /* slurp another character */
  447.     if ((c == '\015') && (d == '\012')) {
  448.       ret = (char *) fs_get (n--);
  449.       memcpy (ret,st,n);    /* copy into a free storage string */
  450.       ret[n] = '\0';        /* tie off string with null */
  451.       return ret;
  452.     }
  453.     n++;            /* count another character searched */
  454.     c = d;            /* remember previous character */
  455.   }
  456.                 /* copy partial string from buffer */
  457.   memcpy ((ret = stp = (char *) fs_get (n)),st,n);
  458.                 /* get more data from the net */
  459.   if (!tcp_getdata (stream)) return NIL;
  460.                 /* special case of newline broken by buffer */
  461.   if ((c == '\015') && (*stream->iptr == '\012')) {
  462.     stream->iptr++;        /* eat the line feed */
  463.     stream->ictr--;
  464.     ret[n - 1] = '\0';        /* tie off string with null */
  465.   }
  466.                 /* else recurse to get remainder */
  467.   else if (st = tcp_getline (stream)) {
  468.     ret = (char *) fs_get (n + 1 + (m = strlen (st)));
  469.     memcpy (ret,stp,n);        /* copy first part */
  470.     memcpy (ret + n,st,m);    /* and second part */
  471.     fs_give ((void **) &stp);    /* flush first part */
  472.     fs_give ((void **) &st);    /* flush second part */
  473.     ret[n + m] = '\0';        /* tie off string with null */
  474.   }
  475.   return ret;
  476. }
  477.  
  478. /* TCP/IP receive buffer
  479.  * Accepts: TCP/IP stream
  480.  *        size in bytes
  481.  *        buffer to read into
  482.  * Returns: T if success, NIL otherwise
  483.  */
  484.  
  485. long tcp_getbuffer (stream,size,buffer)
  486.     TCPSTREAM *stream;
  487.     unsigned long size;
  488.     char *buffer;
  489. {
  490.   unsigned long n;
  491.   char *bufptr = buffer;
  492.   while (size > 0) {        /* until request satisfied */
  493.     if (!tcp_getdata (stream)) return NIL;
  494.     n = min (size,stream->ictr);/* number of bytes to transfer */
  495.                 /* do the copy */
  496.     memcpy (bufptr,stream->iptr,n);
  497.     bufptr += n;        /* update pointer */
  498.     stream->iptr +=n;
  499.     size -= n;            /* update # of bytes to do */
  500.     stream->ictr -=n;
  501.   }
  502.   bufptr[0] = '\0';        /* tie off string */
  503.   return T;
  504. }
  505.  
  506.  
  507. /* TCP/IP receive data
  508.  * Accepts: TCP/IP stream
  509.  * Returns: T if success, NIL otherwise
  510.  */
  511.  
  512. long tcp_getdata (stream)
  513.     TCPSTREAM *stream;
  514. {
  515.   fd_set fds;
  516.   FD_ZERO (&fds);        /* initialize selection vector */
  517.   if (stream->tcpsi < 0) return NIL;
  518.   while (stream->ictr < 1) {    /* if nothing in the buffer */
  519.     FD_SET (stream->tcpsi,&fds);/* set bit in selection vector */
  520.                 /* block and read */
  521.     if ((select (stream->tcpsi+1,&fds,0,0,0) < 0) ||
  522.     ((stream->ictr = read (stream->tcpsi,stream->ibuf,BUFLEN)) < 1)) {
  523.       close (stream->tcpsi);    /* nuke the socket */
  524.       if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  525.       stream->tcpsi = stream->tcpso = -1;
  526.       return NIL;
  527.     }
  528.     stream->iptr = stream->ibuf;/* point at TCP buffer */
  529.   }
  530.   return T;
  531. }
  532.  
  533. /* TCP/IP send string as record
  534.  * Accepts: TCP/IP stream
  535.  *        string pointer
  536.  * Returns: T if success else NIL
  537.  */
  538.  
  539. long tcp_soutr (stream,string)
  540.     TCPSTREAM *stream;
  541.     char *string;
  542. {
  543.   return tcp_sout (stream,string,(unsigned long) strlen (string));
  544. }
  545.  
  546.  
  547. /* TCP/IP send string
  548.  * Accepts: TCP/IP stream
  549.  *        string pointer
  550.  *        byte count
  551.  * Returns: T if success else NIL
  552.  */
  553.  
  554. long tcp_sout (stream,string,size)
  555.     TCPSTREAM *stream;
  556.     char *string;
  557.     unsigned long size;
  558. {
  559.   int i;
  560.   fd_set fds;
  561.   FD_ZERO (&fds);        /* initialize selection vector */
  562.   if (stream->tcpso < 0) return NIL;
  563.   while (size > 0) {        /* until request satisfied */
  564.     FD_SET (stream->tcpso,&fds);/* set bit in selection vector */
  565.     if ((select (stream->tcpso+1,0,&fds,0,0) < 0) ||
  566.     ((i = write (stream->tcpso,string,size)) < 0)) {
  567.       puts (strerror (errno));
  568.       close (stream->tcpsi);    /* nuke the socket */
  569.       if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  570.       stream->tcpsi = stream->tcpso = -1;
  571.       return NIL;
  572.     }
  573.     size -= i;            /* count this size */
  574.     string += i;
  575.   }
  576.   return T;            /* all done */
  577. }
  578.  
  579. /* TCP/IP close
  580.  * Accepts: TCP/IP stream
  581.  */
  582.  
  583. void tcp_close (stream)
  584.     TCPSTREAM *stream;
  585. {
  586.  
  587.   if (stream->tcpsi >= 0) {    /* no-op if no socket */
  588.     close (stream->tcpsi);    /* nuke the socket */
  589.     if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  590.     stream->tcpsi = stream->tcpso = -1;
  591.   }
  592.                 /* flush host names */
  593.   fs_give ((void **) &stream->host);
  594.   fs_give ((void **) &stream->localhost);
  595.   fs_give ((void **) &stream);    /* flush the stream */
  596. }
  597.  
  598.  
  599. /* TCP/IP get host name
  600.  * Accepts: TCP/IP stream
  601.  * Returns: host name for this stream
  602.  */
  603.  
  604. char *tcp_host (stream)
  605.     TCPSTREAM *stream;
  606. {
  607.   return stream->host;        /* return host name */
  608. }
  609.  
  610.  
  611. /* TCP/IP get local host name
  612.  * Accepts: TCP/IP stream
  613.  * Returns: local host name
  614.  */
  615.  
  616. char *tcp_localhost (stream)
  617.     TCPSTREAM *stream;
  618. {
  619.   return stream->localhost;    /* return local host name */
  620. }
  621.