auth/common.c

Go to the documentation of this file.
00001 /*
00002  * $Id: common.c 5596 2009-02-12 18:10:24Z miconda $
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  * History:
00023  * -------
00024  * 2003-03-15: In case of HDR_PROXYAUTH we always extract realm from From,
00025  *             even for REGISTERS
00026  * 2003-09-11: updated to new build_lump_rpl() interface (bogdan)
00027  * 2003-11-11: build_lump_rpl() removed, add_lump_rpl() has flags (bogdan)
00028  */
00029 
00030 /*!
00031  * \file
00032  * \brief Digest Authentication Module
00033  * \ingroup auth
00034  * - Module: \ref auth
00035  */
00036 
00037 #include <string.h>
00038 #include "../../dprint.h"
00039 #include "../../parser/parse_from.h"
00040 #include "../../parser/parse_to.h"
00041 #include "../../parser/parse_uri.h"
00042 #include "../../data_lump_rpl.h"
00043 #include "auth_mod.h"
00044 #include "common.h"
00045 
00046 
00047 /*!
00048  * \brief Return parsed To or From, host part of the parsed uri is realm
00049  * \param _m SIP message
00050  * \param _hftype header field type
00051  * \param _u SIP URI
00052  * \return 0 on success, negative on failure
00053  */
00054 int get_realm(struct sip_msg* _m, hdr_types_t _hftype, struct sip_uri** _u)
00055 {
00056 
00057    if(_u==NULL)
00058       return -1;
00059    if ((REQ_LINE(_m).method.len == 8) 
00060        && !memcmp(REQ_LINE(_m).method.s, "REGISTER", 8) 
00061        && (_hftype == HDR_AUTHORIZATION_T)
00062       ) {
00063       if (!_m->to && ((parse_headers(_m, HDR_TO_F, 0)==-1) || (!_m->to))) {
00064          LM_ERR("failed to parse TO headers\n");
00065          return -1;
00066       }
00067       
00068       /* Body of To header field is parsed automatically */
00069       if((*_u = parse_to_uri(_m))==NULL)
00070          return -1;
00071    } else {
00072       if (parse_from_header(_m) < 0) {
00073          LM_ERR("failed to parse FROM headers\n");
00074          return -2;
00075       }
00076       if((*_u = parse_from_uri(_m))==NULL)
00077          return -1;
00078    }
00079    
00080    return 0;
00081 }
00082 
00083 
00084 /*!
00085  * \brief Create a response with given code and reason phrase
00086  *
00087  * Create a response with given code and reason phrase
00088  * Optionally add new headers specified in _hdr
00089  * \param _m SIP message
00090  * \param _code response code
00091  * \param _reason reason string
00092  * \param _hdr header to add
00093  * \param _hdr_len header length
00094  * \return 1 if reply could be sended, -1 on failure
00095  */
00096 int send_resp(struct sip_msg* _m, int _code, str* _reason,
00097                char* _hdr, int _hdr_len)
00098 {
00099    /* Add new headers if there are any */
00100    if ((_hdr) && (_hdr_len)) {
00101       if (add_lump_rpl( _m, _hdr, _hdr_len, LUMP_RPL_HDR)==0) {
00102          LM_ERR("unable to append hdr\n");
00103          return -1;
00104       }
00105    }
00106 
00107    return slb.send_reply(_m, _code, _reason);
00108 }

Generated on Thu May 17 12:00:25 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6