From c16c561f259659ab0f04d97ea34b6726c00663ff Mon Sep 17 00:00:00 2001 From: cecilihf Date: Fri, 15 Jun 2007 12:26:56 +0000 Subject: [PATCH] A change in the default value of the name, and test for correct naming convention git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@1528 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/mgt_param.c | 22 ++++++++++++++++++---- varnish-cache/bin/varnishd/varnishd.c | 9 ++++++++- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/varnish-cache/bin/varnishd/mgt_param.c b/varnish-cache/bin/varnishd/mgt_param.c index 7b1198b9..bbb1a65c 100644 --- a/varnish-cache/bin/varnishd/mgt_param.c +++ b/varnish-cache/bin/varnishd/mgt_param.c @@ -506,18 +506,31 @@ tweak_name(struct cli *cli, struct parspec *par, const char* arg) char *path; char *old_path; int renaming; + char hostname[1024]; + char valid_chars[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789.-"; (void)par; if (arg != NULL) { + if (strlen(arg) == 0) { + gethostname(hostname, sizeof hostname); + arg = hostname; + } /* Check that the new name follows hostname convention */ - /* [a-zA-Z0-9.-] */ + if (strspn(arg, valid_chars) != strlen(arg)) { + cli_out(cli, "Error: %s is an invalid name\n", arg); + cli_result(cli, CLIS_PARAM); + return; + } asprintf(&old_path, "/tmp/%s", master.name); /* Create/rename the temporary varnish directory */ asprintf(&path, "/tmp/%s", arg); - renaming = (!stat(old_path, &st_old) && S_ISDIR(st_old.st_mode)); + renaming = (master.name && !stat(old_path, &st_old) && + S_ISDIR(st_old.st_mode)); if (stat(path, &st)) { if (renaming) { - if (renaming && rename(old_path, path)) { + if (rename(old_path, path)) { cli_out(cli, "Error: Directory %s could not be " "renamed to %s", @@ -548,6 +561,7 @@ tweak_name(struct cli *cli, struct parspec *par, const char* arg) exit (2); } /* Everything is fine, store the (new) name */ + free(master.name); master.name = strdup(arg); } else @@ -734,7 +748,7 @@ static struct parspec parspec[] = { "naming conventions. Makes it possible to run " "multiple varnishd instances on one server.\n" EXPERIMENTAL, - "hostname" }, + "", "hostname" }, { NULL, NULL, NULL } }; diff --git a/varnish-cache/bin/varnishd/varnishd.c b/varnish-cache/bin/varnishd/varnishd.c index f5cc3cf5..b34587e9 100644 --- a/varnish-cache/bin/varnishd/varnishd.c +++ b/varnish-cache/bin/varnishd/varnishd.c @@ -411,7 +411,10 @@ main(int argc, char *argv[]) struct cli cli[1]; struct pidfh *pfh = NULL; char buf[BUFSIZ]; - + char valid_chars[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789.-"; + setbuf(stdout, NULL); setbuf(stderr, NULL); @@ -451,6 +454,10 @@ main(int argc, char *argv[]) h_arg = optarg; break; case 'n': + if (strspn(optarg, valid_chars) != strlen(optarg)) { + fprintf(stderr, "%s is not a valid name\n", optarg); + exit(1); + } MCF_ParamSet(cli, "name", optarg); break; case 'P': -- 2.39.5