]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/usersys.c
Update all references to OS X to macOS.
[thirdparty/cups.git] / cups / usersys.c
index 77ee7fc65d7a5e0965c32d7fa64f6174eaefe430..9a23d74800109ddf95fceed938a58dce11e34e36 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * "$Id$"
- *
  * User, system, and password routines for CUPS.
  *
  * Copyright 2007-2015 by Apple Inc.
  * Local constants...
  */
 
+#ifdef __APPLE__
+#  define kCUPSPrintingPrefs   CFSTR("org.cups.PrintingPrefs")
+#  define kAllowAnyRootKey     CFSTR("AllowAnyRoot")
+#  define kAllowExpiredCertsKey        CFSTR("AllowExpiredCerts")
+#  define kEncryptionKey       CFSTR("Encryption")
+#  define kGSSServiceNameKey   CFSTR("GSSServiceName")
+#  define kSSLOptionsKey       CFSTR("SSLOptions")
+#  define kTrustOnFirstUseKey  CFSTR("TrustOnFirstUse")
+#  define kValidateCertsKey    CFSTR("ValidateCerts")
+#endif /* __APPLE__ */
+
 #define _CUPS_PASSCHAR '*'             /* Character that is echoed for password */
 
 
@@ -47,7 +56,8 @@ typedef struct _cups_client_conf_s    /**** client.conf config data ****/
 #ifdef HAVE_SSL
   int                  ssl_options;    /* SSLOptions values */
 #endif /* HAVE_SSL */
-  int                  any_root,       /* Allow any (e.g., self-signed) root */
+  int                  trust_first,    /* Trust on first use? */
+                       any_root,       /* Allow any (e.g., self-signed) root */
                        expired_certs,  /* Allow expired certs */
                        validate_certs; /* Validate certificates */
   http_encryption_t    encryption;     /* Encryption setting */
@@ -65,9 +75,15 @@ typedef struct _cups_client_conf_s   /**** client.conf config data ****/
  * Local functions...
  */
 
+#ifdef __APPLE__
+static int     cups_apple_get_boolean(CFStringRef key, int *value);
+static int     cups_apple_get_string(CFStringRef key, char *value, size_t valsize);
+#endif /* __APPLE__ */
+static int     cups_boolean_value(const char *value);
 static void    cups_finalize_client_conf(_cups_client_conf_t *cc);
 static void    cups_init_client_conf(_cups_client_conf_t *cc);
 static void    cups_read_client_conf(cups_file_t *fp, _cups_client_conf_t *cc);
+static void    cups_set_default_ipp_port(_cups_globals_t *cg);
 static void    cups_set_encryption(_cups_client_conf_t *cc, const char *value);
 #ifdef HAVE_GSSAPI
 static void    cups_set_gss_service_name(_cups_client_conf_t *cc, const char *value);
@@ -140,7 +156,7 @@ cupsGetPassword(const char *prompt) /* I - Prompt string */
  * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to
  * do so in each thread for the same function to be used.
  *
- * @since CUPS 1.4/OS X 10.6@
+ * @since CUPS 1.4/macOS 10.6@
  */
 
 const char *                           /* O - Password */
@@ -198,7 +214,7 @@ cupsServer(void)
  * in a program. Multi-threaded programs that override the callback need to do
  * so in each thread for the same callback to be used.
  *
- * @since CUPS 1.5/OS X 10.7@
+ * @since CUPS 1.5/macOS 10.7@
  */
 
 void
@@ -222,7 +238,7 @@ cupsSetClientCertCB(
  * program. Multi-threaded programs that override the setting need to do so in
  * each thread for the same setting to be used.
  *
- * @since CUPS 1.5/OS X 10.7@
+ * @since CUPS 1.5/macOS 10.7@
  */
 
 int                                    /* O - Status of call (0 = success) */
@@ -310,7 +326,7 @@ cupsSetPasswordCB(cups_password_cb_t cb)/* I - Callback function */
  * in a program. Multi-threaded programs that override the callback need to do
  * so in each thread for the same callback to be used.
  *
- * @since CUPS 1.4/OS X 10.6@
+ * @since CUPS 1.4/macOS 10.6@
  */
 
 void
@@ -382,6 +398,9 @@ cupsSetServer(const char *server)   /* I - Server name */
       cg->ipp_port = atoi(port);
     }
 
+    if (!cg->ipp_port)
+      cups_set_default_ipp_port(cg);
+
     if (cg->server[0] == '/')
       strlcpy(cg->servername, "localhost", sizeof(cg->servername));
     else
@@ -392,6 +411,7 @@ cupsSetServer(const char *server)   /* I - Server name */
     cg->server[0]      = '\0';
     cg->servername[0]  = '\0';
     cg->server_version = 20;
+    cg->ipp_port       = 0;
   }
 
   if (cg->http)
@@ -411,7 +431,7 @@ cupsSetServer(const char *server)   /* I - Server name */
  * in a program. Multi-threaded programs that override the callback need to do
  * so in each thread for the same callback to be used.
  *
- * @since CUPS 1.5/OS X 10.7@
+ * @since CUPS 1.5/macOS 10.7@
  */
 
 void
@@ -456,7 +476,7 @@ cupsSetUser(const char *user)               /* I - User name */
  * Setting the string to NULL forces the default value containing the CUPS
  * version, IPP version, and operating system version and architecture.
  *
- * @since CUPS 1.7/OS X 10.9@
+ * @since CUPS 1.7/macOS 10.9@
  */
 
 void
@@ -531,7 +551,7 @@ cupsUser(void)
 /*
  * 'cupsUserAgent()' - Return the default HTTP User-Agent string.
  *
- * @since CUPS 1.7/OS X 10.9@
+ * @since CUPS 1.7/macOS 10.9@
  */
 
 const char *                           /* O - User-Agent string */
@@ -705,6 +725,8 @@ _cupsGetPassword(const char *prompt)        /* I - Prompt string */
 
   noecho = original;
   noecho.c_lflag &= (tcflag_t)~(ICANON | ECHO | ECHOE | ISIG);
+  noecho.c_cc[VMIN]  = 1;
+  noecho.c_cc[VTIME] = 0;
 
   if (tcsetattr(tty, TCSAFLUSH, &noecho))
   {
@@ -908,17 +930,7 @@ _cupsSetDefaults(void)
     cupsSetServer(cc.server_name);
 
   if (!cg->ipp_port)
-  {
-    const char *ipp_port;              /* IPP_PORT environment variable */
-
-    if ((ipp_port = getenv("IPP_PORT")) != NULL)
-    {
-      if ((cg->ipp_port = atoi(ipp_port)) <= 0)
-        cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
-    }
-    else
-      cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
-  }
+    cups_set_default_ipp_port(cg);
 
   if (!cg->user[0])
     strlcpy(cg->user, cc.user, sizeof(cg->user));
@@ -928,6 +940,9 @@ _cupsSetDefaults(void)
     strlcpy(cg->gss_service_name, cc.gss_service_name, sizeof(cg->gss_service_name));
 #endif /* HAVE_GSSAPI */
 
+  if (cg->trust_first < 0)
+    cg->trust_first = cc.trust_first;
+
   if (cg->any_root < 0)
     cg->any_root = cc.any_root;
 
@@ -943,6 +958,57 @@ _cupsSetDefaults(void)
 }
 
 
+#ifdef __APPLE__
+/*
+ * 'cups_apple_get_boolean()' - Get a boolean setting from the CUPS preferences.
+ */
+
+static int                             /* O - 1 if set, 0 otherwise */
+cups_apple_get_boolean(
+    CFStringRef key,                   /* I - Key (name) */
+    int         *value)                        /* O - Boolean value */
+{
+  Boolean      bval,                   /* Preference value */
+               bval_set;               /* Value is set? */
+
+
+  bval = CFPreferencesGetAppBooleanValue(key, kCUPSPrintingPrefs, &bval_set);
+
+  if (bval_set)
+    *value = (int)bval;
+
+  return ((int)bval_set);
+}
+
+
+/*
+ * 'cups_apple_get_string()' - Get a string setting from the CUPS preferences.
+ */
+
+static int                             /* O - 1 if set, 0 otherwise */
+cups_apple_get_string(
+    CFStringRef key,                   /* I - Key (name) */
+    char        *value,                        /* O - String value */
+    size_t      valsize)               /* I - Size of value buffer */
+{
+  CFStringRef  sval;                   /* String value */
+
+
+  if ((sval = CFPreferencesCopyAppValue(key, kCUPSPrintingPrefs)) != NULL)
+  {
+    Boolean result = CFStringGetCString(sval, value, (CFIndex)valsize, kCFStringEncodingUTF8);
+
+    CFRelease(sval);
+
+    if (result)
+      return (1);
+  }
+
+  return (0);
+}
+#endif /* __APPLE__ */
+
+
 /*
  * 'cups_boolean_value()' - Convert a string to a boolean value.
  */
@@ -965,6 +1031,9 @@ cups_finalize_client_conf(
   const char   *value;                 /* Environment variable */
 
 
+  if ((value = getenv("CUPS_TRUSTFIRST")) != NULL)
+    cc->trust_first = cups_boolean_value(value);
+
   if ((value = getenv("CUPS_ANYROOT")) != NULL)
     cc->any_root = cups_boolean_value(value);
 
@@ -992,6 +1061,9 @@ cups_finalize_client_conf(
   * Then apply defaults for those values that haven't been set...
   */
 
+  if (cc->trust_first < 0)
+    cc->trust_first = 1;
+
   if (cc->any_root < 0)
     cc->any_root = 1;
 
@@ -999,7 +1071,7 @@ cups_finalize_client_conf(
     cc->encryption = HTTP_ENCRYPTION_IF_REQUESTED;
 
   if (cc->expired_certs < 0)
-    cc->expired_certs = 1;
+    cc->expired_certs = 0;
 
 #ifdef HAVE_GSSAPI
   if (!cc->gss_service_name[0])
@@ -1014,10 +1086,7 @@ cups_finalize_client_conf(
     * domain socket if it exists and has the right permissions...
     */
 
-    struct stat        sockinfo;               /* Domain socket information */
-
-    if (!stat(CUPS_DEFAULT_DOMAINSOCKET, &sockinfo) &&
-       (sockinfo.st_mode & S_IRWXO) == S_IRWXO)
+    if (!access(CUPS_DEFAULT_DOMAINSOCKET, R_OK))
       cups_set_server_name(cc, CUPS_DEFAULT_DOMAINSOCKET);
     else
 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
@@ -1092,9 +1161,38 @@ cups_init_client_conf(
   memset(cc, 0, sizeof(_cups_client_conf_t));
 
   cc->encryption     = (http_encryption_t)-1;
+  cc->trust_first    = -1;
   cc->any_root       = -1;
   cc->expired_certs  = -1;
   cc->validate_certs = -1;
+
+ /*
+  * Load settings from the org.cups.PrintingPrefs plist (which trump
+  * everything...)
+  */
+
+#ifdef __APPLE__
+  char sval[1024];                     /* String value */
+  int  bval;                           /* Boolean value */
+
+  if (cups_apple_get_boolean(kAllowAnyRootKey, &bval))
+    cc->any_root = bval;
+
+  if (cups_apple_get_boolean(kAllowExpiredCertsKey, &bval))
+    cc->expired_certs = bval;
+
+  if (cups_apple_get_string(kEncryptionKey, sval, sizeof(sval)))
+    cups_set_encryption(cc, sval);
+
+  if (cups_apple_get_string(kSSLOptionsKey, sval, sizeof(sval)))
+    cups_set_ssl_options(cc, sval);
+
+  if (cups_apple_get_boolean(kTrustOnFirstUseKey, &bval))
+    cc->trust_first = bval;
+
+  if (cups_apple_get_boolean(kValidateCertsKey, &bval))
+    cc->validate_certs = bval;
+#endif /* __APPLE__ */
 }
 
 
@@ -1123,7 +1221,7 @@ cups_read_client_conf(
       cups_set_encryption(cc, value);
 #ifndef __APPLE__
    /*
-    * The ServerName directive is not supported on OS X due to app
+    * The ServerName directive is not supported on macOS due to app
     * sandboxing restrictions, i.e. not all apps request network access.
     */
     else if (!_cups_strcasecmp(line, "ServerName") && value)
@@ -1131,6 +1229,8 @@ cups_read_client_conf(
 #endif /* !__APPLE__ */
     else if (!_cups_strcasecmp(line, "User") && value)
       cups_set_user(cc, value);
+    else if (!_cups_strcasecmp(line, "TrustOnFirstUse") && value)
+      cc->trust_first = cups_boolean_value(value);
     else if (!_cups_strcasecmp(line, "AllowAnyRoot") && value)
       cc->any_root = cups_boolean_value(value);
     else if (!_cups_strcasecmp(line, "AllowExpiredCerts") &&
@@ -1150,6 +1250,26 @@ cups_read_client_conf(
 }
 
 
+/*
+ * 'cups_set_default_ipp_port()' - Set the default IPP port value.
+ */
+
+static void
+cups_set_default_ipp_port(
+    _cups_globals_t *cg)               /* I - Global data */
+{
+  const char   *ipp_port;              /* IPP_PORT environment variable */
+
+
+  if ((ipp_port = getenv("IPP_PORT")) != NULL)
+  {
+    if ((cg->ipp_port = atoi(ipp_port)) <= 0)
+      cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
+  }
+  else
+    cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
+}
+
 /*
  * 'cups_set_encryption()' - Set the Encryption value.
  */
@@ -1222,7 +1342,7 @@ cups_set_ssl_options(
 
   for (start = temp; *start; start = end)
   {
-   /* 
+   /*
     * Find end of keyword...
     */
 
@@ -1251,7 +1371,7 @@ cups_set_ssl_options(
 
   cc->ssl_options = options;
 
-  DEBUG_printf(("4cups_set_ssl_options(cc=%p, value=\"%s\") options=%x", cc, value, options));
+  DEBUG_printf(("4cups_set_ssl_options(cc=%p, value=\"%s\") options=%x", (void *)cc, value, options));
 }
 #endif /* HAVE_SSL */
 
@@ -1267,8 +1387,3 @@ cups_set_user(
 {
   strlcpy(cc->user, value, sizeof(cc->user));
 }
-
-
-/*
- * End of "$Id$".
- */