📄 hdr_authorization.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>#include "msg.h"intauthorization_init (authorization_t ** dest){ *dest = (authorization_t *) smalloc (sizeof (authorization_t)); if (*dest == NULL) return -1; (*dest)->auth_type = NULL; (*dest)->username = NULL; (*dest)->realm = NULL; (*dest)->nonce = NULL; (*dest)->uri = NULL; (*dest)->response = NULL; (*dest)->digest = NULL; /* DO NOT USE IT IN AUTHORIZATION_T HEADER?? */ (*dest)->algorithm = NULL; /* optionnal, default is "md5" */ (*dest)->cnonce = NULL; /* optionnal */ (*dest)->opaque = NULL; /* optionnal */ (*dest)->message_qop = NULL; /* optionnal */ (*dest)->nonce_count = NULL; /* optionnal */ (*dest)->auth_param = NULL; /* for other headers --NOT IMPLEMENTED-- */ 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_setauthorization (sip_t * sip, char *hvalue){ authorization_t *authorization; int i; if (sip == NULL || sip->authorizations == NULL) return -1; i = authorization_init (&authorization); if (i != 0) return -1; i = authorization_parse (authorization, hvalue); if (i != 0) { authorization_free (authorization); sfree (authorization); return -1; }#ifdef USE_TMP_BUFFER sip->message_property = 2;#endif list_add (sip->authorizations, authorization, -1); return 0;}/* fills the www-authenticate structure. *//* 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....)*/intauthorization_parse (authorization_t * auth, char *hvalue){ char *space = NULL; char *next = NULL; space = strchr (hvalue, ' '); /* SEARCH FOR SPACE */ if (space == NULL) return -1; if (space - hvalue < 1) return -1; auth->auth_type = (char *) smalloc (space - hvalue + 1); sstrncpy (auth->auth_type, hvalue, space - hvalue); for (;;) { int parse_ok = 0; if (quoted_string_set ("username", space, &(auth->username), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (quoted_string_set ("realm", space, &(auth->realm), &next)) return -1; if (next == NULL) return 0; else if (next != space) { space = next; parse_ok++; } if (quoted_string_set ("nonce", space, &(auth->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 ("uri", space, &(auth->uri), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (quoted_string_set ("response", space, &(auth->response), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (quoted_string_set ("digest", space, &(auth->digest), &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, &(auth->algorithm), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (quoted_string_set ("cnonce", space, &(auth->cnonce), &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, &(auth->opaque), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (token_set ("qop", space, &(auth->message_qop), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (token_set ("nc", space, &(auth->nonce_count), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } /* nothing was recognized: here, we should handle a list of unknown tokens where: token1 = ( token2 | quoted_text ) */ /* TODO */ if (0 == parse_ok) { char *quote1, *quote2, *tmp; /* CAUTION */ /* parameter not understood!!! I'm too lazy to handle IT */ /* let's simply bypass it */ if (strlen (space) < 1) return 0; tmp = strchr (space + 1, ','); if (tmp == NULL) /* it was the last header */ return 0; quote1 = quote_find (space); if ((quote1 != NULL) && (quote1 < tmp)) /* this may be a quoted string! */ { quote2 = quote_find (quote1 + 1); if (quote2 == NULL) return -1; /* bad header format... */ if (tmp < quote2) /* the comma is inside the quotes! */ space = strchr (quote2, ','); else space = tmp; if (space == NULL) /* it was the last header */ return 0; } else space = tmp; /* continue parsing... */ } } return 0; /* ok */}/* returns the authorization header. *//* INPUT : sip_t *sip | sip message. *//* returns null on error. */intmsg_getauthorization (sip_t * sip, int pos, authorization_t ** dest){ authorization_t *authorization; *dest = NULL; if (list_size (sip->authorizations) <= pos) return -1; /* does not exist */ authorization = (authorization_t *) list_get (sip->authorizations, pos); *dest = authorization; return pos;}char *authorization_getauth_type (authorization_t * authorization){ return authorization->auth_type;}voidauthorization_setauth_type (authorization_t * authorization, char *auth_type){ authorization->auth_type = (char *) auth_type;}char *authorization_getusername (authorization_t * authorization){ return authorization->username;}voidauthorization_setusername (authorization_t * authorization, char *username){ authorization->username = (char *) username;}char *authorization_getrealm (authorization_t * authorization){ return authorization->realm;}voidauthorization_setrealm (authorization_t * authorization, char *realm){ authorization->realm = (char *) realm;}char *authorization_getnonce (authorization_t * authorization){ return authorization->nonce;}voidauthorization_setnonce (authorization_t * authorization, char *nonce){ authorization->nonce = (char *) nonce;}char *authorization_geturi (authorization_t * authorization){ return authorization->uri;}voidauthorization_seturi (authorization_t * authorization, char *uri){ authorization->uri = (char *) uri;}char *authorization_getresponse (authorization_t * authorization){ return authorization->response;}voidauthorization_setresponse (authorization_t * authorization, char *response){ authorization->response = (char *) response;}char *authorization_getdigest (authorization_t * authorization){ return authorization->digest;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -