home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / networking / amitcp / lpd.lha / lpd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-11  |  3.3 KB  |  180 lines

  1. /*****************************************************************************
  2. Simple line printer daemon
  3.  
  4. ATTENTION: NO SECURITY FEATURES IMPLEMENTED
  5.  
  6.  
  7.   ©1994 by Juergen Schubert
  8. *****************************************************************************/
  9.  
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15. #include <errno.h>
  16. #include <string.h>
  17. #include <clib/netlib_protos.h>
  18. #include <sys/syslog.h>
  19.  
  20.  
  21.  
  22. /*********** Acknowledgement *****************************************/
  23. char    *sp = "";
  24. #define ack()    write(sock, sp, 1);
  25.  
  26.  
  27.  
  28. #define BUFSIZE 512
  29.  
  30.  
  31. #define true 1
  32. #define false 0
  33.  
  34. const char *ver="$VER: LPD 0.1 (11.09.94)";
  35.  
  36.  
  37.  
  38. int
  39. main(int argc, char **argv)
  40. {
  41.   char buffer[BUFSIZE];
  42.   char device[BUFSIZE];
  43.   char from[BUFSIZE];
  44.   char *cp;
  45.   long size, i;
  46.   int log=false;
  47.   int outhandle;
  48.  
  49.   unsigned int amt, j;
  50.  
  51.   int sock = init_inet_daemon();
  52.  
  53.   if ( argc > 1 )
  54.      log = true;
  55.  
  56.   openlog("lpd", LOG_PID, LOG_LPR);
  57.  
  58.   ack();
  59.  
  60.  
  61.   /******************* Read output device name ***********************/
  62.   cp = buffer;
  63.   do
  64.   {
  65.      if ((size = read(sock, cp, 1)) != 1)
  66.      {
  67.         if (size < 0)
  68.           return 10;
  69.      }
  70.   } while (*cp++ != '\n');
  71.   *--cp = '\0';
  72.   cp = buffer;
  73.  
  74.   ack();
  75.  
  76.  
  77.   if ( cp[0] != '\2' )
  78.   {
  79.      syslog ( LOG_ERR, "Request not supported : %d", cp[0] );
  80.      write ( sock, &cp[0], 1 );
  81.      return 5;
  82.   }
  83.  
  84.   strcpy ( device, &cp[1] );
  85.  
  86.   if ( !strcmp ( device, "lpr" ))  /* Remote host tries to print to default printer */
  87.     strcpy ( device, "par:" );     /* Default printing to par: */
  88.  
  89.   if ( log )
  90.      syslog ( LOG_INFO, "Remote host tries to print to %s", device );
  91.  
  92.  
  93.  
  94.   /******************* Read size and sending host  **********************/
  95.   cp = buffer;
  96.   do
  97.   {
  98.      if ((size = read(sock, cp, 1)) != 1)
  99.      {
  100.         if (size < 0)
  101.           return 10;
  102.      }
  103.   } while (*cp++ != '\n');
  104.   *--cp = '\0';
  105.   cp = buffer;
  106.  
  107.   ack();
  108.  
  109.   switch (*cp++)
  110.   {
  111.      /* \1 = Cleanup not possible */
  112.  
  113.      case '\2':    /* read cf file */
  114.      case '\3': /* read df file */
  115.         size = 0;
  116.         while (*cp >= '0' && *cp <= '9')
  117.            size = size * 10 + (*cp++ - '0');
  118.         if (*cp++ != ' ')
  119.            break;
  120.  
  121.         strcpy(from, cp + 6);
  122.         break;
  123.  
  124.      default:
  125.         syslog ( LOG_ERR, "Cleanup not supported" );
  126.         return 5;
  127.   }
  128.  
  129.   if ( log )
  130.      syslog ( LOG_INFO, "Host %s is printing %ld bytes.", from, size );
  131.  
  132.  
  133.   /****************** Get the job ************************************/
  134.  
  135.   outhandle = open ( device, O_APPEND|O_CREAT|O_WRONLY, S_IREAD|S_IWRITE );
  136.   if ( !outhandle )
  137.   {
  138.      write ( sock, &cp[0], 1 );
  139.      syslog ( LOG_ERR, "Can't get an output handle for %s", device );
  140.      return 10;
  141.   }
  142.  
  143.   for (i = 0; i < size; i += BUFSIZ)
  144.   {
  145.      amt = BUFSIZ;
  146.      cp = buffer;
  147.      if (i + amt > size)
  148.         amt = size - i;
  149.      do
  150.      {
  151.         j = read(sock, cp, amt);
  152.         if (j <= 0)
  153.         {
  154.            syslog (LOG_ERR,"Lost connection");
  155.            break;
  156.         }
  157.         amt -= j;
  158.         cp += j;
  159.      } while (amt > 0);
  160.  
  161.      amt = BUFSIZ;
  162.      cp = buffer;
  163.      if (i + amt > size)
  164.         amt = size - i;
  165.  
  166.      write ( outhandle, cp, amt );
  167.   }
  168.  
  169.   ack();
  170.   ack();
  171.   close ( outhandle );
  172.  
  173.   if ( log )
  174.      syslog ( LOG_INFO, "Printing successfully finished" );
  175.  
  176.  
  177.   close ( sock );
  178.   exit ( 0 );
  179. }
  180.