sip-router

SIP Router Project

Tasklist

FS#89 - tls module 'config' param can point to directory

Attached to Project: sip-router
Opened by Juha Heinanen (jh) - Saturday, 02 October 2010, 16:50 GMT
Last edited by Juha Heinanen (jh) - Thursday, 11 July 2013, 19:50 GMT
Task Type Improvement
Category Module → tls
Status Closed
Assigned To Daniel-Constantin Mierla (miconda)
Operating System All
Severity Medium
Priority Normal
Reported Version Development
Due in Version Undecided
Due Date Undecided
Percent Complete 100%
Votes 0
Private No

Details

i modified tls/tls_config.c tls_load_config() function so that it checks if file pointed to by ‘config’ module param is a directory. if it is, tls_load_config() copies all regular files of that directory to a tmp file and then passes that tmp file to cfg_parser. that way config parser can stay as is.

reason for this improvement is that it is easier from management point of view to add/remove files when peers come and go rather than edit a monolithic single config file. modified tls_load_config() is below. feel free to implement same functionality in some other way. i don’t care how it is done as long as the feature is available. if adopted, i’ll update README accordingly.

– juha


/*
* Create configuration structures from configuration file
*/
tls_domains_cfg_t* tls_load_config(str* filename)
{

cfg_parser_t* parser;
str empty;
struct stat file_status;
char tmp_name[13] = "configXXXXXX";
str filename_str;
DIR *dir;
struct dirent *ent;
int out_fd, in_fd, filename_is_directory;
char *file_path, ch;
parser = NULL;
memset(&file_status, 0, sizeof(struct stat));
dir = (DIR *)NULL;
in_fd = out_fd = filename_is_directory = 0;
file_path = (char *)0;
if ((cfg = tls_new_cfg()) == NULL) goto error;
if (stat(filename->s, &file_status) != 0) {
    LOG(L_ERR, "cannot stat config file %s\n", filename->s);
    goto error;
}
if (S_ISDIR(file_status.st_mode)) {
    filename_is_directory = 1;
    dir = opendir(filename->s);
    if (dir == NULL) {
	LOG(L_ERR, "cannot open directory file %s\n", filename->s);
	goto error;
    }
    out_fd = mkstemp(&(tmp_name[0]));
    if (out_fd == -1) {
	LOG(L_ERR, "cannot make tmp file %s\n", &(tmp_name[0]));
	goto error;
    }
    while ((ent = readdir(dir)) != NULL) {
	file_path = pkg_malloc(filename->len + 1 + 256);
	memcpy(file_path, filename->s, filename->len);
	file_path[filename->len] = '/';
	strcpy(file_path + filename->len + 1, ent->d_name);
	if (stat(file_path, &file_status) != 0) {
	    LOG(L_ERR, "cannot get status of config file %s\n",
		file_path);
	    goto error;
	}
	if (S_ISREG(file_status.st_mode)) {
	    in_fd = open(file_path, O_RDONLY);
	    if (in_fd == -1) {
		LOG(L_ERR, "cannot open config file %s\n",
		    file_path);
		goto error;
	    }
	    pkg_free(file_path);
	    while (read(in_fd, &ch, 1)) {
		write(out_fd, &ch, 1);
	    }
	    close(in_fd);
	    in_fd = 0;
	    ch = '\n';
	    write(out_fd, &ch, 1);
	}
    }
    closedir(dir);
    close(out_fd);
    dir = (DIR *)NULL;
    out_fd = 0;
}
empty.s = 0;
empty.len = 0;
if (filename_is_directory) {
    filename_str.s = &(tmp_name[0]);
    filename_str.len = strlen(&(tmp_name[0]));
    if ((parser = cfg_parser_init(&empty, &filename_str)) == NULL) {
	ERR("tls: Error while initializing configuration file parser.\n");
	unlink(&(tmp_name[0]));
	goto error;
    }
    unlink(&(tmp_name[0]));
} else {
	if ((parser = cfg_parser_init(&empty, filename)) == NULL) {
	    ERR("tls: Error while initializing configuration file parser.\n");
	    goto error;
	}		
}
cfg_section_parser(parser, parse_domain, NULL);
if (sr_cfg_parse(parser)) goto error;
cfg_parser_close(parser);
return cfg;

error:

if (dir) closedir(dir);
if (out_fd > 0) {
    close(out_fd);
    unlink(&(tmp_name[0]));
}
if (file_path) pkg_free(file_path);
if (parser) cfg_parser_close(parser);
if (cfg) tls_free_cfg(cfg);
return 0;

}

This task depends upon

Closed by  Juha Heinanen (jh)
Thursday, 11 July 2013, 19:50 GMT
Reason for closing:  Implemented
Comment by Daniel-Constantin Mierla (miconda) - Monday, 12 March 2012, 23:39 GMT

Looks interesting, you can go ahead and commit if nobody else has other suggestions for such feature.

Comment by Daniel-Constantin Mierla (miconda) - Saturday, 15 June 2013, 20:40 GMT

Was this committed?

Loading...