home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Networking / SambaManager / samba-1.9.17p4 / source / pcap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-30  |  14.6 KB  |  560 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    printcap parsing
  5.    Copyright (C) Karl Auer 1993-1997
  6.  
  7.    Re-working by Martin Kiff, 1994
  8.    
  9.    Re-written again by Andrew Tridgell
  10.    
  11.    This program is free software; you can redistribute it and/or modify
  12.    it under the terms of the GNU General Public License as published by
  13.    the Free Software Foundation; either version 2 of the License, or
  14.    (at your option) any later version.
  15.    
  16.    This program is distributed in the hope that it will be useful,
  17.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.    GNU General Public License for more details.
  20.    
  21.    You should have received a copy of the GNU General Public License
  22.    along with this program; if not, write to the Free Software
  23.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25.  
  26. /*
  27.  *  Parse printcap file.
  28.  *
  29.  *  This module does exactly one thing - it looks into the printcap file
  30.  *  and tells callers if a specified string appears as a printer name.
  31.  *
  32.  *  The way this module looks at the printcap file is very simplistic.
  33.  *  Only the local printcap file is inspected (no searching of NIS
  34.  *  databases etc).
  35.  *
  36.  *  There are assumed to be one or more printer names per record, held
  37.  *  as a set of sub-fields separated by vertical bar symbols ('|') in the
  38.  *  first field of the record. The field separator is assumed to be a colon
  39.  *  ':' and the record separator a newline.
  40.  * 
  41.  *  Lines ending with a backspace '\' are assumed to flag that the following
  42.  *  line is a continuation line so that a set of lines can be read as one
  43.  *  printcap entry.
  44.  *
  45.  *  A line stating with a hash '#' is assumed to be a comment and is ignored
  46.  *  Comments are discarded before the record is strung together from the
  47.  *  set of continuation lines.
  48.  *
  49.  *  Opening a pipe for "lpc status" and reading that would probably 
  50.  *  be pretty effective. Code to do this already exists in the freely
  51.  *  distributable PCNFS server code.
  52.  */
  53.  
  54. #include "includes.h"
  55.  
  56. #include "smb.h"
  57.  
  58. extern int DEBUGLEVEL;
  59.  
  60. #ifdef AIX
  61. /*  ******************************************
  62.      Extend for AIX system and qconfig file
  63.        from 'boulard@univ-rennes1.fr
  64.     ****************************************** */
  65. static int strlocate(char *xpLine,char *xpS)
  66. {
  67.     int iS,iL,i,iRet;
  68.     char *p;
  69.     iS = strlen(xpS);
  70.     iL = strlen(xpLine);
  71.  
  72.     iRet = 0;
  73.     p = xpLine;
  74.     while (iL >= iS)
  75.     {
  76.         if (strncmp(p,xpS,iS) == 0) {iRet =1;break;};
  77.         p++;
  78.         iL--;
  79.     }
  80.     /*DEBUG(3,(" strlocate %s in line '%s',ret=%d\n",xpS,xpLine,iRet));*/
  81.     
  82.     return(iRet);
  83. }
  84.     
  85.     
  86. /* ******************************************************************* */
  87. /* *    Scan qconfig and search all virtual printer (device printer) * */
  88. /* ******************************************************************* */
  89. static void ScanQconfig_fn(char *psz,void (*fn)())
  90. {
  91.     int iLg,iEtat;
  92.     FILE *pfile;
  93.     char *line,*p;
  94.     pstring name,comment;
  95.     line  = NULL;
  96.     *name = 0;
  97.     *comment = 0;
  98.  
  99.     if ((pfile = fopen(psz, "r")) == NULL)
  100.     {
  101.           DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz));
  102.           return;
  103.     }
  104.  
  105.     iEtat = 0;
  106.     /* scan qconfig file for searching <printername>:    */
  107.     for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
  108.     {
  109.         if (*line == '*' || *line == 0)
  110.         continue;
  111.         switch (iEtat)
  112.         {
  113.             case 0: /* locate an entry */
  114.              if (*line == '\t' || *line == ' ') continue;
  115.              if ((p=strchr(line,':')))
  116.              {
  117.                  *p = '\0';
  118.                 p = strtok(line,":");
  119.                 if (strcmp(p,"bsh")!=0)
  120.                   {
  121.                     strcpy(name,p);
  122.                     iEtat = 1;
  123.                     continue;
  124.                   }
  125.              }
  126.              break;
  127.             case 1: /* scanning device stanza */
  128.              if (*line == '*' || *line == 0) continue;
  129.                if (*line != '\t' && *line != ' ')
  130.                {
  131.                  /* name is found without stanza device  */
  132.                  /* probably a good printer ???        */
  133.                  fn(name,comment);
  134.                  iEtat = 0;
  135.                  continue;
  136.                 }
  137.               
  138.                 if (strlocate(line,"backend"))
  139.                 {
  140.                   /* it's a device, not a virtual printer*/
  141.                   iEtat = 0;
  142.                 }
  143.                 else if (strlocate(line,"device"))
  144.                 {
  145.                   /* it's a good virtual printer */
  146.                   fn(name,comment);
  147.                   iEtat = 0;
  148.                   continue;
  149.                 }
  150.                 break;
  151.         }
  152.     }
  153.     fclose(pfile);
  154. }
  155.  
  156. /* Scan qconfig file and locate de printername */
  157.  
  158. static BOOL ScanQconfig(char *psz,char *pszPrintername)
  159. {
  160.     int iLg,iEtat;
  161.     FILE *pfile;
  162.     char *pName;
  163.     char *line;
  164.  
  165.     pName = NULL;
  166.     line  = NULL;
  167.     if ((pszPrintername!= NULL) && ((iLg = strlen(pszPrintername)) > 0))
  168.      pName = malloc(iLg+10);
  169.     if (pName == NULL)
  170.     {
  171.         DEBUG(0,(" Unable to allocate memory for printer %s\n",pszPrintername));
  172.         return(False);
  173.     }
  174.     if ((pfile = fopen(psz, "r")) == NULL)
  175.     {
  176.           DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz));
  177.           free(pName);
  178.           return(False);
  179.     }
  180.     sprintf(pName,"%s:",pszPrintername);
  181.     iLg = strlen(pName);
  182.     /*DEBUG(3,( " Looking for entry %s\n",pName));*/
  183.     iEtat = 0;
  184.     /* scan qconfig file for searching <printername>:    */
  185.     for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
  186.     {
  187.         if (*line == '*' || *line == 0)
  188.         continue;
  189.         switch (iEtat)
  190.         {
  191.             case 0: /* scanning entry */
  192.              if (strncmp(line,pName,iLg) == 0)
  193.              {
  194.                  iEtat = 1;
  195.                  continue;
  196.              }
  197.              break;
  198.             case 1: /* scanning device stanza */
  199.              if (*line == '*' || *line == 0) continue;
  200.                if (*line != '\t' && *line != ' ')
  201.                {
  202.                  /* name is found without stanza device  */
  203.                  /* probably a good printer ???        */
  204.                  free (line);
  205.                  free(pName);
  206.                  fclose(pfile);
  207.                  return(True);
  208.                 }
  209.               
  210.                 if (strlocate(line,"backend"))
  211.                 {
  212.                   /* it's a device, not a virtual printer*/
  213.                   iEtat = 0;
  214.                 }
  215.                 else if (strlocate(line,"device"))
  216.                 {
  217.                   /* it's a good virtual printer */
  218.                   free (line);
  219.                   free(pName);
  220.                   fclose(pfile);
  221.                   return(True);
  222.                 }
  223.                 break;
  224.         }
  225.     }
  226.     free (pName);
  227.     fclose(pfile);
  228.     return(False);
  229. }
  230.  
  231. #endif
  232. /***************************************************************************
  233. Scan printcap file pszPrintcapname for a printer called pszPrintername. 
  234. Return True if found, else False. Returns False on error, too, after logging 
  235. the error at level 0. For generality, the printcap name may be passed - if
  236. passed as NULL, the configuration will be queried for the name.
  237. ***************************************************************************/
  238. #ifdef USE_NETINFO
  239. BOOL pcap_file_printername_ok(char *pszPrintername, char *pszPrintcapname);
  240.  
  241. BOOL pcap_printername_ok(char *pszPrintername, char *pszPrintcapname)
  242. {
  243. /*
  244.  * The code to use when accesssing netinfo on NEXTSTEP/OPENSTEP
  245.  * This file is included in ../pcap.c.
  246.  */
  247.  
  248.     ni_status        status;
  249.     ni_name            name;
  250.     ni_id                ndir;
  251.     ni_idlist        ndirs;
  252.     void                *handle;
  253.     ni_proplist    props;
  254.  
  255.     int                    i,j,k;
  256.  
  257.     DEBUG(10, ("get_smbpwnam: opening netinfo for samba printers.\n"));
  258.  
  259.     /*
  260.      * Scan the NetInfo hierarchy, starting from the local level,
  261.      * until either an entry is found or we are past the top level.
  262.      */
  263.     handle = NULL;
  264.     status = ni_search_for_dir(S_PRINTERDIR, ".", &handle, &ndir, 5, 0, 1);
  265.  
  266.     while (status == NI_OK) {
  267.     
  268.         /* Read the directory and all its subdirectories. */
  269.         status = ni_children(handle, &ndir, &ndirs);
  270.         if (status != NI_OK) {
  271.             ni_free(handle);
  272.             DEBUG(0, ("Unable to get netinfo entries for printers.\n"));
  273.             return;
  274.         }            
  275.  
  276.  
  277.         /*
  278.          * For each subdirectory, scan the properties for the S_PRINTERNBNAME property.
  279.          * If any is present, compare to the name passed.
  280.          */
  281.         for (k = 0; k < ndirs.ni_idlist_len; k++) {
  282.  
  283.             /* Read the directory and all its properties, set the indexes. */
  284.             ndir.nii_object = ndirs.ni_idlist_val[k];
  285.             status = ni_read(handle, &ndir, &props);
  286.             if (status != NI_OK) {
  287.                 DEBUG(0, ("Unable to get netinfo entries for printer with ID %d.\n", ndirs.ni_idlist_val[i]));
  288.                 continue;
  289.             }            
  290.  
  291.             name = NULL;
  292.             for (i = 0; i < props.ni_proplist_len; i++) {
  293.                 if (!strcmp(PWENTNAME(i), S_PRINTERNBNAME))
  294.                     name = PWENTVALU(i);                    
  295.             }
  296.  
  297.             if ((name != NULL) && (strequal(name, pszPrintername))) {
  298.           /* return the name! */
  299.           strcpy(pszPrintername, name);
  300.                 ni_proplist_free(&props);
  301.                 ni_idlist_free(&ndirs);
  302.                 ni_free(handle);
  303.           return(True);          
  304.             }
  305.             ni_proplist_free(&props);
  306.         }
  307.         ni_idlist_free(&ndirs);
  308.  
  309.         status = ni_search_for_dir(S_PRINTERDIR, "..", &handle, &ndir, 5, 0, 1);
  310.     } 
  311.     
  312.     if (strcmp("netinfo", pszPrintcapname)) {
  313.         DEBUG(0, ("No Entry found in NetInfo. Trying flatfile %s.\n", pszPrintcapname));
  314.         return pcap_file_printername_ok(pszPrintername, pszPrintcapname);
  315.     }
  316.     
  317.   return(False);
  318. }
  319.  
  320. #define   pcap_printername_ok pcap_file_printername_ok
  321. #endif /* USE_NETINFO */
  322. BOOL pcap_printername_ok(char *pszPrintername, char *pszPrintcapname)
  323. {
  324.   char *line=NULL;
  325.   char *psz;
  326.   char *p,*q;
  327.   FILE *pfile;
  328.  
  329.   if (pszPrintername == NULL || pszPrintername[0] == '\0')
  330.     {
  331.       DEBUG(0,( "Attempt to locate null printername! Internal error?\n"));
  332.       return(False);
  333.     }
  334.  
  335.   /* only go looking if printcap name supplied */
  336.   if ((psz = pszPrintcapname) == NULL || psz[0] == '\0')
  337.     if (((psz = lp_printcapname()) == NULL) || (psz[0] == '\0'))
  338.       {
  339.     DEBUG(0,( "No printcap file name configured!\n"));
  340.     return(False);
  341.       }
  342. #ifdef AIX
  343.   if (strlocate(psz,"/qconfig") != NULL)
  344.      return(ScanQconfig(psz,pszPrintername));
  345. #endif
  346.   if ((pfile = fopen(psz, "r")) == NULL)
  347.     {
  348.       DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
  349.       return(False);
  350.     }
  351.  
  352.   for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
  353.     {
  354.       if (*line == '#' || *line == 0)
  355.     continue;
  356.  
  357.       /* now we have a real printer line - cut it off at the first : */      
  358.       p = strchr(line,':');
  359.       if (p) *p = 0;
  360.       
  361.       /* now just check if the name is in the list */
  362.       /* NOTE: I avoid strtok as the fn calling this one may be using it */
  363.       for (p=line; p; p=q)
  364.     {
  365.       if ((q = strchr(p,'|'))) *q++ = 0;
  366.  
  367.       if (strequal(p,pszPrintername))
  368.         {
  369.           /* normalise the case */
  370.           strcpy(pszPrintername,p);
  371.           free(line);
  372.           fclose(pfile);
  373.           return(True);          
  374.         }
  375.       p = q;
  376.     }
  377.     }
  378.  
  379.  
  380.   fclose(pfile);
  381.   return(False);
  382. }
  383.  
  384.  
  385. /***************************************************************************
  386. run a function on each printer name in the printcap file. The function is 
  387. passed the primary name and the comment (if possible)
  388. ***************************************************************************/
  389. #ifdef USE_NETINFO
  390. void pcap_file_printer_fn(void (*fn)());
  391.  
  392. void pcap_printer_fn(void (*fn)())
  393. {
  394.     ni_status        status;
  395.     ni_name            name, nbname, comment, driver, printcom, lprmcom, lpqcom,
  396.                             lpresumecom, lppausecom;
  397.     ni_id                ndir;
  398.     ni_idlist        ndirs;
  399.     void                *handle;
  400.     ni_proplist    props;
  401.  
  402.     int                    i,j,k;
  403.  
  404.     DEBUG(10, ("get_smbpwnam: opening netinfo for samba printers.\n"));
  405.  
  406.     /*
  407.      * Scan the NetInfo hierarchy, starting from the local level,
  408.      * until either an entry is found or we are past the top level.
  409.      */
  410.     handle = NULL;
  411.     status = ni_search_for_dir(S_PRINTERDIR, ".", &handle, &ndir, 5, 0, 1);
  412.  
  413.     while (status == NI_OK) {
  414.     
  415.         /* Read the directory and all its subdirectories. */
  416.         status = ni_children(handle, &ndir, &ndirs);
  417.         if (status != NI_OK) {
  418.             ni_free(handle);
  419.             DEBUG(0, ("Unable to get netinfo entries for printers.\n"));
  420.             return;
  421.         }            
  422.  
  423.         /*
  424.          * For each subdirectory, scan the properties for the S_PRINTERNBNAME property.
  425.          * If any is present, compare to the name passed.
  426.          */
  427.         for (k = 0; k < ndirs.ni_idlist_len; k++) {
  428.  
  429.             /* Read the directory and all its properties, set the indexes. */
  430.             ndir.nii_object = ndirs.ni_idlist_val[k];
  431.             status = ni_read(handle, &ndir, &props);
  432.             if (status != NI_OK) {
  433.                 DEBUG(0, ("Unable to get netinfo entries for printer with ID %d.\n", ndirs.ni_idlist_val[i]));
  434.                 continue;
  435.             }            
  436.  
  437.             name = NULL;
  438.             nbname = NULL;
  439.             comment = "From NetInfo";
  440.             driver = NULL;
  441.             printcom = NULL;
  442.             lprmcom = NULL;
  443.             lpqcom = NULL;
  444.             lpresumecom = NULL;
  445.             lppausecom  = NULL;
  446.  
  447.             for (i = 0; i < props.ni_proplist_len; i++) {
  448.                 if (!strcmp(PWENTNAME(i), S_PRINTERRNAME))
  449.                     name = PWENTVALU(i);
  450.                 else if (!strcmp(PWENTNAME(i), S_PRINTERNBNAME))
  451.                     nbname = PWENTVALU(i);
  452.                 else if (!strcmp(PWENTNAME(i), S_PRINTERCOMMENT))
  453.                     comment = PWENTVALU(i);
  454.                 else if (!strcmp(PWENTNAME(i), S_PRINTERDRIVER))
  455.                     driver = PWENTVALU(i);
  456.                 else if (!strcmp(PWENTNAME(i), S_PRINTCOMMAND))
  457.                     printcom = PWENTVALU(i);
  458.                 else if (!strcmp(PWENTNAME(i), S_LPRMCOMMAND))
  459.                     lprmcom = PWENTVALU(i);
  460.                 else if (!strcmp(PWENTNAME(i), S_LPQCOMMAND))
  461.                     lpqcom = PWENTVALU(i);
  462.                 else if (!strcmp(PWENTNAME(i), S_LPRESUMECOMMAND))
  463.                     lpresumecom = PWENTVALU(i);
  464.                 else if (!strcmp(PWENTNAME(i), S_LPPAUSECOMMAND))
  465.                     lppausecom = PWENTVALU(i);
  466.             }
  467.  
  468.             if (nbname != NULL)
  469.                 fn(nbname, name, comment, driver, printcom, lprmcom, lpqcom, lpresumecom, lppausecom);
  470.  
  471.             ni_proplist_free(&props);
  472.         }
  473.         ni_idlist_free(&ndirs);
  474.         
  475.         status = ni_search_for_dir(S_PRINTERDIR, "..", &handle, &ndir, 5, 0, 1);
  476.     } 
  477.     
  478.     if (strcmp("netinfo", lp_printcapname()))
  479.         pcap_file_printer_fn(fn);
  480. }
  481. #define pcap_printer_fn pcap_file_printer_fn
  482. #endif
  483. void pcap_printer_fn(void (*fn)())
  484. {
  485.   pstring name,comment;
  486.   char *line;
  487.   char *psz;
  488.   char *p,*q;
  489.   FILE *pfile;
  490.  
  491.   /* only go looking if printcap name supplied */
  492.   if (((psz = lp_printcapname()) == NULL) || (psz[0] == '\0'))
  493.     {
  494.       DEBUG(0,( "No printcap file name configured!\n"));
  495.       return;
  496.     }
  497.  
  498. #ifdef AIX
  499.   if (strlocate(psz,"/qconfig") != NULL)
  500.   {
  501.       ScanQconfig_fn(psz,fn);
  502.      return;
  503.   }
  504. #endif
  505.   if ((pfile = fopen(psz, "r")) == NULL)
  506.     {
  507.       DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
  508.       return;
  509.     }
  510.  
  511.   for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
  512.     {
  513.       if (*line == '#' || *line == 0)
  514.     continue;
  515.  
  516.       /* now we have a real printer line - cut it off at the first : */      
  517.       p = strchr(line,':');
  518.       if (p) *p = 0;
  519.       
  520.       /* now find the most likely printer name and comment 
  521.        this is pure guesswork, but it's better than nothing */
  522.       *name = 0;
  523.       *comment = 0;
  524.       for (p=line; p; p=q)
  525.     {
  526.       BOOL has_punctuation;
  527.       if ((q = strchr(p,'|'))) *q++ = 0;
  528.  
  529.       has_punctuation = (strchr(p,' ') || strchr(p,'(') || strchr(p,')'));
  530.  
  531.       if (strlen(p)>strlen(comment) && has_punctuation)
  532.         {
  533.           StrnCpy(comment,p,sizeof(comment)-1);
  534.           continue;
  535.         }
  536.  
  537.       if (strlen(p) <= 8 && strlen(p)>strlen(name) && !has_punctuation)
  538.         {
  539.           if (!*comment) pstrcpy(comment,name);
  540.           pstrcpy(name,p);
  541.           continue;
  542.         }
  543.  
  544.       if (!strchr(comment,' ') && 
  545.           strlen(p) > strlen(comment))
  546.         {
  547.           StrnCpy(comment,p,sizeof(comment)-1);
  548.           continue;
  549.         }
  550.     }
  551.  
  552.       comment[60] = 0;
  553.       name[8] = 0;
  554.  
  555.       if (*name)
  556.                 fn(name, name, comment, NULL, NULL, NULL, NULL, NULL, NULL);
  557.     }
  558.   fclose(pfile);
  559. }
  560.