📄 plugin.c
字号:
/* The SUBSCRIBE plugin is a GPL plugin for partysip. Copyright (C) 2002 2003 Aymeric MOIZARD - <jack@atosc.org> The SUBSCRIBE plugin is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The SUBSCRIBE plugin 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 General Public License for more details. You should have received a copy of the GNU General Public License along with Foobar; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#include <partysip/partysip.h>#include "plugin.h"#include <osipparser2/osip_port.h>#include <osipparser2/osip_message.h>#include <ppl/ppl_md5.h>#include <sql_module.h>plugin_ctx_t *plugin_context = NULL;extern psp_plugin_t PPL_DECLARE_DATA mysqldb_subscribe_plugin;extern char name_config[50];intplugin_ctx_init (){ int i; plugin_context = (plugin_ctx_t *) malloc (sizeof (plugin_ctx_t)); if (plugin_context == NULL) return -1; /* my_thread_init(); */ /* do I need that? (only one thread access SQL elements. */ memset(plugin_context, '\0', sizeof(plugin_context)); i = module_subscribe_initialize(); if (i==0) return 0; plugin_ctx_free(); plugin_context = NULL; return -1;}voidplugin_ctx_free (){ /* my_thread_end(); */ if (plugin_context==NULL) return; if (plugin_context->connected==1) mysql_close(&(plugin_context->mysql_conn)); plugin_context->connected = 0; free (plugin_context); plugin_context = NULL;}MYSQL_RES *plugin_uinfo_find_user_by_login(char *username){ MYSQL_RES *result; unsigned int num_fields; unsigned int num_rows; int i; /* int msgid; MYSQLMessage *entry; int mysql_error; struct timeval timeout = { 4 , 0 }; */ char sql_request[512]; sprintf(sql_request, "SELECT userid , username , domain , ha1 , sipurl , email , status , creation_time , update_time FROM psp_users WHERE 1 AND username LIKE \'%s\' LIMIT 0, 2", username); i = mysql_query(&(plugin_context->mysql_conn), sql_request); if (i!=0) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "%s plugin: error in mysql_send_query\n", PLUGIN_NAME)); return NULL; } result = mysql_store_result(&(plugin_context->mysql_conn)); if (result) /* there are rows */ { num_fields = mysql_num_fields(result); /* retrieve rows, then call mysql_free_result(result) */ } else /* mysql_store_result() returned nothing; should it have? */ { if (mysql_errno(&(plugin_context->mysql_conn))) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "%s plugin: error in mysql_store_result (%s)\n", PLUGIN_NAME, mysql_error(&(plugin_context->mysql_conn)))); return NULL; } else if (mysql_field_count(&(plugin_context->mysql_conn)) == 0) { /* query does not return data*/ /* (it was not a SELECT) */ num_rows = mysql_affected_rows(&(plugin_context->mysql_conn)); return NULL; } } return result;}intplugin_validate_credential_for_user (MYSQL_RES * user_result, osip_proxy_authorization_t * p_auth, char *method){ /* find the pending_auth element */ char *response; char *nonce; char *opaque; char *realm; /* char *qop; */ char *uri; MYSQL_ROW row; unsigned long *lengths; char *ha1; char *username; mysql_data_seek(user_result, 0); row = mysql_fetch_row(user_result); lengths = mysql_fetch_lengths(user_result); username = malloc(lengths[1]+1); if (username==NULL) return -1; ha1 = malloc(lengths[3]+1); if (ha1==NULL) { free(username); return -1; } sprintf(username, "%.*s", (int) lengths[1], row[1] ? row[1] : "NULL"); sprintf(ha1, "%.*s", (int) lengths[3], row[3] ? row[3] : "NULL"); OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO2, NULL, "%s plugin: get value of field->name! %s %s\n", PLUGIN_NAME, username, ha1)); nonce = osip_proxy_authorization_get_nonce (p_auth); opaque = osip_proxy_authorization_get_opaque (p_auth); if (opaque == NULL || nonce == NULL) return -1; realm = osip_proxy_authorization_get_realm (p_auth); if (realm == NULL) return -1; response = osip_proxy_authorization_get_response (p_auth); if (response == NULL) return -1; uri = osip_proxy_authorization_get_uri (p_auth); if (uri == NULL) return -1; { /* verify credential */ /* char *pszAlg = "MD5"; */ /* char *pszUser = user->login; */ /* char *pszRealm = sgetcopy_without_string (realm); */ /* char *pszPass = user->passwd; */ char *pszNonce = osip_strdup_without_quote (nonce); char *pszCNonce = NULL; char *szNonceCount = NULL; /* used with algo=md5-sess (not implemented) */ char *pszMethod = method; char *pszURI = osip_strdup_without_quote (uri); char *pszQop = NULL; char *pszResponse = osip_strdup_without_quote (response); HASHHEX HA1; HASHHEX HA2 = ""; HASHHEX Response; /* if (qop!=NULL) pszQop = sgetcopy_without_quote(qop); */ /* A1 = unq(username : unq(realm) : passwd ) */ /* ppl_md5_DigestCalcHA1 (pszAlg, pszUser, pszRealm, pszPass, pszNonce, pszCNonce, HA1); */ osip_strncpy(HA1, ha1, strlen(ha1)); ppl_md5_DigestCalcResponse (HA1, pszNonce, szNonceCount, pszCNonce, pszQop, pszMethod, pszURI, HA2, Response); /* sfree (pszRealm); */ free (pszNonce); free (pszURI); free (pszQop); OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "%s plugin: authentication response:!\nlocal:%s remote:%s\n", PLUGIN_NAME, pszResponse, Response)); if (0 == strcmp (pszResponse, Response)) { free (pszResponse); return 0; } free (pszResponse); } return -1;}MYSQL_RES *plugin_ctx_find_private_user (osip_proxy_authorization_t * p_auth){ MYSQL_RES *user_result; char *realm = psp_config_get_element ("serverrealm"); char *username; char *qusername; if (0 != strcmp (p_auth->realm, realm)) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "%s plugin: authentication header belongs to another proxy!\nlocal:%s remote:%s\n", PLUGIN_NAME, realm, p_auth->uri)); return NULL; /* this header is not for me */ } /* search for a user context */ qusername = osip_proxy_authorization_get_username (p_auth); if (qusername == NULL) return NULL; username = osip_strdup_without_quote (qusername); user_result = plugin_uinfo_find_user_by_login (username); free (username); if (user_result != NULL) { return user_result; } return NULL;}/* HOOK METHODS *//* This method returns: -2 if plugin consider this request should be totally discarded! -1 on error 0 nothing has been done 1 things has been done on psp_req element*/intcb_plugin_validate_subscription (psp_request_t * psp_req){ osip_generic_param_t *tag_to; osip_message_t *request; osip_header_t *event; osip_header_t *expires; osip_accept_t *accept; int pos, i; char eventp[256]; memset(eventp, '\0', 256); osip_strncpy(eventp, "presence", 8); request = psp_request_get_request(psp_req); OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO2, NULL, "%s plugin: Processing authentication in request!\n", PLUGIN_NAME)); if (!MSG_IS_SUBSCRIBE (request)) { /* bug?? */ psp_request_set_state (psp_req, PSP_CONTINUE); return 0; } if (0 != psp_core_is_responsible_for_this_domain (request-> req_uri)) { /* this module only handle SUBSCRIBE to local users */ psp_request_set_state (psp_req, PSP_CONTINUE); return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -