diff options
Diffstat (limited to 'util/libreboot-utils/mkhtemp.c')
| -rw-r--r-- | util/libreboot-utils/mkhtemp.c | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/util/libreboot-utils/mkhtemp.c b/util/libreboot-utils/mkhtemp.c index 3fa4819f..4408f763 100644 --- a/util/libreboot-utils/mkhtemp.c +++ b/util/libreboot-utils/mkhtemp.c @@ -67,27 +67,26 @@ int main(int argc, char *argv[]) { - const char usage_str[] = "usage: %s [-d] [-p dir] [template]"; - - char *tmpdir = NULL; - char *template = NULL; - - char *s = NULL; - int fd = -1; - char c; - int type = MKHTEMP_FILE; - size_t len; - - char *rp; - #if defined (PATH_LEN) && \ (PATH_LEN) >= 256 size_t maxlen = PATH_LEN; #else size_t maxlen = 4096; #endif + size_t len; + size_t tlen; + size_t xc = 0; + char *tmpdir = NULL; + char *template = NULL; + char *p; + char *s = NULL; + char *rp; char resolved[maxlen]; + char c; + + int fd = -1; + int type = MKHTEMP_FILE; if (lbgetprogname(argv[0]) == NULL) err_no_cleanup(errno, "could not set progname"); @@ -123,6 +122,19 @@ main(int argc, char *argv[]) err_no_cleanup(EINVAL, "usage: mkhtemp [-d] [-p dir] [template]\n"); + /* custom template e.g. foo.XXXXXXXXXXXXXXXXXXXXX */ + if (template != NULL) { + if (slen(template, maxlen, &tlen) < 0) + err_no_cleanup(EINVAL, + "invalid template"); + + for (p = template + tlen; + p > template && *--p == 'X'; xc++); + + if (xc < 6) + err_no_cleanup(EINVAL, + "template must end in at least 6 X"); + } /* user supplied -p PATH - WARNING: * this permits symlinks, but only here, @@ -140,7 +152,8 @@ main(int argc, char *argv[]) tmpdir = resolved; } - if (new_tmp_common(&fd, &s, type, tmpdir) < 0) + if (new_tmp_common(&fd, &s, type, + tmpdir, template) < 0) err_no_cleanup(errno, "%s", s); #if defined(__OpenBSD__) && defined(OpenBSD) |
