Debian Codegraph @

src/common/cockpitwebcertificate.c

raw
1 /*
2 * Copyright (C) 2019 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include <assert.h>
9 #include <dirent.h>
10 #include <errno.h>
11 #include <err.h>
12 #include <fcntl.h>
13 #include <limits.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18
19 #include "common/cockpitconf.h"
20 #include "common/cockpitmemory.h"
21 #include "common/cockpitwebcertificate.h"
22
23 static int
24 filter_cert (const struct dirent *entry)
25 {
26 int len = strlen (entry->d_name);
27
28 /* check if entry ends with .crt or .cert */
29 return (len > 4 && strcmp (entry->d_name + len - 4, ".crt") == 0) ||
30 (len > 5 && strcmp (entry->d_name + len - 5, ".cert") == 0);
31 }
32
33 static char *
34 load_cert_from_dir (const char *dir_name,
35 char **error)
36 {
37 struct dirent **certs;
38 int n;
39 char *ret = NULL;
40
41 n = scandir (dir_name, &certs, filter_cert, alphasort);
42 if (n < 0)
43 {
44 if (errno != ENOENT)
45 asprintfx (error, "Error loading certificates from %s: %m", dir_name);
46 return NULL;
47 }
48
49 if (n > 0)
50 asprintfx (&ret, "%s/%s", dir_name, certs[n-1]->d_name);
51 while (n--)
52 free (certs[n]);
53 free (certs);
54 return ret;
55 }
56
57 /**
58 * cockpit_certificate_locate:
59 * @missing_ok: if "no certificate" is a valid result
60 * @error: a pointer to a place to store an error string
61 *
62 * Find Cockpit web server certificate in $XDG_CONFIG_DIRS/cockpit/ws-certs.d/.
63 * The asciibetically latest *.crt or *.cert file wins.
64 *
65 * Return certificate path on success, or %NULL if none is found or
66 * another error occurs (such as a permissions problem, etc).
67 *
68 * @error must be a pointer to a `char *` originally containing %NULL.
69 * It will be set to the error message in case of errors, and %NULL will
70 * be returned. If the error is "no certificate was found" and
71 * @missing_ok is %TRUE then %NULL will be returned, but @error will be
72 * left unset.
73 */
74 char *
75 cockpit_certificate_locate (bool missing_ok,
76 char **error)
77 {
78 const char * const *dirs = cockpit_conf_get_dirs ();
79
80 assert (error != NULL);
81 assert (*error == NULL);
82
83 for (int i = 0; dirs[i]; i++)
84 {
85 char *cert_dir;
86 char *cert_path;
87
88 asprintfx (&cert_dir, "%s/cockpit/ws-certs.d", dirs[i]);
89 cert_path = load_cert_from_dir (cert_dir, error);
90 free (cert_dir);
91
92 if (*error != NULL)
93 return NULL;
94
95 if (cert_path)
96 return cert_path;
97 }
98
99 if (!missing_ok)
100 asprintfx (error, "No certificate found in dir: %s/cockpit/ws-certs.d", dirs[0]);
101
102 return NULL;
103 }
104
105 /**
106 * cockpit_certificate_key_path:
107 *
108 * Return key file path for given certfile, i. e. replace ".crt" or ".cert"
109 * suffix with ".key". Invalid names exit the program. All usages of this
110 * function in our code control the file name, so that should not happen.
111 */
112 char *
113 cockpit_certificate_key_path (const char *certfile)
114 {
115 int len = strlen (certfile);
116 char *keypath = NULL;
117
118 /* .cert suffix case: chop off suffix, append ".key" */
119 if (len > 5 && strcmp (certfile + len - 5, ".cert") == 0)
120 asprintfx (&keypath, "%.*s.key", len - 5, certfile);
121 /* *.crt suffix case */
122 else if (len > 4 && strcmp (certfile + len - 4, ".crt") == 0)
123 asprintfx (&keypath, "%.*s.key", len - 4, certfile);
124 else
125 errx (EXIT_FAILURE, "internal error: invalid certificate file name: %s", certfile);
126
127 return keypath;
128 }