parse_identity.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (c) 2007 iptelorg GmbH
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. /*! \file
  28. * \brief Parser :: Parse Identity header field
  29. *
  30. * \ingroup parser
  31. */
  32. #include <string.h>
  33. #include "parse_identity.h"
  34. #include "parse_def.h"
  35. #include "parser_f.h" /* eat_space_end and so on */
  36. #include "../mem/mem.h"
  37. #include "../ut.h"
  38. /*
  39. * Parse Identity header field
  40. */
  41. #define SP(_c) ((_c)=='\t' || (_c)==' ')
  42. inline static int isendofhash (char* p, char* end)
  43. {
  44. /* new header line */
  45. if ((p<end && *p=='"')
  46. /* end of message */
  47. || ((*p=='\n' || *p=='\r') && p+1==end))
  48. return 1;
  49. else
  50. return 0;
  51. }
  52. /*! \brief
  53. * If the value of Identity header contains any LWS then we've to create
  54. * a new buffer and move there the LWSless part
  55. */
  56. int movetomybuffer (char *pstart,
  57. char *pend,
  58. char *pcur,
  59. struct identity_body *ib)
  60. {
  61. char *phashend;
  62. for (phashend = pcur; !isendofhash(phashend, pend); phashend++);
  63. if (!(ib->hash.s=pkg_malloc(phashend-pstart))) {
  64. LOG(L_ERR, "parse_identity: out of memory\n");
  65. return -2;
  66. }
  67. ib->ballocated=1;
  68. memcpy(ib->hash.s, pstart, ib->hash.len);
  69. return 0;
  70. }
  71. void parse_identity(char *buffer, char* end, struct identity_body* ib)
  72. {
  73. char *p=NULL, *pstart=NULL;
  74. if (!buffer || !end || !ib)
  75. goto error;
  76. ib->error=PARSE_ERROR;
  77. /* if there is a '"' sign then we'll step over it */
  78. *buffer == '"' ? (pstart = buffer + 1) : (pstart = buffer);
  79. ib->hash.s=pstart;
  80. ib->hash.len=0;
  81. for (p = pstart; p < end; p++) {
  82. /* check the BASE64 alphabet */
  83. if (((*p >= 'a' && *p <='z')
  84. || (*p >= 'A' && *p <='Z')
  85. || (*p >= '0' && *p <='9')
  86. || (*p == '+' || *p == '/' || *p == '='))) {
  87. if (ib->ballocated)
  88. ib->hash.s[ib->hash.len]=*p;
  89. ib->hash.len++;
  90. continue;
  91. }
  92. /* LWS */
  93. if (*p=='\n' && p+1<end && SP(*(p+1))) {
  94. /* p - 1 because we don't want to pass '\n' */
  95. if (!ib->ballocated && (movetomybuffer(pstart, end, p-1, ib)))
  96. goto error;
  97. /* p + 1 < end because 'continue' increases p so we'd skip \n
  98. we need after this for loop */
  99. for (p+=1; p + 1 < end && SP(*(p + 1)); p++);
  100. continue;
  101. }
  102. if (*p=='\r' && p+2<end && *(p+1)=='\n' && SP(*(p+2))) {
  103. if (!ib->ballocated && (movetomybuffer(pstart, end, p-1, ib)))
  104. goto error;
  105. for (p+=2; p + 1 < end && SP(*(p + 1)); p++);
  106. continue;
  107. }
  108. if (isendofhash(p, end))
  109. break;
  110. /* parse error */
  111. goto parseerror;
  112. }
  113. /* this is the final quotation mark so we step over */
  114. ib->error=PARSE_OK;
  115. return ;
  116. parseerror:
  117. LOG( L_ERR , "ERROR: parse_identity: "
  118. "unexpected char [0x%X]: <<%.*s>> .\n",
  119. *p,(int)(p-buffer), ZSW(buffer));
  120. error:
  121. return ;
  122. }
  123. int parse_identity_header(struct sip_msg *msg)
  124. {
  125. struct identity_body* identity_b;
  126. if ( !msg->identity
  127. && (parse_headers(msg,HDR_IDENTITY_F,0)==-1
  128. || !msg->identity) ) {
  129. LOG(L_ERR,"ERROR:parse_identity_header: bad msg or missing IDENTITY header\n");
  130. goto error;
  131. }
  132. /* maybe the header is already parsed! */
  133. if (msg->identity->parsed)
  134. return 0;
  135. identity_b=pkg_malloc(sizeof(*identity_b));
  136. if (identity_b==0){
  137. LOG(L_ERR, "ERROR:parse_identity_header: out of memory\n");
  138. goto error;
  139. }
  140. memset(identity_b, 0, sizeof(*identity_b));
  141. parse_identity(msg->identity->body.s,
  142. msg->identity->body.s + msg->identity->body.len+1,
  143. identity_b);
  144. if (identity_b->error==PARSE_ERROR){
  145. free_identity(identity_b);
  146. goto error;
  147. }
  148. msg->identity->parsed=(void*)identity_b;
  149. return 0;
  150. error:
  151. return -1;
  152. }
  153. void free_identity(struct identity_body *ib)
  154. {
  155. if (ib->ballocated)
  156. pkg_free(ib->hash.s);
  157. pkg_free(ib);
  158. }