apps_utils.h 3.67 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1
#ifndef APPS_UTILS_H /** Defined when apps_utils.h has been included. */
Pekka Pessi's avatar
Pekka Pessi committed
2
#define APPS_UTILS_H
Pekka Pessi's avatar
Pekka Pessi committed
3 4 5

/**
 * @nofile apps_utils.h
6 7
 * @brief
 *
8
 * Copyright (C) 2005 Nokia Corporation.
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Written by Pekka Pessi <pekka -dot pessi -at- nokia -dot- com>
 *
 * @STARTLGPL@
 * 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., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 * @ENDLGPL@
28
 *
Pekka Pessi's avatar
Pekka Pessi committed
29
 * @date Created: Thu Apr  8 15:55:15 2004 ppessi
30
 *
Pekka Pessi's avatar
Pekka Pessi committed
31 32
 */

33 34
SOFIA_BEGIN_DECLS

Pekka Pessi's avatar
Pekka Pessi committed
35 36 37 38 39 40
static inline
int proxy_authenticate(context_t *c,
		       nta_outgoing_t *oreq,
		       sip_t const *sip,
		       nta_response_f response_function)
{
41
  if (sip && sip->sip_status->st_status == 407 &&
Pekka Pessi's avatar
Pekka Pessi committed
42
      sip->sip_proxy_authenticate &&
43
      c->c_proxy_auth_retries++ < 3 &&
Pekka Pessi's avatar
Pekka Pessi committed
44 45 46 47 48 49
      c->c_proxy && c->c_proxy->url_user && c->c_proxy->url_password) {
    url_t *u = c->c_proxy;
    msg_t *rmsg = nta_outgoing_getrequest(oreq);
    sip_t *rsip = sip_object(rmsg);

    if (auc_challenge(&c->c_proxy_auth, c->c_home, sip->sip_proxy_authenticate,
50
		      sip_proxy_authorization_class) >= 0
Pekka Pessi's avatar
Pekka Pessi committed
51
	&&
52
	auc_all_credentials(&c->c_proxy_auth, NULL, NULL,
Pekka Pessi's avatar
Pekka Pessi committed
53
			    u->url_user, u->url_password) > 0
54
	&&
Pekka Pessi's avatar
Pekka Pessi committed
55 56 57 58 59 60 61
	auc_authorization(&c->c_proxy_auth, rmsg, (msg_pub_t *)rsip,
			  rsip->sip_request->rq_method_name,
			  rsip->sip_request->rq_url,
			  rsip->sip_payload) > 0) {
      nta_outgoing_destroy(c->c_orq);
      sip_header_remove(rmsg, rsip, (sip_header_t *)rsip->sip_via);
      nta_msg_request_complete(rmsg, c->c_leg, 0, NULL, NULL);
62
      c->c_orq = nta_outgoing_tmcreate(c->c_agent, response_function, c, NULL,
Pekka Pessi's avatar
Pekka Pessi committed
63 64 65 66 67 68
				       rmsg, TAG_END());
      return 1;
    }
  }

  return 0;
69
}
Pekka Pessi's avatar
Pekka Pessi committed
70 71 72 73 74 75 76

static inline
int server_authenticate(context_t *c,
			nta_outgoing_t *oreq,
			sip_t const *sip,
			nta_response_f response_function)
{
77
  if (sip && sip->sip_status->st_status == 401 &&
Pekka Pessi's avatar
Pekka Pessi committed
78
      sip->sip_www_authenticate &&
79
      c->c_auth_retries++ < 3 &&
Pekka Pessi's avatar
Pekka Pessi committed
80 81 82 83 84
      c->c_password && c->c_username) {
    msg_t *rmsg = nta_outgoing_getrequest(oreq);
    sip_t *rsip = sip_object(rmsg);

    if (auc_challenge(&c->c_auth, c->c_home, sip->sip_www_authenticate,
85
		      sip_authorization_class) >= 0
Pekka Pessi's avatar
Pekka Pessi committed
86 87
	&&
	auc_all_credentials(&c->c_auth, NULL, NULL, c->c_username, c->c_password) > 0
88
	&&
Pekka Pessi's avatar
Pekka Pessi committed
89 90 91 92 93 94 95
	auc_authorization(&c->c_auth, rmsg, (msg_pub_t *)rsip,
			  rsip->sip_request->rq_method_name,
			  rsip->sip_request->rq_url,
			  rsip->sip_payload) > 0) {
      nta_outgoing_destroy(c->c_orq);
      sip_header_remove(rmsg, rsip, (sip_header_t *)rsip->sip_via);
      nta_msg_request_complete(rmsg, c->c_leg, 0, NULL, NULL);
96
      c->c_orq = nta_outgoing_tmcreate(c->c_agent, response_function, c, NULL,
Pekka Pessi's avatar
Pekka Pessi committed
97 98 99 100 101 102
				       rmsg, TAG_END());
      return 1;
    }
  }

  return 0;
103
}
Pekka Pessi's avatar
Pekka Pessi committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

static inline
int tag_from_header(nta_agent_t *nta,
		    su_home_t *home,
		    sip_from_t *f)
{
  char *env = getenv("SIPTAG");
  char *t = (void *)nta_agent_newtag(home, NULL, nta);
  int retval;

  if (env) {
    char *t1 = su_sprintf(home, "tag=%s-%s", env, t);
    su_free(home, t);
    t = t1;
  }

  retval = sip_from_tag(home, f, t);

  su_free(home, t);

  return retval;
}

127 128
SOFIA_END_DECLS

Pekka Pessi's avatar
Pekka Pessi committed
129
#endif /* !defined APPS_UTILS_H */