]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/usersys.c
Import CUPS 1.4svn-r7356.
[thirdparty/cups.git] / cups / usersys.c
index cb25863f34f32acfdc12aad34d9210820b87ddf8..28b40d0be537dff00a28345ac4f5a02c6173d77d 100644 (file)
@@ -1,40 +1,32 @@
 /*
- * "$Id: usersys.c 5041 2006-02-01 16:54:50Z mike $"
+ * "$Id: usersys.c 6649 2007-07-11 21:46:42Z mike $"
  *
  *   User, system, and password routines for the Common UNIX Printing
  *   System (CUPS).
  *
+ *   Copyright 2007-2008 by Apple Inc.
  *   Copyright 1997-2006 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products and are protected by Federal
- *   copyright law.  Distribution and use rights are outlined in the file
- *   "LICENSE.txt" which should have been included with this file.  If this
- *   file is missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   property of Apple Inc. and are protected by Federal copyright
+ *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   which should have been included with this file.  If this file is
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
  *
  *   This file is subject to the Apple OS-Developed Software exception.
  *
  * Contents:
  *
- *   cupsEncryption()    - Get the default encryption settings.
- *   cupsGetPassword()   - Get a password from the user.
- *   cupsServer()        - Return the hostname of the default server.
- *   cupsSetEncryption() - Set the encryption preference.
- *   cupsSetPasswordCB() - Set the password callback for CUPS.
- *   cupsSetServer()     - Set the default server name.
- *   cupsSetUser()       - Set the default user name.
- *   cupsUser()          - Return the current users name.
- *   _cupsGetPassword()  - Get a password from the user.
+ *   cupsEncryption()        - Get the default encryption settings.
+ *   cupsGetPassword()       - Get a password from the user.
+ *   cupsServer()            - Return the hostname of the default server.
+ *   cupsSetEncryption()     - Set the encryption preference.
+ *   cupsSetPasswordCB()     - Set the password callback for CUPS.
+ *   cupsSetServer()         - Set the default server name.
+ *   cupsSetUser()           - Set the default user name.
+ *   cupsUser()              - Return the current users name.
+ *   _cupsGetPassword()      - Get a password from the user.
+ *   cups_open_client_conf() - Open the client.conf file.
  */
 
 /*
 #ifdef WIN32
 #  include <windows.h>
 #endif /* WIN32 */
+#include "debug.h"
+
+
+/*
+ * Local functions...
+ */
+
+static cups_file_t     *cups_open_client_conf(void);
 
 
 /*
  * 'cupsEncryption()' - Get the default encryption settings.
  *
  * The default encryption setting comes from the CUPS_ENCRYPTION
- * environment variable, then the ~/.cupsrc file, and finally the
+ * environment variable, then the ~/.cups/client.conf file, and finally the
  * /etc/cups/client.conf file. If not set, the default is
- * HTTP_ENCRYPT_IF_REQUESTED.
+ * @code HTTP_ENCRYPT_IF_REQUESTED@.
  */
 
 http_encryption_t                      /* O - Encryption settings */
@@ -64,7 +64,6 @@ cupsEncryption(void)
 {
   cups_file_t  *fp;                    /* client.conf file */
   char         *encryption;            /* CUPS_ENCRYPTION variable */
-  const char   *home;                  /* Home directory of user */
   char         line[1024],             /* Line from file */
                *value;                 /* Value on line */
   int          linenum;                /* Line number */
@@ -84,26 +83,13 @@ cupsEncryption(void)
     if ((encryption = getenv("CUPS_ENCRYPTION")) == NULL)
     {
      /*
-      * Next check to see if we have a $HOME/.cupsrc or client.conf file...
+      * No, open the client.conf file...
       */
 
-      if ((home = getenv("HOME")) != NULL)
-      {
-       snprintf(line, sizeof(line), "%s/.cupsrc", home);
-       fp = cupsFileOpen(line, "r");
-      }
-      else
-       fp = NULL;
-
-      if (fp == NULL)
-      {
-       snprintf(line, sizeof(line), "%s/client.conf", cg->cups_serverroot);
-       fp = cupsFileOpen(line, "r");
-      }
-
+      fp         = cups_open_client_conf();
       encryption = "IfRequested";
 
-      if (fp != NULL)
+      if (fp)
       {
        /*
        * Read the config file and look for an Encryption line...
@@ -147,7 +133,8 @@ cupsEncryption(void)
 /*
  * 'cupsGetPassword()' - Get a password from the user.
  *
- * Returns NULL if the user does not provide a password.
+ * Uses the current password callback function. Returns @code NULL@ if the
+ * user does not provide a password.
  */
 
 const char *                           /* O - Password */
@@ -164,7 +151,13 @@ cupsGetPassword(const char *prompt)        /* I - Prompt string */
 void
 cupsSetEncryption(http_encryption_t e) /* I - New encryption preference */
 {
-  _cupsGlobals()->encryption = e;
+  _cups_globals_t *cg = _cupsGlobals();        /* Pointer to library globals */
+
+
+  cg->encryption = e;
+
+  if (cg->http)
+    httpEncryption(cg->http, e);
 }
 
 
@@ -180,7 +173,6 @@ cupsServer(void)
 {
   cups_file_t  *fp;                    /* client.conf file */
   char         *server;                /* Pointer to server name */
-  const char   *home;                  /* Home directory of user */
   char         *port;                  /* Port number */
   char         line[1024],             /* Line from file */
                *value;                 /* Value on line */
@@ -204,22 +196,10 @@ cupsServer(void)
     if ((server = getenv("CUPS_SERVER")) == NULL)
     {
      /*
-      * Next check to see if we have a $HOME/.cupsrc or client.conf file...
+      * No environment variable, try the client.conf file...
       */
 
-      if ((home = getenv("HOME")) != NULL)
-      {
-       snprintf(line, sizeof(line), "%s/.cupsrc", home);
-       fp = cupsFileOpen(line, "r");
-      }
-      else
-       fp = NULL;
-
-      if (fp == NULL)
-      {
-       snprintf(line, sizeof(line), "%s/client.conf", cg->cups_serverroot);
-       fp = cupsFileOpen(line, "r");
-      }
+      fp = cups_open_client_conf();
 
 #ifdef CUPS_DEFAULT_DOMAINSOCKET
      /*
@@ -234,7 +214,7 @@ cupsServer(void)
 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
       server = "localhost";
 
-      if (fp != NULL)
+      if (fp)
       {
        /*
        * Read the config file and look for a ServerName line...
@@ -242,15 +222,21 @@ cupsServer(void)
 
         linenum = 0;
        while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum) != NULL)
+        {
+          DEBUG_printf(("cupsServer: %d: %s %s\n", linenum, line,
+                       value ? value : "(null)"));
+
          if (!strcasecmp(line, "ServerName") && value)
          {
           /*
            * Got it!
            */
 
+            DEBUG_puts("cupsServer: Got a ServerName line!");
            server = value;
            break;
          }
+        }
 
        cupsFileClose(fp);
       }
@@ -260,6 +246,8 @@ cupsServer(void)
     * Copy the server name over and set the port number, if any...
     */
 
+    DEBUG_printf(("cupsServer: Using server \"%s\"...\n", server));
+
     strlcpy(cg->server, server, sizeof(cg->server));
 
     if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL &&
@@ -267,6 +255,7 @@ cupsServer(void)
     {
       *port++ = '\0';
 
+      DEBUG_printf(("cupsServer: Using port %d...\n", atoi(port)));
       ippSetPort(atoi(port));
     }
 
@@ -283,7 +272,7 @@ cupsServer(void)
 /*
  * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
  *
- * Pass NULL to restore the default (console) password callback.
+ * Pass @code NULL@ to restore the default (console) password callback.
  */
 
 void
@@ -303,7 +292,7 @@ cupsSetPasswordCB(cups_password_cb_t cb)/* I - Callback function */
  * 'cupsSetServer()' - Set the default server name.
  *
  * The "server" string can be a fully-qualified hostname, a numeric
- * IPv4 or IPv6 address, or a domain socket pathname. Pass NULL to
+ * IPv4 or IPv6 address, or a domain socket pathname. Pass @code NULL@ to
  * restore the default server name.
  */
 
@@ -336,13 +325,19 @@ cupsSetServer(const char *server) /* I - Server name */
     cg->server[0]     = '\0';
     cg->servername[0] = '\0';
   }
+
+  if (cg->http)
+  {
+    httpClose(cg->http);
+    cg->http = NULL;
+  }
 }
 
 
 /*
  * 'cupsSetUser()' - Set the default user name.
  *
- * Pass NULL to restore the default user name.
+ * Pass @code NULL@ to restore the default user name.
  */
 
 void
@@ -469,5 +464,44 @@ _cupsGetPassword(const char *prompt)       /* I - Prompt string */
 
 
 /*
- * End of "$Id: usersys.c 5041 2006-02-01 16:54:50Z mike $".
+ * 'cups_open_client_conf()' - Open the client.conf file.
+ */
+
+static cups_file_t *                   /* O - File or NULL */
+cups_open_client_conf(void)
+{
+  cups_file_t  *fp;                    /* File */
+  const char   *home;                  /* Home directory of user */
+  char         filename[1024];         /* Filename */
+  _cups_globals_t *cg = _cupsGlobals();        /* Pointer to library globals */
+
+
+  if ((home = getenv("HOME")) != NULL)
+  {
+   /*
+    * Look for ~/.cups/client.conf or ~/.cupsrc...
+    */
+
+    snprintf(filename, sizeof(filename), "%s/.cups/client.conf", home);
+    if ((fp = cupsFileOpen(filename, "r")) != NULL)
+    {
+      DEBUG_printf(("cups_open_client_conf: Using \"%s\"...\n", filename));
+      return (fp);
+    }
+
+    snprintf(filename, sizeof(filename), "%s/.cupsrc", home);
+    if ((fp = cupsFileOpen(filename, "r")) != NULL)
+    {
+      DEBUG_printf(("cups_open_client_conf: Using \"%s\"...\n", filename));
+      return (fp);
+    }
+  }
+
+  snprintf(filename, sizeof(filename), "%s/client.conf", cg->cups_serverroot);
+  return (cupsFileOpen(filename, "r"));
+}
+
+
+/*
+ * End of "$Id: usersys.c 6649 2007-07-11 21:46:42Z mike $".
  */