parse_identity.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2007 iptelorg GmbH
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. /*! \file
  26. * \brief Parser :: Parse Identity header field
  27. *
  28. * \ingroup parser
  29. */
  30. #include <string.h>
  31. #include "parse_identity.h"
  32. #include "parse_def.h"
  33. #include "parser_f.h" /* eat_space_end and so on */
  34. #include "../mem/mem.h"
  35. #include "../ut.h"
  36. /*
  37. * Parse Identity header field
  38. */
  39. #define SP(_c) ((_c)=='\t' || (_c)==' ')
  40. inline static int isendofhash (char* p, char* end)
  41. {
  42. /* new header line */
  43. if ((p<end && *p=='"')
  44. /* end of message */
  45. || ((*p=='\n' || *p=='\r') && p+1==end))
  46. return 1;
  47. else
  48. return 0;
  49. }
  50. /*! \brief
  51. * If the value of Identity header contains any LWS then we've to create
  52. * a new buffer and move there the LWSless part
  53. */
  54. int movetomybuffer (char *pstart,
  55. char *pend,
  56. char *pcur,
  57. struct identity_body *ib)
  58. {
  59. char *phashend;
  60. for (phashend = pcur; !isendofhash(phashend, pend); phashend++);
  61. if (!(ib->hash.s=pkg_malloc(phashend-pstart))) {
  62. LOG(L_ERR, "parse_identity: out of memory\n");
  63. return -2;
  64. }
  65. ib->ballocated=1;
  66. memcpy(ib->hash.s, pstart, ib->hash.len);
  67. return 0;
  68. }
  69. void parse_identity(char *buffer, char* end, struct identity_body* ib)
  70. {
  71. char *p=NULL, *pstart=NULL;
  72. if (!buffer || !end || !ib)
  73. goto error;
  74. ib->error=PARSE_ERROR;
  75. /* if there is a '"' sign then we'll step over it */
  76. *buffer == '"' ? (pstart = buffer + 1) : (pstart = buffer);
  77. ib->hash.s=pstart;
  78. ib->hash.len=0;
  79. for (p = pstart; p < end; p++) {
  80. /* check the BASE64 alphabet */
  81. if (((*p >= 'a' && *p <='z')
  82. || (*p >= 'A' && *p <='Z')
  83. || (*p >= '0' && *p <='9')
  84. || (*p == '+' || *p == '/' || *p == '='))) {
  85. if (ib->ballocated)
  86. ib->hash.s[ib->hash.len]=*p;
  87. ib->hash.len++;
  88. continue;
  89. }
  90. /* LWS */
  91. if (*p=='\n' && p+1<end && SP(*(p+1))) {
  92. /* p - 1 because we don't want to pass '\n' */
  93. if (!ib->ballocated && (movetomybuffer(pstart, end, p-1, ib)))
  94. goto error;
  95. /* p + 1 < end because 'continue' increases p so we'd skip \n
  96. we need after this for loop */
  97. for (p+=1; p + 1 < end && SP(*(p + 1)); p++);
  98. continue;
  99. }
  100. if (*p=='\r' && p+2<end && *(p+1)=='\n' && SP(*(p+2))) {
  101. if (!ib->ballocated && (movetomybuffer(pstart, end, p-1, ib)))
  102. goto error;
  103. for (p+=2; p + 1 < end && SP(*(p + 1)); p++);
  104. continue;
  105. }
  106. if (isendofhash(p, end))
  107. break;
  108. /* parse error */
  109. goto parseerror;
  110. }
  111. /* this is the final quotation mark so we step over */
  112. ib->error=PARSE_OK;
  113. return ;
  114. parseerror:
  115. LOG( L_ERR , "ERROR: parse_identity: "
  116. "unexpected char [0x%X]: <<%.*s>> .\n",
  117. *p,(int)(p-buffer), ZSW(buffer));
  118. error:
  119. return ;
  120. }
  121. int parse_identity_header(struct sip_msg *msg)
  122. {
  123. struct identity_body* identity_b;
  124. if ( !msg->identity
  125. && (parse_headers(msg,HDR_IDENTITY_F,0)==-1
  126. || !msg->identity) ) {
  127. LOG(L_ERR,"ERROR:parse_identity_header: bad msg or missing IDENTITY header\n");
  128. goto error;
  129. }
  130. /* maybe the header is already parsed! */
  131. if (msg->identity->parsed)
  132. return 0;
  133. identity_b=pkg_malloc(sizeof(*identity_b));
  134. if (identity_b==0){
  135. LOG(L_ERR, "ERROR:parse_identity_header: out of memory\n");
  136. goto error;
  137. }
  138. memset(identity_b, 0, sizeof(*identity_b));
  139. parse_identity(msg->identity->body.s,
  140. msg->identity->body.s + msg->identity->body.len+1,
  141. identity_b);
  142. if (identity_b->error==PARSE_ERROR){
  143. free_identity(identity_b);
  144. goto error;
  145. }
  146. msg->identity->parsed=(void*)identity_b;
  147. return 0;
  148. error:
  149. return -1;
  150. }
  151. void free_identity(struct identity_body *ib)
  152. {
  153. if (ib->ballocated)
  154. pkg_free(ib->hash.s);
  155. pkg_free(ib);
  156. }