📄 hdr_wwwauthenticate.c
字号:
/* The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-) Copyright (C) 2001,2002,2003 Aymeric MOIZARD jack@atosc.org This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#include <stdlib.h>#include <stdio.h>#include <osip/port.h>#include <osip/smsg.h>intwww_authenticate_init (www_authenticate_t ** dest){ *dest = (www_authenticate_t *) smalloc (sizeof (www_authenticate_t)); if (*dest == NULL) return -1; (*dest)->auth_type = NULL; (*dest)->realm = NULL; (*dest)->domain = NULL; /* optionnal */ (*dest)->nonce = NULL; (*dest)->opaque = NULL; /* optionnal */ (*dest)->stale = NULL; /* optionnal */ (*dest)->algorithm = NULL; /* optionnal */ (*dest)->qop_options = NULL; /* optionnal (contains a list of qop-value) */ return 0;}/* fills the www-authenticate header of message. *//* INPUT : char *hvalue | value of header. *//* OUTPUT: sip_t *sip | structure to save results. *//* returns -1 on error. */intmsg_setwww_authenticate (sip_t * sip, char *hvalue){ www_authenticate_t *www_authenticate; int i; if (sip == NULL || sip->www_authenticates == NULL) return -1; i = www_authenticate_init (&www_authenticate); if (i != 0) return -1; i = www_authenticate_parse (www_authenticate, hvalue); if (i != 0) { www_authenticate_free (www_authenticate); sfree (www_authenticate); return -1; }#ifdef USE_TMP_BUFFER sip->message_property = 2;#endif list_add (sip->www_authenticates, www_authenticate, -1); return 0;}intquoted_string_set (char *name, char *str, char **result, char **next){ *next = str; if (*result != NULL) return 0; /* already parsed */ *next = NULL; while ((' ' == *str) || ('\t' == *str) || (',' == *str)) if (*str) str++; else return -1; /* bad header format */ if (strlen (str) <= strlen (name)) return -1; /* bad header format... */#if (!defined WIN32 && !defined _WIN32_WCE) if (strncasecmp (name, str, strlen (name)) == 0)#else if (_strnicmp (name, str, strlen (name)) == 0)#endif { char *quote1; char *quote2; char *tmp; char *hack = strchr (str, '='); while (' ' == *(hack - 1)) /* get rid of extra spaces */ hack--; if ((size_t) (hack - str) != strlen (name)) { *next = str; return 0; } quote1 = quote_find (str); if (quote1 == NULL) return -1; /* bad header format... */ quote2 = quote_find (quote1 + 1); if (quote2 == NULL) return -1; /* bad header format... */ if (quote2 - quote1 == 1) { /* this is a special case! The quote contains nothing! */ /* example: Digest opaque="",cnonce="" */ /* in this case, we just forget the parameter... this */ /* this should prevent from user manipulating empty */ /* strings */ tmp = quote2 + 1; /* next element start here */ for (; *tmp == ' ' || *tmp == '\t'; tmp++) { } for (; *tmp == '\n' || *tmp == '\r'; tmp++) { } /* skip LWS */ *next = NULL; if (*tmp == '\0') /* end of header detected */ return 0; if (*tmp != '\t' && *tmp != ' ') /* LWS here ? */ *next = tmp; else { /* it is: skip it... */ for (; *tmp == ' ' || *tmp == '\t'; tmp++) { } if (*tmp == '\0') /* end of header detected */ return 0; *next = tmp; } return 0; } *result = (char *) smalloc (quote2 - quote1 + 3); if (*result == NULL) return -1; sstrncpy (*result, quote1, quote2 - quote1 + 1); tmp = quote2 + 1; /* next element start here */ for (; *tmp == ' ' || *tmp == '\t'; tmp++) { } for (; *tmp == '\n' || *tmp == '\r'; tmp++) { } /* skip LWS */ *next = NULL; if (*tmp == '\0') /* end of header detected */ return 0; if (*tmp != '\t' && *tmp != ' ') /* LWS here ? */ *next = tmp; else { /* it is: skip it... */ for (; *tmp == ' ' || *tmp == '\t'; tmp++) { } if (*tmp == '\0') /* end of header detected */ return 0; *next = tmp; } } else *next = str; /* wrong header asked! */ return 0;}inttoken_set (char *name, char *str, char **result, char **next){ char *beg; char *tmp; *next = str; if (*result != NULL) return 0; /* already parsed */ *next = NULL; beg = strchr (str, '='); if (beg == NULL) return -1; /* bad header format... */ if (strlen (str) < 6) return 0; /* end of header... */ while ((' ' == *str) || ('\t' == *str) || (',' == *str)) if (*str) str++; else return -1; /* bad header format */#if (!defined WIN32 && !defined _WIN32_WCE) if (strncasecmp (name, str, strlen (name)) == 0)#else if (_strnicmp (name, str, strlen (name)) == 0)#endif { char *end; end = strchr (str, ','); if (end == NULL) end = str + strlen (str); /* This is the end of the header */ if (end - beg < 2) return -1; *result = (char *) smalloc (end - beg); if (*result == NULL) return -1; sstrncpy (*result, beg + 1, end - beg - 1); sclrspace (*result); /* make sure the element does not contain more parameter */ tmp = (*end) ? (end + 1) : end; //end + 1; for (; *tmp == ' ' || *tmp == '\t'; tmp++) { } for (; *tmp == '\n' || *tmp == '\r'; tmp++) { } /* skip LWS */ *next = NULL; if (*tmp == '\0') /* end of header detected */ return 0; if (*tmp != '\t' && *tmp != ' ') /* LWS here ? */ *next = tmp; else { /* it is: skip it... */ for (; *tmp == ' ' || *tmp == '\t'; tmp++) { } if (*tmp == '\0') /* end of header detected */ return 0; *next = tmp; } } else *next = str; /* next element start here */ return 0;}/* fills the www-authenticate strucuture. *//* INPUT : char *hvalue | value of header. *//* OUTPUT: sip_t *sip | structure to save results. *//* returns -1 on error. *//* TODO: digest-challenge tken has no order preference?? verify many situations (extra SP....)*/intwww_authenticate_parse (www_authenticate_t * wwwa, char *hvalue){ char *space = NULL; char *next = NULL; space = strchr (hvalue, ' '); /* SEARCH FOR SPACE */ if (space == NULL) return -1; if (space - hvalue + 1 < 2) return -1; wwwa->auth_type = (char *) smalloc (space - hvalue + 1); if (wwwa->auth_type == NULL) return -1; sstrncpy (wwwa->auth_type, hvalue, space - hvalue); for (;;) { int parse_ok = 0; if (quoted_string_set ("realm", space, &(wwwa->realm), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (quoted_string_set ("domain", space, &(wwwa->domain), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (quoted_string_set ("nonce", space, &(wwwa->nonce), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (quoted_string_set ("opaque", space, &(wwwa->opaque), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (token_set ("stale", space, &(wwwa->stale), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (token_set ("algorithm", space, &(wwwa->algorithm), &next)) return -1; if (next == NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -