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  * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
8  */
9 
10 /*
11  * Include necessary headers...
12  */
13 
14 #include "cups-private.h"
15 
16 
17 /*
18  * 'main()' - Main entry.
19  */
20 
21 int					/* O - Exit status */
main(int argc,char * argv[])22 main(int  argc,				/* I - Number of command-line arguments */
23      char *argv[])			/* I - Command-line arguments */
24 {
25   http_t	*http;			/* HTTP connection */
26   char		scheme[HTTP_MAX_URI],	/* Scheme from URI */
27 		hostname[HTTP_MAX_URI],	/* Hostname from URI */
28 		username[HTTP_MAX_URI],	/* Username:password from URI */
29 		resource[HTTP_MAX_URI];	/* Resource from URI */
30   int		port;			/* Port number from URI */
31   http_trust_t	trust;			/* Trust evaluation for connection */
32   cups_array_t	*hcreds,		/* Credentials from connection */
33 		*tcreds;		/* Credentials from trust store */
34   char		hinfo[1024],		/* String for connection credentials */
35 		tinfo[1024];		/* String for trust store credentials */
36   static const char *trusts[] =		/* Trust strings */
37   { "OK", "Invalid", "Changed", "Expired", "Renewed", "Unknown" };
38 
39 
40  /*
41   * Check command-line...
42   */
43 
44   if (argc != 2)
45   {
46     puts("Usage: ./testcreds hostname");
47     puts("       ./testcreds https://hostname[:port]");
48     return (1);
49   }
50 
51   if (!strncmp(argv[1], "https://", 8))
52   {
53    /*
54     * Connect to the host and validate credentials...
55     */
56 
57     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)
58     {
59       printf("ERROR: Bad URI \"%s\".\n", argv[1]);
60       return (1);
61     }
62 
63     if ((http = httpConnect2(hostname, port, NULL, AF_UNSPEC, HTTP_ENCRYPTION_ALWAYS, 1, 30000, NULL)) == NULL)
64     {
65       printf("ERROR: Unable to connect to \"%s\" on port %d: %s\n", hostname, port, cupsLastErrorString());
66       return (1);
67     }
68 
69     puts("HTTP Credentials:");
70     if (!httpCopyCredentials(http, &hcreds))
71     {
72       trust = httpCredentialsGetTrust(hcreds, hostname);
73 
74       httpCredentialsString(hcreds, hinfo, sizeof(hinfo));
75 
76       printf("    Certificate Count: %d\n", cupsArrayCount(hcreds));
77       if (trust == HTTP_TRUST_OK)
78         puts("    Trust: OK");
79       else
80         printf("    Trust: %s (%s)\n", trusts[trust], cupsLastErrorString());
81       printf("    Expiration: %s\n", httpGetDateString(httpCredentialsGetExpiration(hcreds)));
82       printf("    IsValidName: %d\n", httpCredentialsAreValidForName(hcreds, hostname));
83       printf("    String: \"%s\"\n", hinfo);
84 
85       httpFreeCredentials(hcreds);
86     }
87     else
88       puts("    Not present (error).");
89 
90     puts("");
91   }
92   else
93   {
94    /*
95     * Load stored credentials...
96     */
97 
98     strlcpy(hostname, argv[1], sizeof(hostname));
99   }
100 
101   printf("Trust Store for \"%s\":\n", hostname);
102 
103   if (!httpLoadCredentials(NULL, &tcreds, hostname))
104   {
105     httpCredentialsString(tcreds, tinfo, sizeof(tinfo));
106 
107     printf("    Certificate Count: %d\n", cupsArrayCount(tcreds));
108     printf("    Expiration: %s\n", httpGetDateString(httpCredentialsGetExpiration(tcreds)));
109     printf("    IsValidName: %d\n", httpCredentialsAreValidForName(tcreds, hostname));
110     printf("    String: \"%s\"\n", tinfo);
111 
112     httpFreeCredentials(tcreds);
113   }
114   else
115     puts("    Not present.");
116 
117   return (0);
118 }
119