Skip to content

Commit 1578a3d

Browse files
committed
Add support for setting the output file mode
Resolves: #2 Signed-off-by: Stephen Gallagher <[email protected]>
1 parent d3dade3 commit 1578a3d

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/sscg.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ main(int argc, const char **argv)
125125
char *cert_file = NULL;
126126
char *cert_key_file = NULL;
127127

128+
int ca_mode = 0644;
129+
int ca_key_mode = 0600;
130+
int cert_mode = 0644;
131+
int cert_key_mode = 0600;
132+
128133
struct sscg_x509_cert *cacert;
129134
struct sscg_evp_pkey *cakey;
130135
struct sscg_x509_cert *svc_cert;
@@ -135,6 +140,8 @@ main(int argc, const char **argv)
135140
BIO *cert_out = NULL;
136141
BIO *cert_key_out = NULL;
137142

143+
FILE *fp;
144+
138145
/* Always use umask 077 for generating certificates and keys */
139146
umask(077);
140147

@@ -202,21 +209,37 @@ main(int argc, const char **argv)
202209
_("Path where the public CA certificate will be stored. (default: \"./ca.crt\")"),
203210
NULL,
204211
},
212+
{"ca-mode", '\0', POPT_ARG_INT, &ca_mode, 0,
213+
_("File mode of the created CA certificate. (default: 0644)"),
214+
_("0644"),
215+
},
205216
{"ca-key-file", '\0', POPT_ARG_STRING, &ca_key_file, 0,
206217
_("Path where the CA's private key will be stored. If unspecified, "
207218
"the key will be destroyed rather than written to the disk."),
208219
NULL,
209220
},
221+
{"ca-key-mode", '\0', POPT_ARG_INT, &ca_key_mode, 0,
222+
_("File mode of the created CA key. (default: 0600)"),
223+
_("0600"),
224+
},
210225
{"cert-file", '\0', POPT_ARG_STRING, &cert_file, 0,
211226
_("Path where the public service certificate will be stored. "
212227
"(default \"./service.pem\")"),
213228
NULL,
214229
},
230+
{"cert-mode", '\0', POPT_ARG_INT, &cert_mode, 0,
231+
_("File mode of the created certificate. (default: 0644)"),
232+
_("0644"),
233+
},
215234
{"cert-key-file", '\0', POPT_ARG_STRING, &cert_key_file, 0,
216235
_("Path where the service's private key will be stored. "
217236
"(default \"service-key.pem\")"),
218237
NULL,
219238
},
239+
{"cert-key-mode", '\0', POPT_ARG_INT, &cert_key_mode, 0,
240+
_("File mode of the created certificate key. (default: 0600)"),
241+
_("0600"),
242+
},
220243
POPT_TABLEEND
221244
};
222245

@@ -391,6 +414,8 @@ main(int argc, const char **argv)
391414

392415
sret = PEM_write_bio_X509(ca_out, cacert->certificate);
393416
CHECK_SSL(sret, PEM_write_bio_X509(CA));
417+
BIO_get_fp(ca_out, &fp);
418+
fchmod(fileno(fp), ca_mode);
394419
BIO_free(ca_out); ca_out = NULL;
395420

396421
if (options->ca_key_file) {
@@ -408,6 +433,8 @@ main(int argc, const char **argv)
408433
sret = PEM_write_bio_PrivateKey(ca_key_out, cakey->evp_pkey,
409434
NULL, NULL, 0, NULL, NULL);
410435
CHECK_SSL(sret, PEM_write_bio_PrivateKey(CA));
436+
BIO_get_fp(ca_key_out, &fp);
437+
fchmod(fileno(fp), ca_key_mode);
411438
BIO_free(ca_key_out); ca_key_out = NULL;
412439
}
413440

@@ -424,6 +451,8 @@ main(int argc, const char **argv)
424451

425452
sret = PEM_write_bio_X509(cert_out, svc_cert->certificate);
426453
CHECK_SSL(sret, PEM_write_bio_X509(svc));
454+
BIO_get_fp(cert_out, &fp);
455+
fchmod(fileno(fp), cert_mode);
427456
BIO_free(cert_out); cert_out = NULL;
428457

429458
if (options->verbosity >= SSCG_DEFAULT) {
@@ -440,9 +469,11 @@ main(int argc, const char **argv)
440469
sret = PEM_write_bio_PrivateKey(cert_key_out, svc_key->evp_pkey,
441470
NULL, NULL, 0, NULL, NULL);
442471
CHECK_SSL(sret, PEM_write_bio_PrivateKey(svc));
472+
BIO_get_fp(cert_key_out, &fp);
473+
fchmod(fileno(fp), cert_key_mode);
443474
BIO_free(cert_key_out); cert_key_out = NULL;
444475

445-
476+
ret = EOK;
446477
done:
447478
BIO_free(ca_key_out);
448479
BIO_free(ca_out);

0 commit comments

Comments
 (0)