Skip to content

Commit 8b4dcea

Browse files
committed
Do not overwrite destination files
... without permission. Add `--force` option. Resolves: #7 Signed-off-by: Stephen Gallagher <[email protected]>
1 parent 12bc1f4 commit 8b4dcea

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

include/sscg.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ struct sscg_options
146146
char *ca_key_file;
147147
char *cert_file;
148148
char *cert_key_file;
149+
150+
/* Overwrite the output files */
151+
bool overwrite;
149152
};
150153

151154
#endif /* _SSCG_H */

src/sscg.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ main (int argc, const char **argv)
139139
int cert_mode = 0644;
140140
int cert_key_mode = 0600;
141141

142+
char *create_mode = NULL;
143+
142144
struct sscg_x509_cert *cacert;
143145
struct sscg_evp_pkey *cakey;
144146
struct sscg_x509_cert *svc_cert;
@@ -201,6 +203,13 @@ main (int argc, const char **argv)
201203
0,
202204
_ ("Display the version number and exit."),
203205
NULL },
206+
{ "force",
207+
'f',
208+
POPT_ARG_NONE,
209+
&options->overwrite,
210+
0,
211+
_ ("Overwrite any pre-existing files in the requested locations"),
212+
NULL },
204213
{ "lifetime",
205214
'\0',
206215
POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT,
@@ -610,14 +619,25 @@ main (int argc, const char **argv)
610619

611620
/* ==== Output the final files ==== */
612621

622+
/* Set the file-creation mode */
623+
if (options->overwrite)
624+
{
625+
create_mode = talloc_strdup (main_ctx, "w");
626+
}
627+
else
628+
{
629+
create_mode = talloc_strdup (main_ctx, "wx");
630+
}
631+
CHECK_MEM (create_mode);
632+
613633
/* Create certificate private key file */
614634
if (options->verbosity >= SSCG_DEFAULT)
615635
{
616636
fprintf (
617637
stdout, "Writing svc private key to %s \n", options->cert_key_file);
618638
}
619639

620-
cert_key_out = BIO_new_file (options->cert_key_file, "w");
640+
cert_key_out = BIO_new_file (options->cert_key_file, create_mode);
621641
CHECK_BIO (cert_key_out, options->cert_key_file);
622642

623643
sret = PEM_write_bio_PrivateKey (
@@ -650,7 +670,7 @@ main (int argc, const char **argv)
650670
}
651671
else
652672
{
653-
cert_out = BIO_new_file (options->cert_file, "w");
673+
cert_out = BIO_new_file (options->cert_file, create_mode);
654674
}
655675
CHECK_BIO (cert_out, options->cert_file);
656676

@@ -696,7 +716,7 @@ main (int argc, const char **argv)
696716
}
697717
else
698718
{
699-
ca_key_out = BIO_new_file (options->ca_key_file, "w");
719+
ca_key_out = BIO_new_file (options->ca_key_file, create_mode);
700720
}
701721
CHECK_BIO (ca_key_out, options->ca_key_file);
702722

@@ -728,7 +748,7 @@ main (int argc, const char **argv)
728748
}
729749
else
730750
{
731-
ca_out = BIO_new_file (options->ca_file, "w");
751+
ca_out = BIO_new_file (options->ca_file, create_mode);
732752
}
733753
CHECK_BIO (ca_out, options->ca_file);
734754

0 commit comments

Comments
 (0)