Home | History | Annotate | Download | only in cups
      1 /*
      2  * HTTP credentials test program for CUPS.
      3  *
      4  * Copyright 2007-2016 by Apple Inc.
      5  * Copyright 1997-2006 by Easy Software Products.
      6  *
      7  * These coded instructions, statements, and computer programs are the
      8  * property of Apple Inc. and are protected by Federal copyright
      9  * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
     10  * which should have been included with this file.  If this file is
     11  * missing or damaged, see the license at "http://www.cups.org/".
     12  *
     13  * This file is subject to the Apple OS-Developed Software exception.
     14  */
     15 
     16 /*
     17  * Include necessary headers...
     18  */
     19 
     20 #include "cups-private.h"
     21 
     22 
     23 /*
     24  * 'main()' - Main entry.
     25  */
     26 
     27 int					/* O - Exit status */
     28 main(int  argc,				/* I - Number of command-line arguments */
     29      char *argv[])			/* I - Command-line arguments */
     30 {
     31   http_t	*http;			/* HTTP connection */
     32   char		scheme[HTTP_MAX_URI],	/* Scheme from URI */
     33 		hostname[HTTP_MAX_URI],	/* Hostname from URI */
     34 		username[HTTP_MAX_URI],	/* Username:password from URI */
     35 		resource[HTTP_MAX_URI];	/* Resource from URI */
     36   int		port;			/* Port number from URI */
     37   http_trust_t	trust;			/* Trust evaluation for connection */
     38   cups_array_t	*hcreds,		/* Credentials from connection */
     39 		*tcreds;		/* Credentials from trust store */
     40   char		hinfo[1024],		/* String for connection credentials */
     41 		tinfo[1024];		/* String for trust store credentials */
     42   static const char *trusts[] =		/* Trust strings */
     43   { "OK", "Invalid", "Changed", "Expired", "Renewed", "Unknown" };
     44 
     45 
     46  /*
     47   * Check command-line...
     48   */
     49 
     50   if (argc != 2)
     51   {
     52     puts("Usage: ./testcreds hostname");
     53     puts("       ./testcreds https://hostname[:port]");
     54     return (1);
     55   }
     56 
     57   if (!strncmp(argv[1], "https://", 8))
     58   {
     59    /*
     60     * Connect to the host and validate credentials...
     61     */
     62 
     63     if (httpSeparateURI(HTTP_URI_CODING_MOST, argv[1], scheme, sizeof(scheme), username, sizeof(username), hostname, sizeof(hostname), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
     64     {
     65       printf("ERROR: Bad URI \"%s\".\n", argv[1]);
     66       return (1);
     67     }
     68 
     69     if ((http = httpConnect2(hostname, port, NULL, AF_UNSPEC, HTTP_ENCRYPTION_ALWAYS, 1, 30000, NULL)) == NULL)
     70     {
     71       printf("ERROR: Unable to connect to \"%s\" on port %d: %s\n", hostname, port, cupsLastErrorString());
     72       return (1);
     73     }
     74 
     75     puts("HTTP Credentials:");
     76     if (!httpCopyCredentials(http, &hcreds))
     77     {
     78       trust = httpCredentialsGetTrust(hcreds, hostname);
     79 
     80       httpCredentialsString(hcreds, hinfo, sizeof(hinfo));
     81 
     82       printf("    Certificate Count: %d\n", cupsArrayCount(hcreds));
     83       if (trust == HTTP_TRUST_OK)
     84         puts("    Trust: OK");
     85       else
     86         printf("    Trust: %s (%s)\n", trusts[trust], cupsLastErrorString());
     87       printf("    Expiration: %s\n", httpGetDateString(httpCredentialsGetExpiration(hcreds)));
     88       printf("    IsValidName: %d\n", httpCredentialsAreValidForName(hcreds, hostname));
     89       printf("    String: \"%s\"\n", hinfo);
     90 
     91       httpFreeCredentials(hcreds);
     92     }
     93     else
     94       puts("    Not present (error).");
     95 
     96     puts("");
     97   }
     98   else
     99   {
    100    /*
    101     * Load stored credentials...
    102     */
    103 
    104     strlcpy(hostname, argv[1], sizeof(hostname));
    105   }
    106 
    107   printf("Trust Store for \"%s\":\n", hostname);
    108 
    109   if (!httpLoadCredentials(NULL, &tcreds, hostname))
    110   {
    111     httpCredentialsString(tcreds, tinfo, sizeof(tinfo));
    112 
    113     printf("    Certificate Count: %d\n", cupsArrayCount(tcreds));
    114     printf("    Expiration: %s\n", httpGetDateString(httpCredentialsGetExpiration(tcreds)));
    115     printf("    IsValidName: %d\n", httpCredentialsAreValidForName(tcreds, hostname));
    116     printf("    String: \"%s\"\n", tinfo);
    117 
    118     httpFreeCredentials(tcreds);
    119   }
    120   else
    121     puts("    Not present.");
    122 
    123   return (0);
    124 }
    125