From: Saverio Proto Date: Thu, 24 Apr 2014 14:02:50 +0000 (+0200) Subject: Use snprintf() instead of xasprintf() when generating filenames X-Git-Url: http://git.meshlink.io/?p=meshlink;a=commitdiff_plain;h=8fb3cf697d16e4870d7305866d03240acd49b386 Use snprintf() instead of xasprintf() when generating filenames --- diff --git a/src/conf.c b/src/conf.c index 08467e50..ed51fc75 100644 --- a/src/conf.c +++ b/src/conf.c @@ -291,46 +291,42 @@ bool read_config_file(splay_tree_t *config_tree, const char *fname) { } bool read_server_config(void) { - char *fname; + char filename[PATH_MAX]; bool x; - xasprintf(&fname, "%s" SLASH "meshlink.conf", mesh->confbase); + snprintf(filename, PATH_MAX,"%s" SLASH "meshlink.conf", mesh->confbase); errno = 0; - x = read_config_file(mesh->config, fname); + x = read_config_file(mesh->config, filename); if(!x && errno) - logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno)); - - free(fname); + logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", filename, strerror(errno)); return x; } bool read_host_config(splay_tree_t *config_tree, const char *name) { - char *fname; + char filename[PATH_MAX]; bool x; - xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); - x = read_config_file(config_tree, fname); - free(fname); + snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); + x = read_config_file(config_tree, filename); return x; } bool append_config_file(const char *name, const char *key, const char *value) { - char *fname; - xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); + char filename[PATH_MAX]; + snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); - FILE *fp = fopen(fname, "a"); + FILE *fp = fopen(filename, "a"); if(!fp) { - logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno)); + logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", filename, strerror(errno)); } else { fprintf(fp, "\n# The following line was automatically added by tinc\n%s = %s\n", key, value); fclose(fp); } - free(fname); return fp != NULL; } diff --git a/src/invitation.c b/src/invitation.c index d027e0d9..db9d9629 100644 --- a/src/invitation.c +++ b/src/invitation.c @@ -79,11 +79,11 @@ char *get_my_hostname() { char *port = NULL; char *hostport = NULL; char *name = get_my_name(false); - char *filename = NULL; + char filename[PATH_MAX]; // Use first Address statement in own host config file if(check_id(name)) { - xasprintf(&filename, "%s" SLASH "hosts" SLASH "%s", confbase, name); + snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", confbase, name); scan_for_hostname(filename, &hostname, &port); scan_for_hostname(tinc_conf, &hostname, &port); } @@ -200,7 +200,6 @@ done: free(hostname); free(port); - free(filename); return hostport; } @@ -236,14 +235,12 @@ int cmd_invite(int argc, char *argv[]) { return 1; // Ensure no host configuration file with that name exists - char *filename = NULL; - xasprintf(&filename, "%s" SLASH "hosts" SLASH "%s", confbase, argv[1]); + char filename [PATH_MAX]; + snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", confbase, argv[1]); if(!access(filename, F_OK)) { - free(filename); fprintf(stderr, "A host config file for %s already exists!\n", argv[1]); return 1; } - free(filename); // If a daemon is running, ensure no other nodes now about this name bool found = false; @@ -267,10 +264,9 @@ int cmd_invite(int argc, char *argv[]) { char hash[64]; - xasprintf(&filename, "%s" SLASH "invitations", confbase); + snprintf(filename,PATH_MAX, "%s" SLASH "invitations", confbase); if(mkdir(filename, 0700) && errno != EEXIST) { fprintf(stderr, "Could not create directory %s: %s\n", filename, strerror(errno)); - free(filename); return 1; } @@ -278,7 +274,6 @@ int cmd_invite(int argc, char *argv[]) { DIR *dir = opendir(filename); if(!dir) { fprintf(stderr, "Could not read directory %s: %s\n", filename, strerror(errno)); - free(filename); return 1; } @@ -290,9 +285,9 @@ int cmd_invite(int argc, char *argv[]) { while((ent = readdir(dir))) { if(strlen(ent->d_name) != 24) continue; - char *invname; + char invname[PATH_MAX]; struct stat st; - xasprintf(&invname, "%s" SLASH "%s", filename, ent->d_name); + snprintf(invname,PATH_MAX, "%s" SLASH "%s", filename, ent->d_name); if(!stat(invname, &st)) { if(deadline < st.st_mtime) count++; @@ -302,21 +297,18 @@ int cmd_invite(int argc, char *argv[]) { fprintf(stderr, "Could not stat %s: %s\n", invname, strerror(errno)); errno = 0; } - free(invname); } if(errno) { fprintf(stderr, "Error while reading directory %s: %s\n", filename, strerror(errno)); closedir(dir); - free(filename); return 1; } closedir(dir); - free(filename); ecdsa_t *key; - xasprintf(&filename, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", confbase); + snprintf(filename,PATH_MAX, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", confbase); // Remove the key if there are no outstanding invitations. if(!count) @@ -327,19 +319,16 @@ int cmd_invite(int argc, char *argv[]) { if(!f) { if(errno != ENOENT) { fprintf(stderr, "Could not read %s: %s\n", filename, strerror(errno)); - free(filename); return 1; } key = ecdsa_generate(); if(!key) { - free(filename); return 1; } f = fopen(filename, "w"); if(!f) { fprintf(stderr, "Could not write %s: %s\n", filename, strerror(errno)); - free(filename); return 1; } chmod(filename, 0600); @@ -355,7 +344,6 @@ int cmd_invite(int argc, char *argv[]) { fprintf(stderr, "Could not read private key from %s\n", filename); } - free(filename); if(!key) return 1; @@ -379,11 +367,10 @@ int cmd_invite(int argc, char *argv[]) { b64encode_urlsafe(cookie, cookie, 18); // Create a file containing the details of the invitation. - xasprintf(&filename, "%s" SLASH "invitations" SLASH "%s", confbase, cookiehash); + snprintf(filename,PATH_MAX, "%s" SLASH "invitations" SLASH "%s", confbase, cookiehash); int ifd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600); if(!ifd) { fprintf(stderr, "Could not create invitation file %s: %s\n", filename, strerror(errno)); - free(filename); return 1; } f = fdopen(ifd, "w"); @@ -418,11 +405,10 @@ int cmd_invite(int argc, char *argv[]) { fprintf(f, "#---------------------------------------------------------------#\n"); fprintf(f, "Name = %s\n", myname); - char *filename2; - xasprintf(&filename2, "%s" SLASH "hosts" SLASH "%s", confbase, myname); + char filename2[PATH_MAX]; + snsprintf(filename2,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", confbase, myname); fcopy(f, filename2); fclose(f); - free(filename2); // Create an URL from the local address, key hash and cookie char *url; diff --git a/src/net.c b/src/net.c index a2cc3ed7..dacf52c3 100644 --- a/src/net.c +++ b/src/net.c @@ -269,7 +269,7 @@ void handle_meta_connection_data(connection_t *c) { } int reload_configuration(void) { - char *fname = NULL; + char filename[PATH_MAX]; /* Reread our own configuration file */ @@ -281,9 +281,8 @@ int reload_configuration(void) { return EINVAL; } - xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->self->name); - read_config_file(mesh->config, fname); - free(fname); + snprintf(filename, PATH_MAX,"%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->self->name); + read_config_file(mesh->config, filename); /* Parse some options that are allowed to be changed while tinc is running */ @@ -296,13 +295,12 @@ int reload_configuration(void) { /* Close connections to hosts that have a changed or deleted host config file */ for list_each(connection_t, c, mesh->connections) { - xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, c->name); + snprintf(filename, PATH_MAX,"%s" SLASH "hosts" SLASH "%s", mesh->confbase, c->name); struct stat s; - if(stat(fname, &s) || s.st_mtime > mesh->last_config_check) { + if(stat(filename, &s) || s.st_mtime > mesh->last_config_check) { logger(DEBUG_CONNECTIONS, LOG_INFO, "Host config file of %s has been changed", c->name); terminate_connection(c, c->status.active); } - free(fname); } mesh->last_config_check = now.tv_sec; diff --git a/src/net_setup.c b/src/net_setup.c index e168f6ac..7fa7b98e 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -82,11 +82,10 @@ bool read_ecdsa_public_key(connection_t *c) { static bool read_ecdsa_private_key(void) { FILE *fp; - char *fname; + char filename[PATH_MAX]; - xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", mesh->confbase); - fp = fopen(fname, "r"); - free(fname); + snprintf(filename,PATH_MAX, "%s" SLASH "ecdsa_key.priv", mesh->confbase); + fp = fopen(filename, "r"); if(!fp) { logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file: %s", strerror(errno)); @@ -104,38 +103,36 @@ static bool read_ecdsa_private_key(void) { static bool read_invitation_key(void) { FILE *fp; - char *fname; + char filename[PATH_MAX]; if(mesh->invitation_key) { ecdsa_free(mesh->invitation_key); mesh->invitation_key = NULL; } - xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase); + snprintf(filename,PATH_MAX, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase); - fp = fopen(fname, "r"); + fp = fopen(filename, "r"); if(fp) { mesh->invitation_key = ecdsa_read_pem_private_key(fp); fclose(fp); if(!mesh->invitation_key) - logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno)); + logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", filename, strerror(errno)); } - free(fname); return mesh->invitation_key; } void load_all_nodes(void) { DIR *dir; struct dirent *ent; - char *dname; + char dname[PATH_MAX]; - xasprintf(&dname, "%s" SLASH "hosts", mesh->confbase); + snprintf(dname,PATH_MAX, "%s" SLASH "hosts", mesh->confbase); dir = opendir(dname); if(!dir) { logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno)); - free(dname); return; } diff --git a/src/tincctl.c b/src/tincctl.c index 394051e2..33b26c2b 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -360,7 +360,7 @@ static FILE *ask_and_open(const char *filename, const char *what, const char *mo static bool ecdsa_keygen(bool ask) { ecdsa_t *key; FILE *f; - char *pubname, *privname; + char pubname[PATH_MAX], privname[PATH_MAX]; fprintf(stderr, "Generating ECDSA keypair:\n"); @@ -370,9 +370,8 @@ static bool ecdsa_keygen(bool ask) { } else fprintf(stderr, "Done.\n"); - xasprintf(&privname, "%s" SLASH "ecdsa_key.priv", confbase); + snprintf(privname,PATH_MAX, "%s" SLASH "ecdsa_key.priv", confbase); f = ask_and_open(privname, "private ECDSA key", "a", ask, 0600); - free(privname); if(!f) return false; @@ -387,19 +386,17 @@ static bool ecdsa_keygen(bool ask) { fclose(f); if(name) - xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, name); + snprintf(pubname, PATH_MAX,"%s" SLASH "hosts" SLASH "%s", confbase, name); else - xasprintf(&pubname, "%s" SLASH "ecdsa_key.pub", confbase); + snprintf(pubname, PATH_MAX,"%s" SLASH "ecdsa_key.pub", confbase); f = ask_and_open(pubname, "public ECDSA key", "a", ask, 0666); - free(pubname); if(!f) return false; char *pubkey = ecdsa_get_base64_public_key(key); fprintf(f, "ECDSAPublicKey = %s\n", pubkey); - free(pubkey); fclose(f); ecdsa_free(key); @@ -414,7 +411,7 @@ static bool ecdsa_keygen(bool ask) { static bool rsa_keygen(int bits, bool ask) { rsa_t *key; FILE *f; - char *pubname, *privname; + char pubname[PATH_MAX], privname[PATH_MAX]; fprintf(stderr, "Generating %d bits keys:\n", bits); @@ -424,9 +421,8 @@ static bool rsa_keygen(int bits, bool ask) { } else fprintf(stderr, "Done.\n"); - xasprintf(&privname, "%s" SLASH "rsa_key.priv", confbase); + snprintf(privname,PATH_MAX, "%s" SLASH "rsa_key.priv", confbase); f = ask_and_open(privname, "private RSA key", "a", ask, 0600); - free(privname); if(!f) return false; @@ -441,12 +437,11 @@ static bool rsa_keygen(int bits, bool ask) { fclose(f); if(name) - xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, name); + snprintf(pubname,PATH_MAX,"%s" SLASH "hosts" SLASH "%s", confbase, name); else - xasprintf(&pubname, "%s" SLASH "rsa_key.pub", confbase); + snprintf(pubname,PATH_MAX,"%s" SLASH "rsa_key.pub", confbase); f = ask_and_open(pubname, "public RSA key", "a", ask, 0666); - free(pubname); if(!f) return false; @@ -1480,9 +1475,9 @@ static int cmd_config(int argc, char *argv[]) { } // Open the right configuration file. - char *filename; + char filename[PATH_MAX]; if(node) - xasprintf(&filename, "%s" SLASH "%s", hosts_dir, node); + snprintf(filename,PATH_MAX "%s" SLASH "%s", hosts_dir, node); else filename = tinc_conf; @@ -1492,11 +1487,11 @@ static int cmd_config(int argc, char *argv[]) { return 1; } - char *tmpfile = NULL; + char tmpfile[PATH_MAX]; FILE *tf = NULL; if(action >= -1) { - xasprintf(&tmpfile, "%s.config.tmp", filename); + snprintf(tmpfile,PATH_MAX, "%s.config.tmp", filename); tf = fopen(tmpfile, "w"); if(!tf) { fprintf(stderr, "Could not open temporary file %s: %s\n", tmpfile, strerror(errno)); @@ -1686,10 +1681,9 @@ int check_port(char *name) { for(int i = 0; i < 100; i++) { int port = 0x1000 + (rand() & 0x7fff); if(try_bind(port)) { - char *filename; - xasprintf(&filename, "%s" SLASH "hosts" SLASH "%s", confbase, name); + char filename[PATH_MAX]; + snprintf(filename,PATH_MAX "%s" SLASH "hosts" SLASH "%s", confbase, name); FILE *f = fopen(filename, "a"); - free(filename); if(!f) { fprintf(stderr, "Please change tinc's Port manually.\n"); return 0;