Commit 28ad8e0e authored by Guillaume Beraudo's avatar Guillaume Beraudo
Browse files

Factoring, visibility and security checks in wizard.

parent be4e74d2
/*
linphone
Copyright (C) 2011 Simon MORLAT (simon.morlat@linphone.org)
sipwizard.c
Copyright (C) 2011 Belledonne Communication, Grenoble, France
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
......@@ -30,9 +30,9 @@ typedef struct _BLReq{
ortp_thread_t th;
}BLReq;
const int XMLRPC_FAILED = -1;
const int XMLRPC_OK = 0;
const char *XMLRPC_URL = "https://www.linphone.org/wizard.php";
static const int XMLRPC_FAILED = -1;
static const int XMLRPC_OK = 0;
static const char *XMLRPC_URL = "https://www.linphone.org/wizard.php";
static void sip_wizard_init_instance(SipSetupContext *ctx){
LinphoneProxyConfig *cfg=sip_setup_context_get_proxy_config(ctx);
......@@ -40,65 +40,12 @@ static void sip_wizard_init_instance(SipSetupContext *ctx){
linphone_proxy_config_enable_register(cfg,FALSE);
}
const char ** sip_wizard_get_domains(SipSetupContext *ctx) {
static const char ** sip_wizard_get_domains(SipSetupContext *ctx) {
LinphoneProxyConfig *cfg=sip_setup_context_get_proxy_config(ctx);
const char **domains = (const char**) &cfg->reg_proxy;
return domains;
}
static SoupMessage * build_xmlrpc_check_account_request(const char *identity){
SoupMessage * msg;
msg=soup_xmlrpc_request_new(XMLRPC_URL,
"check_account",
G_TYPE_STRING, identity,
G_TYPE_INVALID);
if (!msg){
ms_error("Fail to create SoupMessage !");
}else{
SoupBuffer *sb=soup_message_body_flatten(msg->request_body);
ms_message("This is the XML-RPC request we are going to send:\n%s\n",sb->data);
soup_buffer_free(sb);
}
return msg;
}
static SoupMessage * build_xmlrpc_check_account_validated(const char *identity){
SoupMessage * msg;
msg=soup_xmlrpc_request_new(XMLRPC_URL,
"check_account_validated",
G_TYPE_STRING, identity,
G_TYPE_INVALID);
if (!msg){
ms_error("Fail to create SoupMessage !");
}else{
SoupBuffer *sb=soup_message_body_flatten(msg->request_body);
ms_message("This is the XML-RPC request we are going to send:\n%s\n",sb->data);
soup_buffer_free(sb);
}
return msg;
}
static SoupMessage * build_xmlrpc_create_account_request(const char *identity, const char *passwd, const char *email, int suscribe){
SoupMessage * msg;
msg=soup_xmlrpc_request_new(XMLRPC_URL,
"create_account",
G_TYPE_STRING, identity,
G_TYPE_STRING, passwd,
G_TYPE_STRING, email,
G_TYPE_INT, suscribe,
G_TYPE_INVALID);
if (!msg){
ms_error("Fail to create SoupMessage !");
}else{
SoupBuffer *sb=soup_message_body_flatten(msg->request_body);
ms_message("This is the XML-RPC request we are going to send:\n%s\n",sb->data);
soup_buffer_free(sb);
}
return msg;
}
static int xml_rpc_parse_response(BLReq *blreq, SoupMessage *sm){
SoupBuffer *sb;
......@@ -149,7 +96,6 @@ static void * process_xml_rpc_request(void *up){
#endif
code=soup_session_send_message(blreq->session,sm);
if (code==200){
ms_message("Got a response from server, yeah !");
xml_rpc_parse_response(blreq,sm);
}else{
ms_error("request failed, error-code=%i (%s)",code,soup_status_get_phrase(code));
......@@ -161,72 +107,78 @@ static void * process_xml_rpc_request(void *up){
return NULL;
}
int sip_wizard_account_exists(SipSetupContext *ctx, const char *uri) {
/*
* Return 1 if account already exists
* 0 if account doesn't exists
* -1 if information isn't available
*/
SoupMessage *sm;
BLReq *req=ms_new0(BLReq, 1);
req->session=soup_session_sync_new();
sm=build_xmlrpc_check_account_request(uri);
req->msg=sm;
process_xml_rpc_request(req);
if (req->status == XMLRPC_OK) {
return req->result;
} else {
static int do_simple_xmlrpc_request(SoupMessage *msg) {
int ret=-1;
BLReq *req;
if (!msg){
ms_error("Fail to create SoupMessage !");
return -1;
}else{
SoupBuffer *sb=soup_message_body_flatten(msg->request_body);
ms_message("This is the XML-RPC request we are going to send:\n%s\n",sb->data);
soup_buffer_free(sb);
}
req=ms_new0(BLReq, 1);
req->session=soup_session_sync_new();
req->msg=msg;
process_xml_rpc_request(req);
if (req->status == XMLRPC_OK) {
ret=req->result;
}
// Freeing allocated structures lead to a crash (why?)
//g_free(req->session);
//g_free(msg);
ms_free(req);
return ret;
}
int sip_wizard_account_validated(SipSetupContext *ctx, const char *uri) {
/*
* Return 1 if account already exists
* 0 if account doesn't exists
* -1 if information isn't available
*/
SoupMessage *sm;
BLReq *req=ms_new0(BLReq, 1);
req->session=soup_session_sync_new();
sm=build_xmlrpc_check_account_validated(uri);
req->msg=sm;
process_xml_rpc_request(req);
if (req->status == XMLRPC_OK) {
return req->result;
} else {
return -1;
}
/*
* Return 1 if account already exists
* 0 if account doesn't exists
* -1 if information isn't available
*/
static int sip_wizard_account_exists(SipSetupContext *ctx, const char *identity) {
SoupMessage *msg=soup_xmlrpc_request_new(XMLRPC_URL,
"check_account",
G_TYPE_STRING, identity,
G_TYPE_INVALID);
return do_simple_xmlrpc_request(msg);
}
int sip_wizard_create_account(SipSetupContext *ctx, const char *uri, const char *passwd, const char *email, int suscribe) {
/*
* Return 0 if account successfully created
* Else return -1
*/
SoupMessage *sm;
BLReq *req=ms_new0(BLReq, 1);
req->session=soup_session_sync_new();
sm=build_xmlrpc_create_account_request(uri, passwd, email, suscribe);
req->msg=sm;
process_xml_rpc_request(req);
if (req->status == XMLRPC_OK) {
return req->result;
} else {
return -1;
}
static int sip_wizard_account_validated(SipSetupContext *ctx, const char *identity) {
SoupMessage *msg=soup_xmlrpc_request_new(XMLRPC_URL,
"check_account_validated",
G_TYPE_STRING, identity,
G_TYPE_INVALID);
return do_simple_xmlrpc_request(msg);
}
static int sip_wizard_create_account(SipSetupContext *ctx, const char *identity, const char *passwd, const char *email, int suscribe) {
SoupMessage *msg=soup_xmlrpc_request_new(XMLRPC_URL,
"create_account",
G_TYPE_STRING, identity,
G_TYPE_STRING, passwd,
G_TYPE_STRING, email,
G_TYPE_INT, suscribe,
G_TYPE_INVALID);
return do_simple_xmlrpc_request(msg);
}
static void guess_display_name(LinphoneAddress *from){
char *dn=(char*)ms_malloc(strlen(linphone_address_get_username(from))+3);
const char *username=linphone_address_get_username(from);
char *dn=(char*)ms_malloc(strlen(username)+1);
const char *it;
char *wptr=dn;
bool_t begin=TRUE;
bool_t surname=0;
for(it=linphone_address_get_username(from);*it!='\0';++it){
bool_t surname=FALSE;
for(it=username;*it!='\0';++it){
if (begin){
*wptr=toupper(*it);
begin=FALSE;
......@@ -235,9 +187,12 @@ static void guess_display_name(LinphoneAddress *from){
*wptr=' ';
begin=TRUE;
surname=TRUE;
}else *wptr=*it;
}else {
*wptr=*it;
}
wptr++;
}
*wptr='\0';
linphone_address_set_display_name(from,dn);
ms_free(dn);
}
......
......@@ -20,16 +20,16 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "linphone.h"
#include <glib.h>
#include <glib/gprintf.h>
LinphoneAccountCreator *linphone_gtk_assistant_get_creator(GtkWidget*w);
static LinphoneAccountCreator *linphone_gtk_assistant_get_creator(GtkWidget*w);
const int PASSWORD_MIN_SIZE = 6;
const int LOGIN_MIN_SIZE = 4;
int is_username_available = 0;
int is_email_correct = 0;
int is_password_correct = 0;
static const int PASSWORD_MIN_SIZE = 6;
static const int LOGIN_MIN_SIZE = 4;
static int is_username_available = 0;
static int is_email_correct = 0;
static int is_password_correct = 0;
GdkPixbuf *ok;
GdkPixbuf *notok;
static GdkPixbuf *ok;
static GdkPixbuf *notok;
static GtkWidget *create_intro(){
GtkWidget *vbox=gtk_vbox_new(FALSE,2);
......@@ -75,7 +75,7 @@ static void account_informations_changed(GtkEntry *entry, GtkWidget *w) {
const gchar *needle = "@";
if (entry == username && g_strrstr(gtk_entry_get_text(username), needle) != NULL) {
gtk_entry_set_text(domain, g_strrstr(gtk_entry_get_text(username), "@")+1);
gtk_entry_set_text(domain, g_strrstr(gtk_entry_get_text(username), needle)+1);
}
gtk_assistant_set_page_complete(GTK_ASSISTANT(assistant),w,
......@@ -92,7 +92,7 @@ static void linphone_account_informations_changed(GtkEntry *entry, GtkWidget *w)
static GtkWidget *create_linphone_account_informations_page() {
GtkWidget *vbox=gtk_table_new(3, 2, TRUE);
GtkWidget *label=gtk_label_new(_("Enter your linphone.org's username"));
GtkWidget *label=gtk_label_new(_("Enter your linphone.org username"));
GdkColor color;
gdk_color_parse ("red", &color);
......@@ -129,12 +129,12 @@ static GtkWidget *create_account_informations_page() {
gtk_widget_modify_fg(labelEmpty, GTK_STATE_NORMAL, &color);
GtkWidget *labelUsername=gtk_label_new(_("Identity:"));
GtkWidget *labelUsernameExemple=gtk_label_new(_("exemple: user@sip.linphone.org"));
GtkWidget *labelUsernameExemple=gtk_label_new(_("example: user@sip.linphone.org"));
GtkWidget *labelPassword=gtk_label_new(_("Password:"));
GtkWidget *entryPassword=gtk_entry_new();
gtk_entry_set_visibility(GTK_ENTRY(entryPassword), FALSE);
GtkWidget *labelDomain=gtk_label_new(_("Proxy:"));
GtkWidget *labelDomainExemple=gtk_label_new(_("exemple: sip.linphone.org"));
GtkWidget *labelDomainExemple=gtk_label_new(_("example: sip.linphone.org"));
GtkWidget *labelRoute=gtk_label_new(_("Route (optional):"));
GtkWidget *entryUsername=gtk_entry_new();
GtkWidget *entryDomain=gtk_entry_new();
......@@ -179,7 +179,7 @@ static int create_account(GtkWidget *page) {
if (res) {
if (!g_regex_match_simple("^sip:[a-zA-Z]+[a-zA-Z0-9.\\-_]{2,}@sip.linphone.org$",creator->username, 0, 0)) {
gchar identity[128];
g_sprintf(identity, "sip:%s@sip.linphone.org", creator->username);
g_snprintf(identity, sizeof(identity), "sip:%s@sip.linphone.org", creator->username);
linphone_account_creator_set_username(creator, identity);
linphone_account_creator_set_domain(creator, "sip:sip.linphone.org");
}
......@@ -354,7 +354,7 @@ static GtkWidget *wait_for_activation() {
return vbox;
}
int is_account_validated(GtkWidget *page) {
static int is_account_validated(GtkWidget *page) {
LinphoneAccountCreator *creator=linphone_gtk_assistant_get_creator(gtk_widget_get_toplevel(page));
return linphone_account_creator_test_validation(creator);
}
......@@ -383,8 +383,9 @@ static void linphone_gtk_assistant_prepare(GtkWidget *assistant, GtkWidget *page
const gchar *needle = "@";
username = g_strndup(username, (g_strrstr(username, needle) - username));
gchar domain[128];
g_sprintf(domain, "\"%s\"", creator->domain + 4);
g_snprintf(domain, sizeof(domain), "\"%s\"", creator->domain + 4);
LinphoneAuthInfo *info=linphone_auth_info_new(username, username, creator->password, NULL, domain);
g_free(username);
linphone_core_add_auth_info(linphone_gtk_get_core(),info);
if (linphone_core_add_proxy_config(linphone_gtk_get_core(),cfg)==-1)
......@@ -417,7 +418,7 @@ static int linphone_gtk_assistant_forward(int curpage, gpointer data){
LinphoneAccountCreator *c=linphone_gtk_assistant_get_creator(w);
if (!g_regex_match_simple("^sip:[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\\.-][a-z0-9]+)*)+\\.[a-z]{2,}$", gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"username"))), 0, 0)) {
gchar identity[128];
g_sprintf(identity, "sip:%s", gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"username"))));
g_snprintf(identity, sizeof(identity), "sip:%s", gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"username"))));
linphone_account_creator_set_username(c, identity);
} else {
linphone_account_creator_set_username(c, gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"username"))));
......@@ -425,7 +426,7 @@ static int linphone_gtk_assistant_forward(int curpage, gpointer data){
if (!g_regex_match_simple("^sip:([a-z0-9]+([\\.-][a-z0-9]+)*)+\\.[a-z]{2,}$", gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"domain"))), 0, 0)) {
gchar proxy[128];
g_sprintf(proxy, "sip:%s", gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"domain"))));
g_snprintf(proxy, sizeof(proxy), "sip:%s", gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"domain"))));
linphone_account_creator_set_domain(c, proxy);
} else {
linphone_account_creator_set_domain(c, gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"domain"))));
......@@ -437,7 +438,7 @@ static int linphone_gtk_assistant_forward(int curpage, gpointer data){
else if (curpage == 3) { // Linphone Account's informations entered
LinphoneAccountCreator *c=linphone_gtk_assistant_get_creator(w);
gchar identity[128];
g_sprintf(identity, "sip:%s@sip.linphone.org", gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"username"))));
g_snprintf(identity, sizeof(identity), "sip:%s@sip.linphone.org", gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"username"))));
linphone_account_creator_set_username(c, identity);
linphone_account_creator_set_domain(c, "sip:sip.linphone.org");
linphone_account_creator_set_password(c,gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"password"))));
......@@ -482,7 +483,7 @@ static LinphoneAccountCreator * linphone_gtk_assistant_init(GtkWidget *w){
return NULL;
}
LinphoneAccountCreator *linphone_gtk_assistant_get_creator(GtkWidget*w){
static LinphoneAccountCreator *linphone_gtk_assistant_get_creator(GtkWidget*w){
return (LinphoneAccountCreator*)g_object_get_data(G_OBJECT(w),"creator");
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment