| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Copyright (C) 1999 - 2005, Digium, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  |  * \brief ASTdb Management | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * \author Mark Spencer <markster@digium.com>  | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \note DB3 is licensed under Sleepycat Public License and is thus incompatible | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * with GPL.  To avoid having to make another exception (and complicate  | 
					
						
							|  |  |  |  * licensing even further) we elect to use DB1 which is BSD licensed  | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <sys/time.h>
 | 
					
						
							|  |  |  | #include <signal.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <dirent.h>
 | 
					
						
							| 
									
										
										
										
											2005-04-22 13:11:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-21 06:02:45 +00:00
										 |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/file.h"
 | 
					
						
							|  |  |  | #include "asterisk/app.h"
 | 
					
						
							|  |  |  | #include "asterisk/dsp.h"
 | 
					
						
							|  |  |  | #include "asterisk/logger.h"
 | 
					
						
							|  |  |  | #include "asterisk/options.h"
 | 
					
						
							|  |  |  | #include "asterisk/astdb.h"
 | 
					
						
							|  |  |  | #include "asterisk/cli.h"
 | 
					
						
							|  |  |  | #include "asterisk/utils.h"
 | 
					
						
							|  |  |  | #include "asterisk/lock.h"
 | 
					
						
							|  |  |  | #include "asterisk/manager.h"
 | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | #include "db1-ast/include/db.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-01 21:53:30 +00:00
										 |  |  | #ifdef __CYGWIN__
 | 
					
						
							|  |  |  | #define dbopen __dbopen
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | static DB *astdb; | 
					
						
							| 
									
										
										
										
											2004-06-09 01:45:08 +00:00
										 |  |  | AST_MUTEX_DEFINE_STATIC(dblock); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int dbinit(void)  | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2006-02-09 16:59:50 +00:00
										 |  |  | 	if (!astdb && !(astdb = dbopen((char *)ast_config_AST_DB, O_CREAT | O_RDWR, 0664, DB_BTREE, NULL))) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Unable to open Asterisk database\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-02-09 16:59:50 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int keymatch(const char *key, const char *prefix) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2004-05-28 18:56:23 +00:00
										 |  |  | 	int preflen = strlen(prefix); | 
					
						
							|  |  |  | 	if (!preflen) | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		return 1; | 
					
						
							|  |  |  | 	if (!strcasecmp(key, prefix)) | 
					
						
							|  |  |  | 		return 1; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) { | 
					
						
							| 
									
										
										
										
											2004-05-28 18:56:23 +00:00
										 |  |  | 		if (key[preflen] == '/') | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 			return 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-12-22 01:07:02 +00:00
										 |  |  | static inline int subkeymatch(const char *key, const char *suffix) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int suffixlen = strlen(suffix); | 
					
						
							|  |  |  | 	if (suffixlen) { | 
					
						
							|  |  |  | 		const char *subkey = key + strlen(key) - suffixlen; | 
					
						
							|  |  |  | 		if (subkey < key) | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		if (!strcasecmp(subkey, suffix)) | 
					
						
							|  |  |  | 			return 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | int ast_db_deltree(const char *family, const char *keytree) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char prefix[256]; | 
					
						
							|  |  |  | 	DBT key, data; | 
					
						
							|  |  |  | 	char *keys; | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	int pass; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if (family) { | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		if (keytree) { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 			snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 			snprintf(prefix, sizeof(prefix), "/%s", family); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} else if (keytree) { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2004-07-09 10:08:09 +00:00
										 |  |  | 		prefix[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 	ast_mutex_lock(&dblock); | 
					
						
							| 
									
										
										
										
											2006-01-07 19:30:44 +00:00
										 |  |  | 	if (dbinit()) { | 
					
						
							|  |  |  | 		ast_mutex_unlock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2006-01-07 19:30:44 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	memset(&key, 0, sizeof(key)); | 
					
						
							|  |  |  | 	memset(&data, 0, sizeof(data)); | 
					
						
							|  |  |  | 	pass = 0; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		if (key.size) { | 
					
						
							|  |  |  | 			keys = key.data; | 
					
						
							|  |  |  | 			keys[key.size - 1] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 			keys = "<bad key>"; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		if (keymatch(keys, prefix)) { | 
					
						
							|  |  |  | 			astdb->del(astdb, &key, 0); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	astdb->sync(astdb, 0); | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 	ast_mutex_unlock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ast_db_put(const char *family, const char *keys, char *value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char fullkey[256]; | 
					
						
							|  |  |  | 	DBT key, data; | 
					
						
							| 
									
										
										
										
											2004-05-28 18:56:23 +00:00
										 |  |  | 	int res, fullkeylen; | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 	ast_mutex_lock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	if (dbinit()) { | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 		ast_mutex_unlock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-28 18:56:23 +00:00
										 |  |  | 	fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	memset(&key, 0, sizeof(key)); | 
					
						
							|  |  |  | 	memset(&data, 0, sizeof(data)); | 
					
						
							|  |  |  | 	key.data = fullkey; | 
					
						
							| 
									
										
										
										
											2004-05-28 18:56:23 +00:00
										 |  |  | 	key.size = fullkeylen + 1; | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	data.data = value; | 
					
						
							|  |  |  | 	data.size = strlen(value) + 1; | 
					
						
							|  |  |  | 	res = astdb->put(astdb, &key, &data, 0); | 
					
						
							|  |  |  | 	astdb->sync(astdb, 0); | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 	ast_mutex_unlock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	if (res) | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family); | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ast_db_get(const char *family, const char *keys, char *value, int valuelen) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	char fullkey[256] = ""; | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	DBT key, data; | 
					
						
							| 
									
										
										
										
											2004-05-28 18:56:23 +00:00
										 |  |  | 	int res, fullkeylen; | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 	ast_mutex_lock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	if (dbinit()) { | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 		ast_mutex_unlock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-28 18:56:23 +00:00
										 |  |  | 	fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	memset(&key, 0, sizeof(key)); | 
					
						
							|  |  |  | 	memset(&data, 0, sizeof(data)); | 
					
						
							| 
									
										
										
										
											2003-08-13 04:36:06 +00:00
										 |  |  | 	memset(value, 0, valuelen); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	key.data = fullkey; | 
					
						
							| 
									
										
										
										
											2004-05-28 18:56:23 +00:00
										 |  |  | 	key.size = fullkeylen + 1; | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	res = astdb->get(astdb, &key, &data, 0); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 	ast_mutex_unlock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Be sure to NULL terminate our data either way */ | 
					
						
							|  |  |  | 	if (res) { | 
					
						
							|  |  |  | 		ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2003-08-13 03:34:44 +00:00
										 |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		printf("Got value of size %d\n", data.size); | 
					
						
							| 
									
										
										
										
											2003-08-13 03:34:44 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		if (data.size) { | 
					
						
							|  |  |  | 			((char *)data.data)[data.size - 1] = '\0'; | 
					
						
							| 
									
										
										
										
											2003-08-13 04:36:06 +00:00
										 |  |  | 			/* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */ | 
					
						
							| 
									
										
										
										
											2006-10-25 14:32:08 +00:00
										 |  |  | 			ast_copy_string(value, data.data, (valuelen > data.size) ? data.size : valuelen); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ast_db_del(const char *family, const char *keys) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char fullkey[256]; | 
					
						
							|  |  |  | 	DBT key; | 
					
						
							| 
									
										
										
										
											2004-05-28 18:56:23 +00:00
										 |  |  | 	int res, fullkeylen; | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 	ast_mutex_lock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	if (dbinit()) { | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 		ast_mutex_unlock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2004-05-28 18:56:23 +00:00
										 |  |  | 	fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	memset(&key, 0, sizeof(key)); | 
					
						
							|  |  |  | 	key.data = fullkey; | 
					
						
							| 
									
										
										
										
											2004-05-28 18:56:23 +00:00
										 |  |  | 	key.size = fullkeylen + 1; | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	res = astdb->del(astdb, &key, 0); | 
					
						
							|  |  |  | 	astdb->sync(astdb, 0); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 	ast_mutex_unlock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (res)  | 
					
						
							|  |  |  | 		ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family); | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int database_put(int fd, int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	if (argc != 5) | 
					
						
							|  |  |  | 		return RESULT_SHOWUSAGE; | 
					
						
							|  |  |  | 	res = ast_db_put(argv[2], argv[3], argv[4]); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	if (res)  { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		ast_cli(fd, "Failed to update entry\n"); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		ast_cli(fd, "Updated database successfully\n"); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	return RESULT_SUCCESS; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int database_get(int fd, int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	char tmp[256]; | 
					
						
							|  |  |  | 	if (argc != 4) | 
					
						
							|  |  |  | 		return RESULT_SHOWUSAGE; | 
					
						
							|  |  |  | 	res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp)); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	if (res) { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		ast_cli(fd, "Database entry not found.\n"); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		ast_cli(fd, "Value: %s\n", tmp); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	return RESULT_SUCCESS; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int database_del(int fd, int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	if (argc != 4) | 
					
						
							|  |  |  | 		return RESULT_SHOWUSAGE; | 
					
						
							|  |  |  | 	res = ast_db_del(argv[2], argv[3]); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	if (res) { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		ast_cli(fd, "Database entry does not exist.\n"); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		ast_cli(fd, "Database entry removed.\n"); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	return RESULT_SUCCESS; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int database_deltree(int fd, int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	if ((argc < 3) || (argc > 4)) | 
					
						
							|  |  |  | 		return RESULT_SHOWUSAGE; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	if (argc == 4) { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		res = ast_db_deltree(argv[2], argv[3]); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		res = ast_db_deltree(argv[2], NULL); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (res) { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		ast_cli(fd, "Database entries do not exist.\n"); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		ast_cli(fd, "Database entries removed.\n"); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	return RESULT_SUCCESS; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int database_show(int fd, int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char prefix[256]; | 
					
						
							|  |  |  | 	DBT key, data; | 
					
						
							|  |  |  | 	char *keys, *values; | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	int pass; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (argc == 4) { | 
					
						
							|  |  |  | 		/* Family and key tree */ | 
					
						
							|  |  |  | 		snprintf(prefix, sizeof(prefix), "/%s/%s", argv[2], argv[3]); | 
					
						
							|  |  |  | 	} else if (argc == 3) { | 
					
						
							|  |  |  | 		/* Family only */ | 
					
						
							|  |  |  | 		snprintf(prefix, sizeof(prefix), "/%s", argv[2]); | 
					
						
							|  |  |  | 	} else if (argc == 2) { | 
					
						
							|  |  |  | 		/* Neither */ | 
					
						
							| 
									
										
										
										
											2004-07-09 10:08:09 +00:00
										 |  |  | 		prefix[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		return RESULT_SHOWUSAGE; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 	ast_mutex_lock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	if (dbinit()) { | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 		ast_mutex_unlock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		ast_cli(fd, "Database unavailable\n"); | 
					
						
							|  |  |  | 		return RESULT_SUCCESS;	 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	memset(&key, 0, sizeof(key)); | 
					
						
							|  |  |  | 	memset(&data, 0, sizeof(data)); | 
					
						
							|  |  |  | 	pass = 0; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		if (key.size) { | 
					
						
							|  |  |  | 			keys = key.data; | 
					
						
							|  |  |  | 			keys[key.size - 1] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 			keys = "<bad key>"; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		if (data.size) { | 
					
						
							|  |  |  | 			values = data.data; | 
					
						
							|  |  |  | 			values[data.size - 1]='\0'; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2003-04-13 04:17:45 +00:00
										 |  |  | 			values = "<bad value>"; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 		if (keymatch(keys, prefix)) { | 
					
						
							|  |  |  | 				ast_cli(fd, "%-50s: %-25s\n", keys, values); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 	ast_mutex_unlock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	return RESULT_SUCCESS;	 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-12-22 01:07:02 +00:00
										 |  |  | static int database_showkey(int fd, int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char suffix[256]; | 
					
						
							|  |  |  | 	DBT key, data; | 
					
						
							|  |  |  | 	char *keys, *values; | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	int pass; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (argc == 3) { | 
					
						
							|  |  |  | 		/* Key only */ | 
					
						
							|  |  |  | 		snprintf(suffix, sizeof(suffix), "/%s", argv[2]); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2004-12-22 01:07:02 +00:00
										 |  |  | 		return RESULT_SHOWUSAGE; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-12-22 01:07:02 +00:00
										 |  |  | 	ast_mutex_lock(&dblock); | 
					
						
							|  |  |  | 	if (dbinit()) { | 
					
						
							|  |  |  | 		ast_mutex_unlock(&dblock); | 
					
						
							|  |  |  | 		ast_cli(fd, "Database unavailable\n"); | 
					
						
							|  |  |  | 		return RESULT_SUCCESS;	 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	memset(&key, 0, sizeof(key)); | 
					
						
							|  |  |  | 	memset(&data, 0, sizeof(data)); | 
					
						
							|  |  |  | 	pass = 0; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) { | 
					
						
							| 
									
										
										
										
											2004-12-22 01:07:02 +00:00
										 |  |  | 		if (key.size) { | 
					
						
							|  |  |  | 			keys = key.data; | 
					
						
							|  |  |  | 			keys[key.size - 1] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2004-12-22 01:07:02 +00:00
										 |  |  | 			keys = "<bad key>"; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-12-22 01:07:02 +00:00
										 |  |  | 		if (data.size) { | 
					
						
							|  |  |  | 			values = data.data; | 
					
						
							|  |  |  | 			values[data.size - 1]='\0'; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2004-12-22 01:07:02 +00:00
										 |  |  | 			values = "<bad value>"; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-12-22 01:07:02 +00:00
										 |  |  | 		if (subkeymatch(keys, suffix)) { | 
					
						
							|  |  |  | 				ast_cli(fd, "%-50s: %-25s\n", keys, values); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ast_mutex_unlock(&dblock); | 
					
						
							|  |  |  | 	return RESULT_SUCCESS;	 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-13 04:17:45 +00:00
										 |  |  | struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char prefix[256]; | 
					
						
							|  |  |  | 	DBT key, data; | 
					
						
							|  |  |  | 	char *keys, *values; | 
					
						
							| 
									
										
										
										
											2006-02-09 16:59:50 +00:00
										 |  |  | 	int values_len; | 
					
						
							| 
									
										
										
										
											2003-04-13 04:17:45 +00:00
										 |  |  | 	int res; | 
					
						
							|  |  |  | 	int pass; | 
					
						
							|  |  |  | 	struct ast_db_entry *last = NULL; | 
					
						
							|  |  |  | 	struct ast_db_entry *cur, *ret=NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-26 18:54:24 +00:00
										 |  |  | 	if (!ast_strlen_zero(family)) { | 
					
						
							|  |  |  | 		if (!ast_strlen_zero(keytree)) { | 
					
						
							| 
									
										
										
										
											2003-04-13 04:17:45 +00:00
										 |  |  | 			/* Family and key tree */ | 
					
						
							|  |  |  | 			snprintf(prefix, sizeof(prefix), "/%s/%s", family, prefix); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2003-04-13 04:17:45 +00:00
										 |  |  | 			/* Family only */ | 
					
						
							|  |  |  | 			snprintf(prefix, sizeof(prefix), "/%s", family); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2004-07-09 10:08:09 +00:00
										 |  |  | 		prefix[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 	ast_mutex_lock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-04-13 04:17:45 +00:00
										 |  |  | 	if (dbinit()) { | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 		ast_mutex_unlock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-04-13 04:17:45 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Database unavailable\n"); | 
					
						
							|  |  |  | 		return NULL;	 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	memset(&key, 0, sizeof(key)); | 
					
						
							|  |  |  | 	memset(&data, 0, sizeof(data)); | 
					
						
							|  |  |  | 	pass = 0; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) { | 
					
						
							| 
									
										
										
										
											2003-04-13 04:17:45 +00:00
										 |  |  | 		if (key.size) { | 
					
						
							|  |  |  | 			keys = key.data; | 
					
						
							|  |  |  | 			keys[key.size - 1] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2003-04-13 04:17:45 +00:00
										 |  |  | 			keys = "<bad key>"; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2003-04-13 04:17:45 +00:00
										 |  |  | 		if (data.size) { | 
					
						
							|  |  |  | 			values = data.data; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 			values[data.size - 1] = '\0'; | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2003-04-13 04:17:45 +00:00
										 |  |  | 			values = "<bad value>"; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-02-09 16:59:50 +00:00
										 |  |  | 		values_len = strlen(values) + 1; | 
					
						
							|  |  |  | 		if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) { | 
					
						
							|  |  |  | 			cur->next = NULL; | 
					
						
							|  |  |  | 			cur->key = cur->data + values_len; | 
					
						
							|  |  |  | 			strcpy(cur->data, values); | 
					
						
							|  |  |  | 			strcpy(cur->key, keys); | 
					
						
							|  |  |  | 			if (last) { | 
					
						
							|  |  |  | 				last->next = cur; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				ret = cur; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2006-02-09 16:59:50 +00:00
										 |  |  | 			last = cur; | 
					
						
							| 
									
										
										
										
											2003-04-13 04:17:45 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-08-13 15:25:16 +00:00
										 |  |  | 	ast_mutex_unlock(&dblock); | 
					
						
							| 
									
										
										
										
											2003-04-13 04:17:45 +00:00
										 |  |  | 	return ret;	 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ast_db_freetree(struct ast_db_entry *dbe) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct ast_db_entry *last; | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	while (dbe) { | 
					
						
							| 
									
										
										
										
											2003-04-13 04:17:45 +00:00
										 |  |  | 		last = dbe; | 
					
						
							|  |  |  | 		dbe = dbe->next; | 
					
						
							|  |  |  | 		free(last); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | static char database_show_usage[] = | 
					
						
							|  |  |  | "Usage: database show [family [keytree]]\n" | 
					
						
							|  |  |  | "       Shows Asterisk database contents, optionally restricted\n" | 
					
						
							|  |  |  | "to a given family, or family and keytree.\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-12-22 01:07:02 +00:00
										 |  |  | static char database_showkey_usage[] = | 
					
						
							|  |  |  | "Usage: database showkey <keytree>\n" | 
					
						
							|  |  |  | "       Shows Asterisk database contents, restricted to a given key.\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | static char database_put_usage[] = | 
					
						
							|  |  |  | "Usage: database put <family> <key> <value>\n" | 
					
						
							|  |  |  | "       Adds or updates an entry in the Asterisk database for\n" | 
					
						
							|  |  |  | "a given family, key, and value.\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char database_get_usage[] = | 
					
						
							|  |  |  | "Usage: database get <family> <key>\n" | 
					
						
							|  |  |  | "       Retrieves an entry in the Asterisk database for a given\n" | 
					
						
							|  |  |  | "family and key.\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char database_del_usage[] = | 
					
						
							|  |  |  | "Usage: database del <family> <key>\n" | 
					
						
							|  |  |  | "       Deletes an entry in the Asterisk database for a given\n" | 
					
						
							|  |  |  | "family and key.\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char database_deltree_usage[] = | 
					
						
							|  |  |  | "Usage: database deltree <family> [keytree]\n" | 
					
						
							|  |  |  | "       Deletes a family or specific keytree within a family\n" | 
					
						
							|  |  |  | "in the Asterisk database.\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | struct ast_cli_entry cli_database[] = { | 
					
						
							|  |  |  | 	{ { "database", "show", NULL }, | 
					
						
							|  |  |  | 	database_show, "Shows database contents", | 
					
						
							|  |  |  | 	database_show_usage }, | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | 	{ { "database", "showkey", NULL }, | 
					
						
							|  |  |  | 	database_showkey, "Shows database contents", | 
					
						
							|  |  |  | 	database_showkey_usage }, | 
					
						
							| 
									
										
										
										
											2004-12-22 01:07:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | 	{ { "database", "get", NULL }, | 
					
						
							|  |  |  | 	database_get, "Gets database value", | 
					
						
							|  |  |  | 	database_get_usage }, | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | 	{ { "database", "put", NULL }, | 
					
						
							|  |  |  | 	database_put, "Adds/updates database value", | 
					
						
							|  |  |  | 	database_put_usage }, | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | 	{ { "database", "del", NULL }, | 
					
						
							|  |  |  | 	database_del, "Removes database key/value", | 
					
						
							|  |  |  | 	database_del_usage }, | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | 	{ { "database", "deltree", NULL }, | 
					
						
							|  |  |  | 	database_deltree, "Removes database keytree/values", | 
					
						
							|  |  |  | 	database_deltree_usage }, | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-05 22:16:33 +00:00
										 |  |  | static int manager_dbput(struct mansession *s, const struct message *m) | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-01-05 22:16:33 +00:00
										 |  |  | 	const char *family = astman_get_header(m, "Family"); | 
					
						
							|  |  |  | 	const char *key = astman_get_header(m, "Key"); | 
					
						
							|  |  |  | 	const char *val = astman_get_header(m, "Val"); | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 	int res; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-03-19 15:53:40 +00:00
										 |  |  | 	if (ast_strlen_zero(family)) { | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 		astman_send_error(s, m, "No family specified"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-03-19 15:53:40 +00:00
										 |  |  | 	if (ast_strlen_zero(key)) { | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 		astman_send_error(s, m, "No key specified"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-03-19 15:53:40 +00:00
										 |  |  | 	if (ast_strlen_zero(val)) { | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 		astman_send_error(s, m, "No val specified"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-05 22:16:33 +00:00
										 |  |  | 	res = ast_db_put(family, key, (char *) val); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	if (res) { | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 		astman_send_error(s, m, "Failed to update entry"); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 		astman_send_ack(s, m, "Updated database successfully"); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-05 22:16:33 +00:00
										 |  |  | static int manager_dbget(struct mansession *s, const struct message *m) | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-01-05 22:16:33 +00:00
										 |  |  | 	const char *id = astman_get_header(m,"ActionID"); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:38:20 +00:00
										 |  |  | 	char idText[256] = ""; | 
					
						
							| 
									
										
										
										
											2007-01-05 22:16:33 +00:00
										 |  |  | 	const char *family = astman_get_header(m, "Family"); | 
					
						
							|  |  |  | 	const char *key = astman_get_header(m, "Key"); | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 	char tmp[256]; | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-03-19 15:53:40 +00:00
										 |  |  | 	if (ast_strlen_zero(family)) { | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 		astman_send_error(s, m, "No family specified."); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-03-19 15:53:40 +00:00
										 |  |  | 	if (ast_strlen_zero(key)) { | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 		astman_send_error(s, m, "No key specified."); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-26 18:54:24 +00:00
										 |  |  | 	if (!ast_strlen_zero(id)) | 
					
						
							| 
									
										
										
										
											2005-07-20 00:38:20 +00:00
										 |  |  | 		snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 	res = ast_db_get(family, key, tmp, sizeof(tmp)); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	if (res) { | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 		astman_send_error(s, m, "Database entry not found"); | 
					
						
							| 
									
										
										
										
											2005-07-20 00:09:39 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 		astman_send_ack(s, m, "Result will follow"); | 
					
						
							| 
									
										
										
										
											2006-03-25 23:50:09 +00:00
										 |  |  | 		astman_append(s, "Event: DBGetResponse\r\n" | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 				"Family: %s\r\n" | 
					
						
							|  |  |  | 				"Key: %s\r\n" | 
					
						
							| 
									
										
										
										
											2005-07-20 00:38:20 +00:00
										 |  |  | 				"Val: %s\r\n" | 
					
						
							|  |  |  | 				"%s" | 
					
						
							|  |  |  | 				"\r\n", | 
					
						
							|  |  |  | 				family, key, tmp, idText); | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | int astdb_init(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	dbinit(); | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | 	ast_cli_register_multiple(cli_database, sizeof(cli_database) / sizeof(struct ast_cli_entry)); | 
					
						
							| 
									
										
										
										
											2005-01-29 23:33:14 +00:00
										 |  |  | 	ast_manager_register("DBGet", EVENT_FLAG_SYSTEM, manager_dbget, "Get DB Entry"); | 
					
						
							|  |  |  | 	ast_manager_register("DBPut", EVENT_FLAG_SYSTEM, manager_dbput, "Put DB Entry"); | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } |