res_odbc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Copyright (C) 1999, Mark Spencer
  5. *
  6. * Mark Spencer <markster@linux-support.net>
  7. *
  8. * res_odbc.c <ODBC resource manager>
  9. * Copyright (C) 2004 Anthony Minessale II <anthmct@yahoo.com>
  10. */
  11. #include <asterisk/file.h>
  12. #include <asterisk/logger.h>
  13. #include <asterisk/channel.h>
  14. #include <asterisk/config.h>
  15. #include <asterisk/options.h>
  16. #include <asterisk/pbx.h>
  17. #include <asterisk/module.h>
  18. #include <asterisk/cli.h>
  19. #include <asterisk/lock.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <asterisk/res_odbc.h>
  24. #define MAX_ODBC_HANDLES 25
  25. struct odbc_list
  26. {
  27. char name[80];
  28. odbc_obj *obj;
  29. int used;
  30. };
  31. static struct odbc_list ODBC_REGISTRY[MAX_ODBC_HANDLES];
  32. static void odbc_destroy(void)
  33. {
  34. int x = 0;
  35. for (x = 0; x < MAX_ODBC_HANDLES; x++) {
  36. if (ODBC_REGISTRY[x].obj) {
  37. destroy_obdc_obj(&ODBC_REGISTRY[x].obj);
  38. ODBC_REGISTRY[x].obj = NULL;
  39. }
  40. }
  41. }
  42. static odbc_obj *odbc_read(struct odbc_list *registry, char *name)
  43. {
  44. int x = 0;
  45. for (x = 0; x < MAX_ODBC_HANDLES; x++) {
  46. if (registry[x].used && !strcmp(registry[x].name, name)) {
  47. return registry[x].obj;
  48. }
  49. }
  50. return NULL;
  51. }
  52. static int odbc_write(struct odbc_list *registry, char *name, odbc_obj * obj)
  53. {
  54. int x = 0;
  55. for (x = 0; x < MAX_ODBC_HANDLES; x++) {
  56. if (!registry[x].used) {
  57. strncpy(registry[x].name, name, sizeof(registry[x].name) - 1);
  58. registry[x].obj = obj;
  59. registry[x].used = 1;
  60. return 1;
  61. }
  62. }
  63. return 0;
  64. }
  65. static void odbc_init(void)
  66. {
  67. int x = 0;
  68. for (x = 0; x < MAX_ODBC_HANDLES; x++) {
  69. memset(&ODBC_REGISTRY[x], 0, sizeof(struct odbc_list));
  70. }
  71. }
  72. static char *tdesc = "ODBC Resource";
  73. /* internal stuff */
  74. static int load_odbc_config(void)
  75. {
  76. static char *cfg = "res_odbc.conf";
  77. struct ast_config *config;
  78. struct ast_variable *v;
  79. char *cat, *dsn, *username, *password;
  80. int enabled;
  81. int connect = 0;
  82. char *env_var;
  83. odbc_obj *obj;
  84. config = ast_load(cfg);
  85. if (config) {
  86. for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
  87. if (!strcmp(cat, "ENV")) {
  88. for (v = ast_variable_browse(config, cat); v; v = v->next) {
  89. env_var = malloc(strlen(v->name) + strlen(v->value) + 2);
  90. if (env_var) {
  91. sprintf(env_var, "%s=%s", v->name, v->value);
  92. ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
  93. putenv(env_var);
  94. free(env_var);
  95. }
  96. }
  97. cat = ast_category_browse(config, cat);
  98. }
  99. dsn = username = password = NULL;
  100. enabled = 1;
  101. connect = 0;
  102. for (v = ast_variable_browse(config, cat); v; v = v->next) {
  103. if (!strcmp(v->name, "enabled"))
  104. enabled = ast_true(v->value);
  105. if (!strcmp(v->name, "pre-connect"))
  106. connect = ast_true(v->value);
  107. if (!strcmp(v->name, "dsn"))
  108. dsn = v->value;
  109. if (!strcmp(v->name, "username"))
  110. username = v->value;
  111. if (!strcmp(v->name, "password"))
  112. password = v->value;
  113. }
  114. if (enabled && dsn && username && password) {
  115. obj = new_odbc_obj(cat, dsn, username, password);
  116. if (obj) {
  117. register_odbc_obj(cat, obj);
  118. ast_log(LOG_NOTICE, "registered database handle '%s' dsn->[%s]\n", cat, obj->dsn);
  119. if (connect) {
  120. odbc_obj_connect(obj);
  121. }
  122. } else {
  123. ast_log(LOG_WARNING, "Addition of obj %s failed.\n", cat);
  124. }
  125. }
  126. }
  127. ast_destroy(config);
  128. }
  129. return 0;
  130. }
  131. int odbc_dump_fd(int fd, odbc_obj * obj)
  132. {
  133. ast_cli(fd, "Name: %s\nDSN: %s\nConnected: %s\n", obj->name, obj->dsn, obj->up ? "yes" : "no");
  134. return 0;
  135. }
  136. static int odbc_connect_usage(int fd)
  137. {
  138. ast_cli(fd, "usage odbc connect <DSN>\n");
  139. return 0;
  140. }
  141. static int odbc_disconnect_usage(int fd)
  142. {
  143. ast_cli(fd, "usage odbc disconnect <DSN>\n");
  144. return 0;
  145. }
  146. static int odbc_show_command(int fd, int argc, char **argv)
  147. {
  148. odbc_obj *obj;
  149. int x = 0;
  150. if (!strcmp(argv[1], "show")) {
  151. if (!argv[2] || (argv[2] && !strcmp(argv[2], "all"))) {
  152. for (x = 0; x < MAX_ODBC_HANDLES; x++) {
  153. if (!ODBC_REGISTRY[x].used)
  154. break;
  155. if (ODBC_REGISTRY[x].obj)
  156. odbc_dump_fd(fd, ODBC_REGISTRY[x].obj);
  157. }
  158. } else {
  159. obj = odbc_read(ODBC_REGISTRY, argv[2]);
  160. if (obj)
  161. odbc_dump_fd(fd, obj);
  162. }
  163. }
  164. return 0;
  165. }
  166. static int odbc_disconnect_command(int fd, int argc, char **argv){
  167. odbc_obj *obj;
  168. if (!strcmp(argv[1], "disconnect")) {
  169. if (!argv[2])
  170. return odbc_disconnect_usage(fd);
  171. obj = odbc_read(ODBC_REGISTRY, argv[2]);
  172. if (obj) {
  173. odbc_obj_disconnect(obj);
  174. }
  175. }
  176. return 0;
  177. }
  178. static int odbc_connect_command(int fd, int argc, char **argv){
  179. odbc_obj *obj;
  180. if (!argv[1])
  181. return odbc_connect_usage(fd);
  182. if (!strcmp(argv[1], "connect") || !strcmp(argv[1], "disconnect")) {
  183. if (!argv[2])
  184. return odbc_connect_usage(fd);
  185. obj = odbc_read(ODBC_REGISTRY, argv[2]);
  186. if (obj) {
  187. odbc_obj_connect(obj);
  188. }
  189. }
  190. return 0;
  191. }
  192. static char connect_usage[] =
  193. "Usage: odbc connect <DSN>\n"
  194. " Connect to ODBC DSN\n";
  195. static char disconnect_usage[] =
  196. "Usage: odbc connect <DSN>\n"
  197. " Disconnect from ODBC DSN\n";
  198. static char show_usage[] =
  199. "Usage: odbc show {DSN}\n"
  200. " Show ODBC {DSN}\n"
  201. " Specifying DSN will show that DSN else, all DSNs are shown\n";
  202. static struct ast_cli_entry odbc_connect_struct =
  203. { { "odbc", "connect", NULL }, odbc_connect_command, "Connect to ODBC DSN", connect_usage };
  204. static struct ast_cli_entry odbc_disconnect_struct =
  205. { { "odbc", "disconnect", NULL }, odbc_disconnect_command, "Disconnect from ODBC DSN", disconnect_usage };
  206. static struct ast_cli_entry odbc_show_struct =
  207. { { "odbc", "show", NULL }, odbc_show_command, "Show ODBC DSN(s)", show_usage };
  208. /* api calls */
  209. int register_odbc_obj(char *name, odbc_obj * obj)
  210. {
  211. if (obj != NULL)
  212. return odbc_write(ODBC_REGISTRY, name, obj);
  213. return 0;
  214. }
  215. odbc_obj *fetch_odbc_obj(char *name)
  216. {
  217. return (odbc_obj *) odbc_read(ODBC_REGISTRY, name);
  218. }
  219. odbc_obj *new_odbc_obj(char *name, char *dsn, char *username, char *password)
  220. {
  221. static odbc_obj *new;
  222. new = malloc(sizeof(odbc_obj));
  223. if (!new)
  224. return NULL;
  225. memset(new, 0, sizeof(odbc_obj));
  226. new->env = SQL_NULL_HANDLE;
  227. new->name = malloc(strlen(name) + 1);
  228. if (new->name == NULL)
  229. return NULL;
  230. new->dsn = malloc(strlen(dsn) + 1);
  231. if (new->dsn == NULL)
  232. return NULL;
  233. new->username = malloc(strlen(username) + 1);
  234. if (new->username == NULL)
  235. return NULL;
  236. new->password = malloc(strlen(password) + 1);
  237. if (new->password == NULL)
  238. return NULL;
  239. strcpy(new->name, name);
  240. strcpy(new->dsn, dsn);
  241. strcpy(new->username, username);
  242. strcpy(new->password, password);
  243. new->up = 0;
  244. ast_mutex_init(&new->lock);
  245. return new;
  246. }
  247. void destroy_obdc_obj(odbc_obj ** obj)
  248. {
  249. odbc_obj_disconnect(*obj);
  250. ast_mutex_lock(&(*obj)->lock);
  251. SQLFreeHandle(SQL_HANDLE_STMT, (*obj)->stmt);
  252. SQLFreeHandle(SQL_HANDLE_DBC, (*obj)->con);
  253. SQLFreeHandle(SQL_HANDLE_ENV, (*obj)->env);
  254. free((*obj)->name);
  255. free((*obj)->dsn);
  256. free((*obj)->username);
  257. free((*obj)->password);
  258. ast_mutex_unlock(&(*obj)->lock);
  259. free(*obj);
  260. }
  261. odbc_status odbc_obj_disconnect(odbc_obj * obj)
  262. {
  263. int res;
  264. ast_mutex_lock(&obj->lock);
  265. if (obj->up) {
  266. res = SQLDisconnect(obj->con);
  267. } else {
  268. res = -1;
  269. }
  270. if (res == ODBC_SUCCESS) {
  271. ast_log(LOG_WARNING, "res_odbc: disconnected %d from %s [%s]\n", res, obj->name, obj->dsn);
  272. obj->up = 0;
  273. } else {
  274. ast_log(LOG_WARNING, "res_odbc: %s [%s] already disconnected\n",
  275. obj->name, obj->dsn);
  276. }
  277. ast_mutex_unlock(&obj->lock);
  278. return ODBC_SUCCESS;
  279. }
  280. odbc_status odbc_obj_connect(odbc_obj * obj)
  281. {
  282. int res;
  283. long int err;
  284. short int mlen;
  285. char msg[200], stat[10];
  286. ast_mutex_lock(&obj->lock);
  287. if (obj->env == SQL_NULL_HANDLE) {
  288. res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &obj->env);
  289. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  290. if (option_verbose > 3)
  291. ast_log(LOG_WARNING, "res_odbc: Error AllocHandle\n");
  292. ast_mutex_unlock(&obj->lock);
  293. return ODBC_FAIL;
  294. }
  295. res = SQLSetEnvAttr(obj->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
  296. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  297. if (option_verbose > 3)
  298. ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
  299. SQLFreeHandle(SQL_HANDLE_ENV, obj->env);
  300. ast_mutex_unlock(&obj->lock);
  301. return ODBC_FAIL;
  302. }
  303. res = SQLAllocHandle(SQL_HANDLE_DBC, obj->env, &obj->con);
  304. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  305. if (option_verbose > 3)
  306. ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
  307. SQLFreeHandle(SQL_HANDLE_ENV, obj->env);
  308. ast_mutex_unlock(&obj->lock);
  309. return ODBC_FAIL;
  310. }
  311. SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
  312. }
  313. res = SQLConnect(obj->con,
  314. (SQLCHAR *) obj->dsn, SQL_NTS,
  315. (SQLCHAR *) obj->username, SQL_NTS,
  316. (SQLCHAR *) obj->password, SQL_NTS);
  317. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  318. SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, stat, &err, msg, 100, &mlen);
  319. SQLFreeHandle(SQL_HANDLE_ENV, obj->env);
  320. if (option_verbose > 3)
  321. ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%ld %s\n", res, err, msg);
  322. return ODBC_FAIL;
  323. } else {
  324. if (option_verbose > 3)
  325. ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->name, obj->dsn);
  326. obj->up = 1;
  327. }
  328. ast_mutex_unlock(&obj->lock);
  329. return ODBC_SUCCESS;
  330. }
  331. STANDARD_LOCAL_USER;
  332. LOCAL_USER_DECL;
  333. int unload_module(void)
  334. {
  335. STANDARD_HANGUP_LOCALUSERS;
  336. odbc_destroy();
  337. ast_cli_unregister(&odbc_disconnect_struct);
  338. ast_cli_unregister(&odbc_connect_struct);
  339. ast_cli_unregister(&odbc_show_struct);
  340. ast_log(LOG_NOTICE, "res_odbc unloaded.\n");
  341. return 0;
  342. }
  343. int load_module(void)
  344. {
  345. odbc_init();
  346. load_odbc_config();
  347. ast_cli_register(&odbc_disconnect_struct);
  348. ast_cli_register(&odbc_connect_struct);
  349. ast_cli_register(&odbc_show_struct);
  350. ast_log(LOG_NOTICE, "res_odbc loaded.\n");
  351. return 0;
  352. }
  353. char *description(void)
  354. {
  355. return tdesc;
  356. }
  357. int usecount(void)
  358. {
  359. int res;
  360. STANDARD_USECOUNT(res);
  361. return res;
  362. }
  363. char *key()
  364. {
  365. return ASTERISK_GPL_KEY;
  366. }