examples/lm-change-password.c
author hallski <hallski>
Mon, 08 Nov 2004 13:21:52 +0000
changeset 104 f43fde0f89d4
parent 99 81e9d035de02
child 107 aa7e8b466ca8
permissions -rw-r--r--
2004-11-08 Mikael Hallendal <micke@imendio.com> * examples/lm-change-password.c: Added SSL support * examples/lm-register.c: Added SSL support
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
85
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
     1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
     2
/*
99
81e9d035de02 2004-10-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 85
diff changeset
     3
 * Copyright (C) 2004 Imendio AB
85
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
     4
 *
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
     5
 * This library is free software; you can redistribute it and/or
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
     6
 * modify it under the terms of the GNU Library General Public
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
     7
 * License as published by the Free Software Foundation; either
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
     8
 * version 2 of the License, or (at your option) any later version.
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
     9
 *
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    10
 * This library is distributed in the hope that it will be useful,
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    13
 * Library General Public License for more details.
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    14
 *
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    15
 * You should have received a copy of the GNU Library General Public
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    16
 * License along with this library; if not, write to the
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    18
 * Boston, MA 02111-1307, USA.
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    19
 */
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    20
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    21
/* 
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    22
 * Small tool to change password on an account
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    23
 */
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    24
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    25
#include <loudmouth/loudmouth.h>
104
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    26
#include <string.h>
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    27
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    28
static LmSSLResponse 
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    29
ssl_func (LmSSL        *ssl,
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    30
	  LmSSLStatus   status,
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    31
	  gpointer      user_data)
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    32
{
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    33
	return LM_SSL_RESPONSE_CONTINUE;
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    34
}
85
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    35
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    36
static void
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    37
print_usage (const gchar *exec_name)
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    38
{
104
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    39
	g_print ("Usage: %s <server> <username> <oldpassword> <newpassword> [--ssl]\n",
85
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    40
		 exec_name);
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    41
}
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    42
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    43
int 
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    44
main (int argc, char **argv)
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    45
{
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    46
	LmConnection  *connection;
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    47
	const gchar   *server;
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    48
	const gchar   *username;
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    49
	const gchar   *old_pass;
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    50
	const gchar   *new_pass;
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    51
	GError        *error = NULL;
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    52
	LmMessage     *m;
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    53
	LmMessageNode *query;
104
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    54
	gboolean       use_ssl = FALSE;
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    55
	
85
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    56
	if (argc < 5) {
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    57
		print_usage (argv[0]);
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    58
		return -1;
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    59
	}
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    60
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    61
	server = argv[1];
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    62
	username = argv[2];
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    63
	old_pass = argv[3];
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    64
	new_pass = argv[4];
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    65
104
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    66
	if (argc >= 5) {
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    67
		int i;
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    68
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    69
		for (i = 5; i < argc; ++i) {
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    70
			if (strcmp (argv[i], "-s") == 0 ||
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    71
			    strcmp (argv[i], "--ssl") == 0) {
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    72
				use_ssl = TRUE;
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    73
				break;
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    74
			}
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    75
		}
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    76
	}
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    77
85
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    78
	connection = lm_connection_new (server);
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    79
104
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    80
	if (use_ssl) {
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    81
		LmSSL *ssl;
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    82
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    83
		if (!lm_ssl_is_supported ()) {
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    84
			g_print ("This loudmouth installation doesn't support SSL\n");
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    85
			return 1;
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    86
		}
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    87
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    88
		g_print ("Setting ssl\n");
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    89
		ssl = lm_ssl_new (NULL, ssl_func, NULL, NULL);
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    90
		lm_connection_set_ssl (connection, ssl);
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    91
		lm_ssl_unref (ssl);
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    92
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    93
		lm_connection_set_port (connection,
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    94
					LM_CONNECTION_DEFAULT_PORT_SSL);
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    95
	}
f43fde0f89d4 2004-11-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    96
85
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    97
	if (!lm_connection_open_and_block (connection, &error)) {
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    98
		g_error ("Failed to open: %s\n", error->message);
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
    99
	}
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   100
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   101
	if (!lm_connection_authenticate_and_block (connection,
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   102
						   username, old_pass, 
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   103
						   "Password changer",
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   104
						   &error)) {
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   105
		g_error ("Failed to authenticate: %s\n", error->message);
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   106
	}
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   107
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   108
	m = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, 
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   109
					 LM_MESSAGE_SUB_TYPE_SET);
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   110
	
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   111
	query = lm_message_node_add_child (m->node, "query", NULL);
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   112
	
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   113
	lm_message_node_set_attributes (query, "xmlns", "jabber:iq:register",
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   114
					NULL);
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   115
	lm_message_node_add_child (query, "username", username);
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   116
	lm_message_node_add_child (query, "password", new_pass);
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   117
	
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   118
	if (!lm_connection_send (connection, m, &error)) {
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   119
		g_error ("Failed to send: %s\n", error->message);
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   120
	}
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   121
	
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   122
	lm_connection_close (connection, NULL);
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   123
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   124
	return 0;
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   125
}
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   126
cc434baf49cf Added a few files I forget when syncing with svn
hallski <hallski>
parents:
diff changeset
   127