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