]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/adminutil.c
Merge changes from CUPS 1.4svn-r8606.
[thirdparty/cups.git] / cups / adminutil.c
index 70fdce425634dc3c54858f5881b3d8dddf7c9c12..e2c5ea827dbe49154805a3e5ea9003c194157386 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * "$Id: adminutil.c 6930 2007-09-08 00:28:06Z mike $"
+ * "$Id: adminutil.c 7850 2008-08-20 00:07:25Z mike $"
  *
  *   Administration utility API definitions for the Common UNIX Printing
  *   System (CUPS).
  *
- *   Copyright 2007-2008 by Apple Inc.
+ *   Copyright 2007-2009 by Apple Inc.
  *   Copyright 2001-2007 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
@@ -75,11 +75,13 @@ static void         write_option(cups_file_t *dstfp, int order,
 
 /*
  * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
+ *
+ * @since CUPS 1.2/Mac OS X 10.5@
  */
 
 char *                                 /* O - PPD file or NULL */
 cupsAdminCreateWindowsPPD(
-    http_t     *http,                  /* I - Connection to server */
+    http_t     *http,                  /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
     const char *dest,                  /* I - Printer or class */
     char       *buffer,                        /* I - Filename buffer */
     int        bufsize)                        /* I - Size of filename buffer */
@@ -121,6 +123,9 @@ cupsAdminCreateWindowsPPD(
   if (buffer)
     *buffer = '\0';
 
+  if (!http)
+    http = _cupsConnect();
+
   if (!http || !dest || !buffer || bufsize < 2)
     return (NULL);
 
@@ -257,9 +262,9 @@ cupsAdminCreateWindowsPPD(
       if ((ptr = strchr(line, ':')) == NULL)
       {
         snprintf(line, sizeof(line),
-                 _cupsLangString(language, _("Missing value on line %d!")),
-                 linenum);
-        _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
+                _cupsLangString(language, _("Missing value on line %d!")),
+                linenum);
+        _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line, 0);
 
         cupsFileClose(srcfp);
         cupsFileClose(dstfp);
@@ -278,7 +283,7 @@ cupsAdminCreateWindowsPPD(
                 _cupsLangString(language,
                                 _("Missing double quote on line %d!")),
                 linenum);
-        _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
+        _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line, 0);
 
         cupsFileClose(srcfp);
         cupsFileClose(dstfp);
@@ -297,7 +302,7 @@ cupsAdminCreateWindowsPPD(
                 _cupsLangString(language,
                                 _("Bad option + choice on line %d!")),
                 linenum);
-        _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
+        _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line, 0);
 
         cupsFileClose(srcfp);
         cupsFileClose(dstfp);
@@ -340,10 +345,7 @@ cupsAdminCreateWindowsPPD(
 
   if (linenum == 0)
   {
-    snprintf(line, sizeof(line),
-             _cupsLangString(language, _("Empty PPD file!")),
-             linenum);
-    _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
+    _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, _("Empty PPD file!"), 1);
 
     cupsFileClose(dstfp);
     unlink(buffer);
@@ -386,7 +388,7 @@ cupsAdminCreateWindowsPPD(
   {
     write_option(dstfp, jclorder ++, "cupsJobSheetsStart", "Start Banner",
                  "job-sheets", suppattr, defattr, 0, 2);
-    write_option(dstfp, jclorder ++, "cupsJobSheetsEnd", "End Banner",
+    write_option(dstfp, jclorder, "cupsJobSheetsEnd", "End Banner",
                  "job-sheets", suppattr, defattr, 1, 2);
   }
 
@@ -401,6 +403,8 @@ cupsAdminCreateWindowsPPD(
 
 /*
  * 'cupsAdminExportSamba()' - Export a printer to Samba.
+ *
+ * @since CUPS 1.2/Mac OS X 10.5@
  */
 
 int                                    /* O - 1 on success, 0 on failure */
@@ -431,7 +435,7 @@ cupsAdminExportSamba(
 
   if (!dest || !ppd || !samba_server || !samba_user || !samba_password)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, NULL);
+    _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
     return (0);
   }
 
@@ -441,7 +445,7 @@ cupsAdminExportSamba(
 
   if ((fp = cupsTempFile2(authfile, sizeof(authfile))) == NULL)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
+    _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
     return (0);
   }
 
@@ -494,7 +498,7 @@ cupsAdminExportSamba(
                               _("Unable to copy Windows 2000 printer "
                                 "driver files (%d)!")), status);
 
-      _cupsSetError(IPP_INTERNAL_ERROR, message);
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
       if (logfile)
        _cupsLangPrintf(logfile, "%s\n", message);
@@ -529,7 +533,7 @@ cupsAdminExportSamba(
                                 _("Unable to copy CUPS printer driver "
                                   "files (%d)!")), status);
 
-       _cupsSetError(IPP_INTERNAL_ERROR, message);
+       _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
        if (logfile)
          _cupsLangPrintf(logfile, "%s\n", message);
@@ -572,7 +576,7 @@ cupsAdminExportSamba(
                               _("Unable to install Windows 2000 printer "
                                 "driver files (%d)!")), status);
 
-      _cupsSetError(IPP_INTERNAL_ERROR, message);
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
       if (logfile)
        _cupsLangPrintf(logfile, "%s\n", message);
@@ -617,7 +621,7 @@ cupsAdminExportSamba(
                               _("Unable to copy Windows 9x printer "
                                 "driver files (%d)!")), status);
 
-      _cupsSetError(IPP_INTERNAL_ERROR, message);
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
       if (logfile)
        _cupsLangPrintf(logfile, "%s\n", message);
@@ -646,7 +650,7 @@ cupsAdminExportSamba(
                               _("Unable to install Windows 9x printer "
                                 "driver files (%d)!")), status);
 
-      _cupsSetError(IPP_INTERNAL_ERROR, message);
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
       if (logfile)
        _cupsLangPrintf(logfile, "%s\n", message);
@@ -698,7 +702,7 @@ cupsAdminExportSamba(
                               _("Unable to copy 64-bit Windows printer "
                                 "driver files (%d)!")), status);
 
-      _cupsSetError(IPP_INTERNAL_ERROR, message);
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
       if (logfile)
        _cupsLangPrintf(logfile, "%s\n", message);
@@ -733,7 +737,7 @@ cupsAdminExportSamba(
                                 _("Unable to copy 64-bit CUPS printer driver "
                                   "files (%d)!")), status);
 
-       _cupsSetError(IPP_INTERNAL_ERROR, message);
+       _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
        if (logfile)
          _cupsLangPrintf(logfile, "%s\n", message);
@@ -776,7 +780,7 @@ cupsAdminExportSamba(
                               _("Unable to install Windows 2000 printer "
                                 "driver files (%d)!")), status);
 
-      _cupsSetError(IPP_INTERNAL_ERROR, message);
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
       if (logfile)
        _cupsLangPrintf(logfile, "%s\n", message);
@@ -801,13 +805,13 @@ cupsAdminExportSamba(
                                "are installed!")),
               sizeof(message));
 
-    _cupsSetError(IPP_NOT_FOUND, message);
+    _cupsSetError(IPP_NOT_FOUND, message, 0);
     _cupsLangPrintf(logfile, "%s\n", message);
   }
 
   if (have_drivers == 0)
   {
-    _cupsSetError(IPP_NOT_FOUND, message);
+    _cupsSetError(IPP_NOT_FOUND, message, 0);
 
     unlink(authfile);
 
@@ -828,7 +832,7 @@ cupsAdminExportSamba(
                             _("Unable to set Windows printer driver (%d)!")),
                             status);
 
-    _cupsSetError(IPP_INTERNAL_ERROR, message);
+    _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
     if (logfile)
       _cupsLangPrintf(logfile, "%s\n", message);
@@ -850,12 +854,12 @@ cupsAdminExportSamba(
  * The returned settings should be freed with cupsFreeOptions() when
  * you are done with them.
  *
- * @since CUPS 1.3@
+ * @since CUPS 1.3/Mac OS X 10.5@
  */
 
 int                                    /* O - 1 on success, 0 on failure */
 cupsAdminGetServerSettings(
-    http_t        *http,               /* I - Connection to server */
+    http_t        *http,               /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
     int           *num_settings,       /* O - Number of settings */
     cups_option_t **settings)          /* O - Settings */
 {
@@ -874,7 +878,7 @@ cupsAdminGetServerSettings(
 
 int                                    /* O - 1 on success, 0 on failure */
 _cupsAdminGetServerSettings(
-    http_t        *http,               /* I - Connection to server */
+    http_t        *http,               /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
     int           *num_settings,       /* O - Number of settings */
     cups_option_t **settings)          /* O - Settings */
 {
@@ -893,9 +897,65 @@ _cupsAdminGetServerSettings(
   * Range check input...
   */
 
+  if (!http)
+  {
+   /*
+    * See if we are connected to the same server...
+    */
+
+    if (cg->http)
+    {
+     /*
+      * Compare the connection hostname, port, and encryption settings to
+      * the cached defaults; these were initialized the first time we
+      * connected...
+      */
+
+      if (strcmp(cg->http->hostname, cg->server) ||
+          cg->ipp_port != _httpAddrPort(cg->http->hostaddr) ||
+         (cg->http->encryption != cg->encryption &&
+          cg->http->encryption == HTTP_ENCRYPT_NEVER))
+      {
+       /*
+       * Need to close the current connection because something has changed...
+       */
+
+       httpClose(cg->http);
+       cg->http = NULL;
+      }
+    }
+
+   /*
+    * (Re)connect as needed...
+    */
+
+    if (!cg->http)
+    {
+      if ((cg->http = _httpCreate(cupsServer(), ippPort(),
+                                  cupsEncryption())) == NULL)
+      {
+       if (errno)
+         _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
+       else
+         _cupsSetError(IPP_SERVICE_UNAVAILABLE,
+                       _("Unable to connect to host."), 1);
+
+       if (num_settings)
+         *num_settings = 0;
+
+       if (settings)
+         *settings = NULL;
+
+       return (0);
+      }
+    }
+
+    http = cg->http;
+  }
+
   if (!http || !num_settings || !settings)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, NULL);
+    _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
 
     if (num_settings)
       *num_settings = 0;
@@ -922,9 +982,9 @@ _cupsAdminGetServerSettings(
 
 
       snprintf(message, sizeof(message),
-               _cupsLangString(cupsLangDefault(), _("open of %s failed: %s")),
+               _cupsLangString(cupsLangDefault(), _("Open of %s failed: %s")),
                cupsdconf, strerror(errno));
-      _cupsSetError(IPP_INTERNAL_ERROR, message);
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
     }
   }
   else
@@ -961,7 +1021,7 @@ _cupsAdminGetServerSettings(
       if (!value && strncmp(line, "</", 2))
         value = line + strlen(line);
 
-      if (!strcasecmp(line, "Port") || !strcasecmp(line, "Listen"))
+      if ((!strcasecmp(line, "Port") || !strcasecmp(line, "Listen")) && value)
       {
        char    *port;                  /* Pointer to port number, if any */
 
@@ -1017,7 +1077,7 @@ _cupsAdminGetServerSettings(
       {
        in_policy = 0;
       }
-      else if (!strcasecmp(line, "<Limit") && in_policy)
+      else if (!strcasecmp(line, "<Limit") && in_policy && value)
       {
        /*
        * See if the policy limit is for the Cancel-Job operation...
@@ -1050,7 +1110,7 @@ _cupsAdminGetServerSettings(
       {
        cancel_policy = 0;
       }
-      else if (!strcasecmp(line, "<Location"))
+      else if (!strcasecmp(line, "<Location") && value)
       {
         in_admin_location = !strcasecmp(value, "/admin");
        in_location       = 1;
@@ -1060,7 +1120,7 @@ _cupsAdminGetServerSettings(
        in_admin_location = 0;
        in_location       = 0;
       }
-      else if (!strcasecmp(line, "Allow") &&
+      else if (!strcasecmp(line, "Allow") && value &&
                strcasecmp(value, "localhost") && strcasecmp(value, "127.0.0.1")
 #ifdef AF_LOCAL
               && *value != '/'
@@ -1075,7 +1135,13 @@ _cupsAdminGetServerSettings(
         else if (!strcasecmp(value, "all"))
          remote_any = 1;
       }
-      else if (line[0] != '<' && !in_location && !in_policy)
+      else if (line[0] != '<' && !in_location && !in_policy &&
+              strcasecmp(line, "Allow") &&
+               strcasecmp(line, "AuthType") &&
+              strcasecmp(line, "Deny") &&
+              strcasecmp(line, "Order") &&
+              strcasecmp(line, "Require") &&
+              strcasecmp(line, "Satisfy"))
         cg->cupsd_num_settings = cupsAddOption(line, value,
                                               cg->cupsd_num_settings,
                                               &(cg->cupsd_settings));
@@ -1139,12 +1205,12 @@ _cupsAdminGetServerSettings(
 /*
  * 'cupsAdminSetServerSettings()' - Set settings on the server.
  *
- * @since CUPS 1.3@
+ * @since CUPS 1.3/Mac OS X 10.5@
  */
 
 int                                    /* O - 1 on success, 0 on failure */
 cupsAdminSetServerSettings(
-    http_t        *http,               /* I - Connection to server */
+    http_t        *http,               /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
     int           num_settings,                /* I - Number of settings */
     cups_option_t *settings)           /* I - Settings */
 {
@@ -1160,7 +1226,7 @@ cupsAdminSetServerSettings(
 
 int                                    /* O - 1 on success, 0 on failure */
 _cupsAdminSetServerSettings(
-    http_t        *http,               /* I - Connection to server */
+    http_t        *http,               /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
     int           num_settings,                /* I - Number of settings */
     cups_option_t *settings)           /* I - Settings */
 {
@@ -1213,9 +1279,12 @@ _cupsAdminSetServerSettings(
   * Range check input...
   */
 
+  if (!http)
+    http = _cupsConnect();
+
   if (!http || !num_settings || !settings)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, NULL);
+    _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
 
     return (0);
   }
@@ -1224,12 +1293,12 @@ _cupsAdminSetServerSettings(
   * Get the cupsd.conf file...
   */
 
-  if ((status = get_cupsd_conf(http, cg, 0, cupsdconf, sizeof(cupsdconf),
-                               &remote)) == HTTP_OK)
+  if (get_cupsd_conf(http, cg, 0, cupsdconf, sizeof(cupsdconf),
+                     &remote) == HTTP_OK)
   {
     if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL)
     {
-      _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
+      _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
       return (0);
     }
   }
@@ -1386,7 +1455,7 @@ _cupsAdminSetServerSettings(
     if (remote)
       unlink(cupsdconf);
 
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
+    _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
     return (0);
   }
 
@@ -1509,27 +1578,33 @@ _cupsAdminSetServerSettings(
          {
            cupsFilePuts(temp, "BrowseAllow all\n");
 
-           if (!remotep || !*remotep)
-             cupsFilePuts(temp, "BrowseRemoteProtocols "
-                                CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS "\n");
-            else if (remotep)
-             cupsFilePrintf(temp, "BrowseRemoteProtocols %s\n", remotep);
+           if (!remotep)
+             remotep = CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS;
+
+           cupsFilePrintf(temp, "BrowseRemoteProtocols %s\n", remotep);
           }
          else
            cupsFilePuts(temp, "BrowseRemoteProtocols\n");
 
+         cupsd_num_settings = cupsAddOption("BrowseRemoteProtocols", remotep,
+                                            cupsd_num_settings,
+                                            &cupsd_settings);
+
          if (new_share_printers)
          {
            cupsFilePuts(temp, "BrowseAddress @LOCAL\n");
 
-           if (!localp || !*localp)
-             cupsFilePuts(temp, "BrowseLocalProtocols "
-                                CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS "\n");
-            else if (localp)
-             cupsFilePrintf(temp, "BrowseLocalProtocols %s\n", localp);
+           if (!localp)
+             localp = CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS;
+
+           cupsFilePrintf(temp, "BrowseLocalProtocols %s\n", localp);
          }
          else
            cupsFilePuts(temp, "BrowseLocalProtocols\n");
+
+         cupsd_num_settings = cupsAddOption("BrowseLocalProtocols", localp,
+                                            cupsd_num_settings,
+                                            &cupsd_settings);
         }
        else
        {
@@ -1552,7 +1627,7 @@ _cupsAdminSetServerSettings(
       else
       {
         cupsFilePuts(temp, "# Show general information in error_log.\n");
-       cupsFilePuts(temp, "LogLevel info\n");
+       cupsFilePuts(temp, "LogLevel " CUPS_DEFAULT_LOG_LEVEL "\n");
       }
     }
     else if (!strcasecmp(line, "<Policy"))
@@ -1576,7 +1651,7 @@ _cupsAdminSetServerSettings(
                             "  <Limit Cancel-Job>\n"
                             "    Order deny,allow\n"
                             "    Require user @OWNER "
-                            CUPS_DEFAULT_PRINTADMIN_AUTH "\n"
+                            CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n"
                             "  </Limit>\n");
       }
 
@@ -1723,7 +1798,7 @@ _cupsAdminSetServerSettings(
                           "  <Limit Cancel-Job>\n"
                           "    Order deny,allow\n"
                           "    Require user @OWNER "
-                          CUPS_DEFAULT_PRINTADMIN_AUTH "\n"
+                          CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n"
                           "  </Limit>\n");
 
       in_cancel_job = 0;
@@ -1731,8 +1806,7 @@ _cupsAdminSetServerSettings(
     else if ((((in_admin_location || in_conf_location || in_root_location) &&
                remote_admin >= 0) ||
               (in_root_location && share_printers >= 0)) &&
-             (((!strcasecmp(line, "Allow") || !strcasecmp(line, "Deny")) &&
-              !strcasecmp(value, "@LOCAL")) ||
+             (!strcasecmp(line, "Allow") || !strcasecmp(line, "Deny") ||
              !strcasecmp(line, "Order")))
       continue;
     else if (in_cancel_job == 2)
@@ -1832,7 +1906,7 @@ _cupsAdminSetServerSettings(
     else
     {
       cupsFilePuts(temp, "# Show general information in error_log.\n");
-      cupsFilePuts(temp, "LogLevel info\n");
+      cupsFilePuts(temp, "LogLevel " CUPS_DEFAULT_LOG_LEVEL "\n");
     }
   }
 
@@ -1949,7 +2023,7 @@ _cupsAdminSetServerSettings(
                         "  <Limit Cancel-Job>\n"
                         "    Order deny,allow\n"
                         "    Require user @OWNER "
-                        CUPS_DEFAULT_PRINTADMIN_AUTH "\n"
+                        CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n"
                         "  </Limit>\n");
 
     cupsFilePuts(temp, "  <Limit All>\n"
@@ -2136,7 +2210,7 @@ do_samba_command(const char *command,     /* I - Command to run */
   if (logfile)
     _cupsLangPuts(logfile, "\n");
 
-  DEBUG_printf(("status=%d\n", status));
+  DEBUG_printf(("9do_samba_command: status=%d", status));
 
   if (WIFEXITED(status))
     return (WEXITSTATUS(status));
@@ -2194,7 +2268,7 @@ get_cupsd_conf(
       snprintf(message, sizeof(message),
                _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
                name, strerror(errno));
-      _cupsSetError(IPP_INTERNAL_ERROR, message);
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
       *name = '\0';
 
@@ -2216,7 +2290,7 @@ get_cupsd_conf(
     {
       *name = '\0';
 
-      _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
+      _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
 
       invalidate_cupsd_cache(cg);
 
@@ -2373,5 +2447,5 @@ write_option(cups_file_t     *dstfp,      /* I - PPD file */
 
 
 /*
- * End of "$Id: adminutil.c 6930 2007-09-08 00:28:06Z mike $".
+ * End of "$Id: adminutil.c 7850 2008-08-20 00:07:25Z mike $".
  */