cpl_rpc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. *
  27. */
  28. #include <stdio.h>
  29. #include <sys/uio.h>
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <fcntl.h>
  35. #include <sys/uio.h>
  36. #include <errno.h>
  37. #include <string.h>
  38. #include <ctype.h>
  39. #include "../../str.h"
  40. #include "../../dprint.h"
  41. #include "../../mem/mem.h"
  42. #include "../../mem/shm_mem.h"
  43. #include "cpl_db.h"
  44. #include "cpl_parser.h"
  45. #include "cpl_loader.h"
  46. #include "cpl_rpc.h"
  47. static inline int check_userhost( char *p, char *end)
  48. {
  49. char *p1;
  50. int dot;
  51. /* parse user name */
  52. p1 = p;
  53. while (p<end && (isalnum((int)*p) || *p=='-' || *p=='_' || *p=='.' ))
  54. p++;
  55. if (p==p1 || p==end || *p!='@')
  56. return -1;
  57. p++;
  58. /* parse the host part */
  59. dot = 1;
  60. p1 = p;
  61. while (p<end) {
  62. if (*p=='.') {
  63. if (dot) return -1; /* dot after dot */
  64. dot = 1;
  65. } else if (isalnum((int)*p) || *p=='-' || *p=='_' ) {
  66. dot = 0;
  67. } else {
  68. return -1;
  69. }
  70. p++;
  71. }
  72. if (p1==p || dot)
  73. return -1;
  74. return 0;
  75. }
  76. static const char* cpl_load_doc[] = {
  77. "Load a CPL script to the server.", /* Documentation string */
  78. 0 /* Method signature(s) */
  79. };
  80. static void cpl_load(rpc_t* rpc, void* c)
  81. {
  82. char* cpl_file;
  83. int cpl_file_len;
  84. str user;
  85. str xml = STR_NULL;
  86. str bin = STR_NULL;
  87. str enc_log = STR_NULL;
  88. DBG("DEBUG:cpl-c:cpl_load: \"LOAD_CPL\" FIFO command received!\n");
  89. if (rpc->scan(c, "s", &user.s) < 1) {
  90. rpc->fault(c, 400, "Username parameter not found");
  91. return;
  92. }
  93. user.len = strlen(user.s);
  94. DBG("DEBUG:cpl_load: user=%.*s\n", user.len, user.s);
  95. if (rpc->scan(c, "s", &cpl_file) < 1) {
  96. rpc->fault(c, 400, "CPL file name expected");
  97. return;
  98. }
  99. cpl_file_len = strlen(cpl_file);
  100. DBG("DEBUG:cpl-c:cpl_load: cpl file=%s\n", cpl_file);
  101. /* check user+host */
  102. if (check_userhost( user.s, user.s+user.len)!=0) {
  103. LOG(L_ERR,"ERROR:cpl-c:cpl_load: invalid user@host [%.*s]\n",
  104. user.len,user.s);
  105. rpc->fault(c, 400, "Bad user@host: %.*s", user.len, user.s);
  106. return;
  107. }
  108. /* load the xml file - this function will allocated a buff for the loading
  109. * the cpl file and attach it to xml.s -> don't forget to free it! */
  110. if (load_file( cpl_file, &xml)!=1) {
  111. rpc->fault(c, 400, "Cannot read CPL file\n");
  112. goto error;
  113. }
  114. /* get the binary coding for the XML file */
  115. if (encodeCPL( &xml, &bin, &enc_log)!=1) {
  116. rpc->fault(c, 400, "%.*s", enc_log.len, enc_log.s);
  117. goto error;
  118. }
  119. /* write both the XML and binary formats into database */
  120. if (write_to_db(user.s, &xml, &bin)!=1) {
  121. rpc->fault(c, 400, "Cannot save CPL to database");
  122. goto error;
  123. }
  124. /* free the memory used for storing the cpl script in XML format */
  125. pkg_free( xml.s );
  126. /* everything was OK -> dump the logs into response file */
  127. rpc->add(c, "S", &enc_log);
  128. if (enc_log.s) pkg_free ( enc_log.s );
  129. return;
  130. error:
  131. if (enc_log.s) pkg_free ( enc_log.s );
  132. if (xml.s) pkg_free ( xml.s );
  133. }
  134. static const char* cpl_remove_doc[] = {
  135. "Remove a CPL script from server.", /* Documentation string */
  136. 0 /* Method signature(s) */
  137. };
  138. static void cpl_remove(rpc_t* rpc, void* c)
  139. {
  140. char* user;
  141. int user_len;
  142. DBG("DEBUG:cpl-c:cpl_remove: \"REMOVE_CPL\" FIFO command received!\n");
  143. if (rpc->scan(c, "s", &user) < 1) {
  144. rpc->fault(c, 400, "Username parameter not found");
  145. return;
  146. }
  147. user_len = strlen(user);
  148. /* check user+host */
  149. if (check_userhost( user, user+user_len)!=0) {
  150. LOG(L_ERR,"ERROR:cpl-c:cpl_remove: invalid user@host [%.*s]\n",
  151. user_len,user);
  152. rpc->fault(c, 400, "Bad user@host: '%s'", user);
  153. return;
  154. }
  155. if (rmv_from_db(user)!=1) {
  156. rpc->fault(c, 400, "Error while removing CPL script of %s from database", user);
  157. return;
  158. }
  159. }
  160. static const char* cpl_get_doc[] = {
  161. "Return a CPL script.", /* Documentation string */
  162. 0 /* Method signature(s) */
  163. };
  164. static void cpl_get(rpc_t* rpc, void* c)
  165. {
  166. str user;
  167. str script = STR_NULL;
  168. if (rpc->scan(c, "S", &user) < 1) {
  169. rpc->fault(c, 400, "Username parameter expected");
  170. return;
  171. }
  172. DBG("DEBUG:cpl-c:cpl_get: user=%.*s\n", user.len, user.s);
  173. /* check user+host */
  174. if (check_userhost( user.s, user.s+user.len)!=0) {
  175. LOG(L_ERR,"ERROR:cpl-c:cpl_load: invalid user@host [%.*s]\n",
  176. user.len,user.s);
  177. rpc->fault(c, 400, "Bad user@host '%.*s'", user.len, user.s);
  178. return;
  179. }
  180. /* get the script for this user */
  181. if (get_user_script(&user, &script, 0)==-1) {
  182. rpc->fault(c, 500, "Database query failed");
  183. return;
  184. }
  185. rpc->add(c, "S", &script);
  186. if (script.s) shm_free( script.s );
  187. }
  188. /*
  189. * RPC Methods exported by this module
  190. */
  191. rpc_export_t cpl_rpc_methods[] = {
  192. {"cpl.load", cpl_load, cpl_load_doc, 0},
  193. {"cpl.remove", cpl_remove, cpl_remove_doc, 0},
  194. {"cpl.get", cpl_get, cpl_get_doc, 0},
  195. {0, 0, 0, 0}
  196. };