]>
git.proxmox.com Git - swtpm.git/blob - src/swtpm_localca/swtpm_localca_utils.c
1 /* SPDX-License-Identifier: BSD-3-Clause */
3 * swtpm_localca_utils.c: Utility functions
5 * Author: Stefan Berger, stefanb@linux.ibm.com
7 * Copyright (c) IBM Corporation, 2021
17 #include <sys/types.h>
24 #include "swtpm_utils.h"
25 #include "swtpm_localca.h"
26 #include "swtpm_localca_utils.h"
28 /* Create a directory pat (and all its predecessors) if it doesn't exist */
29 int makedir(const char *dirname
, const char *purpose
)
33 if (stat(dirname
, &statbuf
) != 0) {
34 logit(gl_LOGFILE
, "Creating swtpm-localca dir '%s'.\n", dirname
);
35 if (g_mkdir_with_parents(dirname
, S_IRWXU
| S_IRWXG
| S_IXGRP
| S_IRGRP
) == -1) {
36 logerr(gl_LOGFILE
, "Could not create directory for '%s': %s\n",
37 purpose
, strerror(errno
));
44 /* Get a configuration value given its name */
45 gchar
*get_config_value(gchar
**config_file_lines
, const gchar
*configname
, const gchar
*fallback
)
47 g_autofree gchar
*regex
= g_strdup_printf("^%s[[:space:]]*=[[:space:]]*([^#\n]*).*", configname
);
53 if (regcomp(&preg
, regex
, REG_EXTENDED
) != 0) {
54 logerr(gl_LOGFILE
, "Internal error: Could not compile regex\n");
58 for (idx
= 0; config_file_lines
[idx
] != NULL
; idx
++) {
59 const gchar
*line
= config_file_lines
[idx
];
60 if (regexec(&preg
, line
, 2, pmatch
, 0) == 0) {
61 g_autofree gchar
*tmp
= NULL
;
63 tmp
= g_strndup(&line
[pmatch
[1].rm_so
],
64 pmatch
[1].rm_eo
- pmatch
[1].rm_so
);
66 result
= resolve_string(tmp
);
74 result
= g_strdup(fallback
);
75 //printf("Found match for %s: |%s|\n", configname, result);
80 /* Extract all environment variables from the config file and add them to
81 * the given environent.
82 * Environment variable lines must start with 'env:' and must not contain
83 * trailing spaces or a comment starting with '#'
85 int get_config_envvars(gchar
**config_file_lines
, gchar
***env
)
87 const char *regex
= "^env:([a-zA-Z_][a-zA-Z_0-9]*)[[:space:]]*=[[:space:]]*([^\n]*)";
92 if (regcomp(&preg
, regex
, REG_EXTENDED
) != 0) {
93 logerr(gl_LOGFILE
, "Internal error: Could not compile regex\n");
97 for (idx
= 0; config_file_lines
[idx
] != NULL
; idx
++) {
98 const gchar
*line
= config_file_lines
[idx
];
99 if (regexec(&preg
, line
, 3, pmatch
, 0) == 0) {
100 g_autofree gchar
*key
= NULL
, *value
= NULL
;
102 key
= g_strndup(&line
[pmatch
[1].rm_so
],
103 pmatch
[1].rm_eo
- pmatch
[1].rm_so
);
104 value
= g_strndup(&line
[pmatch
[2].rm_so
],
105 pmatch
[2].rm_eo
- pmatch
[2].rm_so
);
106 *env
= g_environ_setenv(*env
, key
, value
, TRUE
);
115 /* flock a file; the file descriptor for the file to unlock later on is returned */
116 int lock_file(const gchar
*lockfile
)
119 mode_t mode
= S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
;
121 lockfd
= open(lockfile
, O_RDWR
| O_CREAT
, mode
);
123 logerr(gl_LOGFILE
, "Could not open lockfile %s: %s\n", lockfile
, strerror(errno
));
127 if (flock(lockfd
, LOCK_EX
) < 0) {
128 logerr(gl_LOGFILE
, "Could not lock file %s: %s\n", lockfile
, strerror(errno
));
135 /* unlock a file previously locked using lock_file */
136 void unlock_file(int lockfd
) {
138 flock(lockfd
, LOCK_UN
);
143 /* Replace a few characters in vmid so it can be used by CommonName in cert */
144 void vmid_replacechars(char *vmid
) {
148 while ((c
= vmid
[i
])) {
151 // https://github.com/gnutls/gnutls/blob/gnutls_3_6_x/lib/x509/x509_dn.c#L167
152 if (i
== 0 || vmid
[i
- 1] != '\\')