}
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;
}
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);
}
free(hostname);
free(port);
- free(filename);
return hostport;
}
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;
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;
}
DIR *dir = opendir(filename);
if(!dir) {
fprintf(stderr, "Could not read directory %s: %s\n", filename, strerror(errno));
- free(filename);
return 1;
}
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++;
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)
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);
fprintf(stderr, "Could not read private key from %s\n", filename);
}
- free(filename);
if(!key)
return 1;
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");
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;
}
int reload_configuration(void) {
- char *fname = NULL;
+ char filename[PATH_MAX];
/* Reread our own configuration file */
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 */
/* 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;
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));
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;
}
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");
} 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;
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);
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);
} 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;
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;
}
// 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;
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));
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;