app_sql_postgres.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Connect to PostgreSQL
  5. *
  6. * Copyright (C) 2002, Christos Ricudis
  7. *
  8. * Christos Ricudis <ricudis@itc.auth.gr>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/file.h>
  14. #include <asterisk/logger.h>
  15. #include <asterisk/channel.h>
  16. #include <asterisk/pbx.h>
  17. #include <asterisk/module.h>
  18. #include <asterisk/linkedlists.h>
  19. #include <asterisk/chanvars.h>
  20. #include <asterisk/lock.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <sys/types.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include "libpq-fe.h"
  29. #define EXTRA_LOG 0
  30. static char *tdesc = "Simple PostgreSQL Interface";
  31. static char *app = "PGSQL";
  32. static char *synopsis = "Do several SQLy things";
  33. static char *descrip =
  34. "PGSQL(): Do several SQLy things\n"
  35. "Syntax:\n"
  36. " PGSQL(Connect var option-string)\n"
  37. " Connects to a database. Option string contains standard PostgreSQL\n"
  38. " parameters like host=, dbname=, user=. Connection identifer returned\n"
  39. " in ${var}\n"
  40. " PGSQL(Query var ${connection_identifier} query-string)\n"
  41. " Executes standard SQL query contained in query-string using established\n"
  42. " connection identified by ${connection_identifier}. Reseult of query is\n"
  43. " is stored in ${var}.\n"
  44. " PGSQL(Fetch statusvar ${result_identifier} var1 var2 ... varn)\n"
  45. " Fetches a single row from a result set contained in ${result_identifier}.\n"
  46. " Assigns returned fields to ${var1} ... ${varn}. ${statusvar} is set TRUE\n"
  47. " if additional rows exist in reseult set.\n"
  48. " PGSQL(Clear ${result_identifier})\n"
  49. " Frees memory and datastructures associated with result set.\n"
  50. " PGSQL(Disconnect ${connection_identifier})\n"
  51. " Disconnects from named connection to PostgreSQL.\n" ;
  52. /*
  53. Syntax of SQL commands :
  54. Connect var option-string
  55. Connects to a database using the option-string and stores the
  56. connection identifier in ${var}
  57. Query var ${connection_identifier} query-string
  58. Submits query-string to database backend and stores the result
  59. identifier in ${var}
  60. Fetch statusvar ${result_identifier} var1 var2 var3 ... varn
  61. Fetches a row from the query and stores end-of-table status in
  62. ${statusvar} and columns in ${var1}..${varn}
  63. Clear ${result_identifier}
  64. Clears data structures associated with ${result_identifier}
  65. Disconnect ${connection_identifier}
  66. Disconnects from named connection
  67. EXAMPLES OF USE :
  68. exten => s,2,PGSQL(Connect connid host=localhost user=asterisk dbname=credit)
  69. exten => s,3,PGSQL(Query resultid ${connid} SELECT username,credit FROM credit WHERE callerid=${CALLERIDNUM})
  70. exten => s,4,PGSQL(Fetch fetchid ${resultid} datavar1 datavar2)
  71. exten => s,5,GotoIf(${fetchid}?6:8)
  72. exten => s,6,Festival("User ${datavar1} currently has credit balance of ${datavar2} dollars.")
  73. exten => s,7,Goto(s,4)
  74. exten => s,8,PGSQL(Clear ${resultid})
  75. exten => s,9,PGSQL(Disconnect ${connid})
  76. */
  77. STANDARD_LOCAL_USER;
  78. LOCAL_USER_DECL;
  79. #define AST_PGSQL_ID_DUMMY 0
  80. #define AST_PGSQL_ID_CONNID 1
  81. #define AST_PGSQL_ID_RESID 2
  82. #define AST_PGSQL_ID_FETCHID 3
  83. struct ast_PGSQL_id {
  84. int identifier_type; /* 0=dummy, 1=connid, 2=resultid */
  85. int identifier;
  86. void *data;
  87. AST_LIST_ENTRY(ast_PGSQL_id) entries;
  88. } *ast_PGSQL_id;
  89. AST_LIST_HEAD(PGSQLidshead,ast_PGSQL_id) PGSQLidshead;
  90. static void *find_identifier(int identifier,int identifier_type) {
  91. struct PGSQLidshead *headp;
  92. struct ast_PGSQL_id *i;
  93. void *res=NULL;
  94. int found=0;
  95. headp=&PGSQLidshead;
  96. if (AST_LIST_LOCK(headp)) {
  97. ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
  98. } else {
  99. AST_LIST_TRAVERSE(headp,i,entries) {
  100. if ((i->identifier==identifier) && (i->identifier_type==identifier_type)) {
  101. found=1;
  102. res=i->data;
  103. break;
  104. }
  105. }
  106. if (!found) {
  107. ast_log(LOG_WARNING,"Identifier %d, identifier_type %d not found in identifier list\n",identifier,identifier_type);
  108. }
  109. AST_LIST_UNLOCK(headp);
  110. }
  111. return(res);
  112. }
  113. static int add_identifier(int identifier_type,void *data) {
  114. struct ast_PGSQL_id *i,*j;
  115. struct PGSQLidshead *headp;
  116. int maxidentifier=0;
  117. headp=&PGSQLidshead;
  118. i=NULL;
  119. j=NULL;
  120. if (AST_LIST_LOCK(headp)) {
  121. ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
  122. return(-1);
  123. } else {
  124. i=malloc(sizeof(struct ast_PGSQL_id));
  125. AST_LIST_TRAVERSE(headp,j,entries) {
  126. if (j->identifier>maxidentifier) {
  127. maxidentifier=j->identifier;
  128. }
  129. }
  130. i->identifier=maxidentifier+1;
  131. i->identifier_type=identifier_type;
  132. i->data=data;
  133. AST_LIST_INSERT_HEAD(headp,i,entries);
  134. AST_LIST_UNLOCK(headp);
  135. }
  136. return(i->identifier);
  137. }
  138. static int del_identifier(int identifier,int identifier_type) {
  139. struct ast_PGSQL_id *i;
  140. struct PGSQLidshead *headp;
  141. int found=0;
  142. headp=&PGSQLidshead;
  143. if (AST_LIST_LOCK(headp)) {
  144. ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
  145. } else {
  146. AST_LIST_TRAVERSE(headp,i,entries) {
  147. if ((i->identifier==identifier) &&
  148. (i->identifier_type==identifier_type)) {
  149. AST_LIST_REMOVE(headp,i,ast_PGSQL_id,entries);
  150. free(i);
  151. found=1;
  152. break;
  153. }
  154. }
  155. AST_LIST_UNLOCK(headp);
  156. }
  157. if (found==0) {
  158. ast_log(LOG_WARNING,"Could not find identifier %d, identifier_type %d in list to delete\n",identifier,identifier_type);
  159. return(-1);
  160. } else {
  161. return(0);
  162. }
  163. }
  164. static int aPGSQL_connect(struct ast_channel *chan, void *data) {
  165. char *s1;
  166. char s[100] = "";
  167. char *optionstring;
  168. char *var;
  169. int l;
  170. int res;
  171. PGconn *karoto;
  172. int id;
  173. char *stringp=NULL;
  174. res=0;
  175. l=strlen(data)+2;
  176. s1=malloc(l);
  177. strncpy(s1, data, l -1);
  178. stringp=s1;
  179. strsep(&stringp," "); // eat the first token, we already know it :P
  180. var=strsep(&stringp," ");
  181. optionstring=strsep(&stringp,"\n");
  182. karoto = PQconnectdb(optionstring);
  183. if (PQstatus(karoto) == CONNECTION_BAD) {
  184. ast_log(LOG_WARNING,"Connection to database using '%s' failed. postgress reports : %s\n", optionstring,
  185. PQerrorMessage(karoto));
  186. res=-1;
  187. } else {
  188. ast_log(LOG_WARNING,"adding identifier\n");
  189. id=add_identifier(AST_PGSQL_ID_CONNID,karoto);
  190. snprintf(s, sizeof(s), "%d", id);
  191. pbx_builtin_setvar_helper(chan,var,s);
  192. }
  193. free(s1);
  194. return res;
  195. }
  196. static int aPGSQL_query(struct ast_channel *chan, void *data) {
  197. char *s1,*s2,*s3,*s4;
  198. char s[100] = "";
  199. char *querystring;
  200. char *var;
  201. int l;
  202. int res,nres;
  203. PGconn *karoto;
  204. PGresult *PGSQLres;
  205. int id,id1;
  206. char *stringp=NULL;
  207. res=0;
  208. l=strlen(data)+2;
  209. s1=malloc(l);
  210. s2=malloc(l);
  211. strncpy(s1, data, l - 1);
  212. stringp=s1;
  213. strsep(&stringp," "); // eat the first token, we already know it :P
  214. s3=strsep(&stringp," ");
  215. while (1) { // ugly trick to make branches with break;
  216. var=s3;
  217. s4=strsep(&stringp," ");
  218. id=atoi(s4);
  219. querystring=strsep(&stringp,"\n");
  220. if ((karoto=find_identifier(id,AST_PGSQL_ID_CONNID))==NULL) {
  221. ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aPGSQL_query\n",id);
  222. res=-1;
  223. break;
  224. }
  225. PGSQLres=PQexec(karoto,querystring);
  226. if (PGSQLres==NULL) {
  227. ast_log(LOG_WARNING,"aPGSQL_query: Connection Error (connection identifier = %d, error message : %s)\n",id,PQerrorMessage(karoto));
  228. res=-1;
  229. break;
  230. }
  231. if (PQresultStatus(PGSQLres) == PGRES_BAD_RESPONSE ||
  232. PQresultStatus(PGSQLres) == PGRES_NONFATAL_ERROR ||
  233. PQresultStatus(PGSQLres) == PGRES_FATAL_ERROR) {
  234. ast_log(LOG_WARNING,"aPGSQL_query: Query Error (connection identifier : %d, error message : %s)\n",id,PQcmdStatus(PGSQLres));
  235. res=-1;
  236. break;
  237. }
  238. nres=PQnfields(PGSQLres);
  239. id1=add_identifier(AST_PGSQL_ID_RESID,PGSQLres);
  240. snprintf(s, sizeof(s), "%d", id1);
  241. pbx_builtin_setvar_helper(chan,var,s);
  242. break;
  243. }
  244. free(s1);
  245. free(s2);
  246. return(res);
  247. }
  248. static int aPGSQL_fetch(struct ast_channel *chan, void *data) {
  249. char *s1,*s2,*fetchid_var,*s4,*s5,*s6,*s7;
  250. char s[100];
  251. char *var;
  252. int l;
  253. int res;
  254. PGresult *PGSQLres;
  255. int id,id1,i,j,fnd;
  256. int *lalares=NULL;
  257. int nres;
  258. struct ast_var_t *variables;
  259. struct varshead *headp;
  260. char *stringp=NULL;
  261. headp=&chan->varshead;
  262. res=0;
  263. l=strlen(data)+2;
  264. s7=NULL;
  265. s1=malloc(l);
  266. s2=malloc(l);
  267. strncpy(s1, data, l - 1);
  268. stringp=s1;
  269. strsep(&stringp," "); // eat the first token, we already know it :P
  270. fetchid_var=strsep(&stringp," ");
  271. while (1) { // ugly trick to make branches with break;
  272. var=fetchid_var; // fetchid
  273. fnd=0;
  274. AST_LIST_TRAVERSE(headp,variables,entries) {
  275. if (strncasecmp(ast_var_name(variables),fetchid_var,strlen(fetchid_var))==0) {
  276. s7=ast_var_value(variables);
  277. fnd=1;
  278. break;
  279. }
  280. }
  281. if (fnd==0) {
  282. s7="0";
  283. pbx_builtin_setvar_helper(chan,fetchid_var,s7);
  284. }
  285. s4=strsep(&stringp," ");
  286. id=atoi(s4); // resultid
  287. if ((PGSQLres=find_identifier(id,AST_PGSQL_ID_RESID))==NULL) {
  288. ast_log(LOG_WARNING,"Invalid result identifier %d passed in aPGSQL_fetch\n",id);
  289. res=-1;
  290. break;
  291. }
  292. id=atoi(s7); //fetchid
  293. if ((lalares=find_identifier(id,AST_PGSQL_ID_FETCHID))==NULL) {
  294. i=0; // fetching the very first row
  295. } else {
  296. i=*lalares;
  297. free(lalares);
  298. del_identifier(id,AST_PGSQL_ID_FETCHID); // will re-add it a bit later
  299. }
  300. if (i<PQntuples(PGSQLres)) {
  301. nres=PQnfields(PGSQLres);
  302. ast_log(LOG_WARNING,"ast_PGSQL_fetch : nres = %d i = %d ;\n",nres,i);
  303. for (j=0;j<nres;j++) {
  304. s5=strsep(&stringp," ");
  305. if (s5==NULL) {
  306. ast_log(LOG_WARNING,"ast_PGSQL_fetch : More tuples (%d) than variables (%d)\n",nres,j);
  307. break;
  308. }
  309. s6=PQgetvalue(PGSQLres,i,j);
  310. if (s6==NULL) {
  311. ast_log(LOG_WARNING,"PWgetvalue(res,%d,%d) returned NULL in ast_PGSQL_fetch\n",i,j);
  312. break;
  313. }
  314. ast_log(LOG_WARNING,"===setting variable '%s' to '%s'\n",s5,s6);
  315. pbx_builtin_setvar_helper(chan,s5,s6);
  316. }
  317. lalares=malloc(sizeof(int));
  318. *lalares = ++i; // advance to the next row
  319. id1 = add_identifier(AST_PGSQL_ID_FETCHID,lalares);
  320. } else {
  321. ast_log(LOG_WARNING,"ast_PGSQL_fetch : EOF\n");
  322. id1 = 0; // no more rows
  323. }
  324. snprintf(s, sizeof(s), "%d", id1);
  325. ast_log(LOG_WARNING,"Setting var '%s' to value '%s'\n",fetchid_var,s);
  326. pbx_builtin_setvar_helper(chan,fetchid_var,s);
  327. break;
  328. }
  329. free(s1);
  330. free(s2);
  331. return(res);
  332. }
  333. static int aPGSQL_reset(struct ast_channel *chan, void *data) {
  334. char *s1,*s3;
  335. int l;
  336. PGconn *karoto;
  337. int id;
  338. char *stringp=NULL;
  339. l=strlen(data)+2;
  340. s1=malloc(l);
  341. strncpy(s1, data, l - 1);
  342. stringp=s1;
  343. strsep(&stringp," "); // eat the first token, we already know it :P
  344. s3=strsep(&stringp," ");
  345. id=atoi(s3);
  346. if ((karoto=find_identifier(id,AST_PGSQL_ID_CONNID))==NULL) {
  347. ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aPGSQL_reset\n",id);
  348. } else {
  349. PQreset(karoto);
  350. }
  351. free(s1);
  352. return(0);
  353. }
  354. static int aPGSQL_clear(struct ast_channel *chan, void *data) {
  355. char *s1,*s3;
  356. int l;
  357. PGresult *karoto;
  358. int id;
  359. char *stringp=NULL;
  360. l=strlen(data)+2;
  361. s1=malloc(l);
  362. strncpy(s1, data, l - 1);
  363. stringp=s1;
  364. strsep(&stringp," "); // eat the first token, we already know it :P
  365. s3=strsep(&stringp," ");
  366. id=atoi(s3);
  367. if ((karoto=find_identifier(id,AST_PGSQL_ID_RESID))==NULL) {
  368. ast_log(LOG_WARNING,"Invalid result identifier %d passed in aPGSQL_clear\n",id);
  369. } else {
  370. PQclear(karoto);
  371. del_identifier(id,AST_PGSQL_ID_RESID);
  372. }
  373. free(s1);
  374. return(0);
  375. }
  376. static int aPGSQL_disconnect(struct ast_channel *chan, void *data) {
  377. char *s1,*s3;
  378. int l;
  379. PGconn *karoto;
  380. int id;
  381. char *stringp=NULL;
  382. l=strlen(data)+2;
  383. s1=malloc(l);
  384. strncpy(s1, data, l - 1);
  385. stringp=s1;
  386. strsep(&stringp," "); // eat the first token, we already know it :P
  387. s3=strsep(&stringp," ");
  388. id=atoi(s3);
  389. if ((karoto=find_identifier(id,AST_PGSQL_ID_CONNID))==NULL) {
  390. ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aPGSQL_disconnect\n",id);
  391. } else {
  392. PQfinish(karoto);
  393. del_identifier(id,AST_PGSQL_ID_CONNID);
  394. }
  395. free(s1);
  396. return(0);
  397. }
  398. static int aPGSQL_debug(struct ast_channel *chan, void *data) {
  399. ast_log(LOG_WARNING,"Debug : %s\n",(char *)data);
  400. return(0);
  401. }
  402. static int PGSQL_exec(struct ast_channel *chan, void *data)
  403. {
  404. struct localuser *u;
  405. int result;
  406. #if EXTRA_LOG
  407. printf("PRSQL_exec: data=%s\n",(char*)data);
  408. #endif
  409. if (!data) {
  410. ast_log(LOG_WARNING, "APP_PGSQL requires an argument (see manual)\n");
  411. return -1;
  412. }
  413. LOCAL_USER_ADD(u);
  414. result=0;
  415. if (strncasecmp("connect",data,strlen("connect"))==0) {
  416. result=(aPGSQL_connect(chan,data));
  417. } else if (strncasecmp("query",data,strlen("query"))==0) {
  418. result=(aPGSQL_query(chan,data));
  419. } else if (strncasecmp("fetch",data,strlen("fetch"))==0) {
  420. result=(aPGSQL_fetch(chan,data));
  421. } else if (strncasecmp("reset",data,strlen("reset"))==0) {
  422. result=(aPGSQL_reset(chan,data));
  423. } else if (strncasecmp("clear",data,strlen("clear"))==0) {
  424. result=(aPGSQL_clear(chan,data));
  425. } else if (strncasecmp("debug",data,strlen("debug"))==0) {
  426. result=(aPGSQL_debug(chan,data));
  427. } else if (strncasecmp("disconnect",data,strlen("disconnect"))==0) {
  428. result=(aPGSQL_disconnect(chan,data));
  429. } else {
  430. ast_log(LOG_WARNING, "Unknown APP_PGSQL argument : %s\n",(char *)data);
  431. result=-1;
  432. }
  433. LOCAL_USER_REMOVE(u);
  434. return result;
  435. }
  436. int unload_module(void)
  437. {
  438. STANDARD_HANGUP_LOCALUSERS;
  439. return ast_unregister_application(app);
  440. }
  441. int load_module(void)
  442. {
  443. struct PGSQLidshead *headp;
  444. headp=&PGSQLidshead;
  445. AST_LIST_HEAD_INIT(headp);
  446. return ast_register_application(app, PGSQL_exec, synopsis, descrip);
  447. }
  448. char *description(void)
  449. {
  450. return tdesc;
  451. }
  452. int usecount(void)
  453. {
  454. int res;
  455. STANDARD_USECOUNT(res);
  456. return res;
  457. }
  458. char *key()
  459. {
  460. return ASTERISK_GPL_KEY;
  461. }