cpl_loader.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. * History:
  29. * -------
  30. * 2003-08-21: cpl_remove() added (bogdan)
  31. * 2003-06-24: file created (bogdan)
  32. */
  33. #include <stdio.h>
  34. #include <sys/uio.h>
  35. #include <stdlib.h>
  36. #include <unistd.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <fcntl.h>
  40. #include <sys/uio.h>
  41. #include <errno.h>
  42. #include <string.h>
  43. #include <ctype.h>
  44. #include "../../str.h"
  45. #include "../../dprint.h"
  46. #include "../../mem/mem.h"
  47. #include "../../mem/shm_mem.h"
  48. #include "cpl_db.h"
  49. #include "cpl_parser.h"
  50. #include "cpl_loader.h"
  51. #define MAX_STATIC_BUF 256
  52. extern db_con_t* db_hdl;
  53. #if 0
  54. /* debug function -> write into a file the content of a str struct. */
  55. int write_to_file(char *filename, str *buf)
  56. {
  57. int fd;
  58. int ret;
  59. fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,0644);
  60. if (!fd) {
  61. LOG(L_ERR,"ERROR:cpl-c:write_to_file: cannot open file : %s\n",
  62. strerror(errno));
  63. goto error;
  64. }
  65. while ( (ret=write( fd, buf->s, buf->len))!=buf->len) {
  66. if ((ret==-1 && errno!=EINTR)|| ret!=-1) {
  67. LOG(L_ERR,"ERROR:cpl-c:write_to_file:cannot write to file:"
  68. "%s write_ret=%d\n",strerror(errno), ret );
  69. goto error;
  70. }
  71. }
  72. close(fd);
  73. return 0;
  74. error:
  75. return -1;
  76. }
  77. #endif
  78. /* Loads a file into a buffer; first the file length will be determined for
  79. * allocated an exact buffer len for storing the file content into.
  80. * Returns: 1 - success
  81. * -1 - error
  82. */
  83. int load_file( char *filename, str *xml)
  84. {
  85. int n;
  86. int offset;
  87. int fd;
  88. xml->s = 0;
  89. xml->len = 0;
  90. /* open the file for reading */
  91. fd = open(filename,O_RDONLY);
  92. if (fd==-1) {
  93. LOG(L_ERR,"ERROR:cpl-c:load_file: cannot open file for reading:"
  94. " %s\n",strerror(errno));
  95. goto error;
  96. }
  97. /* get the file length */
  98. if ( (xml->len=lseek(fd,0,SEEK_END))==-1) {
  99. LOG(L_ERR,"ERROR:cpl-c:load_file: cannot get file length (lseek):"
  100. " %s\n", strerror(errno));
  101. goto error;
  102. }
  103. DBG("DEBUG:cpl-c:load_file: file size = %d\n",xml->len);
  104. if ( lseek(fd,0,SEEK_SET)==-1 ) {
  105. LOG(L_ERR,"ERROR:cpl-c:load_file: cannot go to beginning (lseek):"
  106. " %s\n",strerror(errno));
  107. goto error;
  108. }
  109. /* get some memory */
  110. xml->s = (char*)pkg_malloc( xml->len+1/*null terminated*/ );
  111. if (!xml->s) {
  112. LOG(L_ERR,"ERROR:cpl-c:load_file: no more free pkg memory\n");
  113. goto error;
  114. }
  115. /*start reading */
  116. offset = 0;
  117. while ( offset<xml->len ) {
  118. n=read( fd, xml->s+offset, xml->len-offset);
  119. if (n==-1) {
  120. if (errno!=EINTR) {
  121. LOG(L_ERR,"ERROR:cpl-c:load_file: read failed:"
  122. " %s\n", strerror(errno));
  123. goto error;
  124. }
  125. } else {
  126. if (n==0) break;
  127. offset += n;
  128. }
  129. }
  130. if (xml->len!=offset) {
  131. LOG(L_ERR,"ERROR:cpl-c:load_file: couldn't read all file!\n");
  132. goto error;
  133. }
  134. xml->s[xml->len] = 0;
  135. close(fd);
  136. return 1;
  137. error:
  138. if (fd!=-1) close(fd);
  139. if (xml->s) pkg_free( xml->s);
  140. return -1;
  141. }
  142. /* Writes an array of texts into the given response file.
  143. * Accepts also empty texts, case in which it will be created an empty
  144. * response file.
  145. */
  146. void write_to_file( char *file, str *txt, int n )
  147. {
  148. int fd;
  149. /* open file for write */
  150. fd = open( file, O_WRONLY|O_CREAT|O_TRUNC/*|O_NOFOLLOW*/, 0600 );
  151. if (fd==-1) {
  152. LOG(L_ERR,"ERROR:cpl-c:write_to_file: cannot open response file "
  153. "<%s>: %s\n", file, strerror(errno));
  154. return;
  155. }
  156. /* write the txt, if any */
  157. if (n>0) {
  158. again:
  159. if ( writev( fd, (struct iovec*)txt, n)==-1) {
  160. if (errno==EINTR) {
  161. goto again;
  162. } else {
  163. LOG(L_ERR,"ERROR:cpl-c:write_logs_to_file: writev failed: "
  164. "%s\n", strerror(errno) );
  165. }
  166. }
  167. }
  168. /* close the file*/
  169. close( fd );
  170. return;
  171. }
  172. static inline int check_userhost( char *p, char *end)
  173. {
  174. char *p1;
  175. int dot;
  176. /* parse user name */
  177. p1 = p;
  178. while (p<end && (isalnum((int)*p) || *p=='-' || *p=='_' || *p=='.' ))
  179. p++;
  180. if (p==p1 || p==end || *p!='@')
  181. return -1;
  182. p++;
  183. /* parse the host part */
  184. dot = 1;
  185. p1 = p;
  186. while (p<end) {
  187. if (*p=='.') {
  188. if (dot) return -1; /* dot after dot */
  189. dot = 1;
  190. } else if (isalnum((int)*p) || *p=='-' || *p=='_' ) {
  191. dot = 0;
  192. } else {
  193. return -1;
  194. }
  195. p++;
  196. }
  197. if (p1==p || dot)
  198. return -1;
  199. return 0;
  200. }
  201. #undef MAX_STATIC_BUF