25 #include <loudmouth/loudmouth.h> |
25 #include <loudmouth/loudmouth.h> |
26 #include <string.h> |
26 #include <string.h> |
27 |
27 |
28 static LmSSLResponse |
28 static LmSSLResponse |
29 ssl_func (LmSSL *ssl, |
29 ssl_func (LmSSL *ssl, |
30 LmSSLStatus status, |
30 LmSSLStatus status, |
31 gpointer user_data) |
31 gpointer user_data) |
32 { |
32 { |
33 return LM_SSL_RESPONSE_CONTINUE; |
33 return LM_SSL_RESPONSE_CONTINUE; |
34 } |
34 } |
35 |
35 |
36 static void |
36 static void |
37 print_usage (const gchar *exec_name) |
37 print_usage (const gchar *exec_name) |
38 { |
38 { |
39 g_print ("Usage: %s <server> <username> <oldpassword> <newpassword> [--ssl] [--host <host>]\n", |
39 g_print ("Usage: %s <server> <username> <oldpassword> <newpassword> [--ssl] [--host <host>]\n", |
40 exec_name); |
40 exec_name); |
41 } |
41 } |
42 |
42 |
43 int |
43 int |
44 main (int argc, char **argv) |
44 main (int argc, char **argv) |
45 { |
45 { |
46 LmConnection *connection; |
46 LmConnection *connection; |
47 const gchar *server; |
47 const gchar *server; |
48 const gchar *username; |
48 const gchar *username; |
49 const gchar *old_pass; |
49 const gchar *old_pass; |
50 const gchar *new_pass; |
50 const gchar *new_pass; |
51 const gchar *host; |
51 const gchar *host; |
52 GError *error = NULL; |
52 GError *error = NULL; |
53 LmMessage *m; |
53 LmMessage *m; |
54 LmMessage *reply; |
54 LmMessage *reply; |
55 LmMessageNode *query; |
55 LmMessageNode *query; |
56 gboolean use_ssl = FALSE; |
56 gboolean use_ssl = FALSE; |
57 |
57 |
58 |
58 |
59 if (argc < 5) { |
59 if (argc < 5) { |
60 print_usage (argv[0]); |
60 print_usage (argv[0]); |
61 return -1; |
61 return -1; |
62 } |
62 } |
63 |
63 |
64 server = argv[1]; |
64 server = argv[1]; |
65 username = argv[2]; |
65 username = argv[2]; |
66 old_pass = argv[3]; |
66 old_pass = argv[3]; |
67 new_pass = argv[4]; |
67 new_pass = argv[4]; |
68 host = NULL; |
68 host = NULL; |
69 |
69 |
70 if (argc >= 5) { |
70 if (argc >= 5) { |
71 int i; |
71 int i; |
72 |
72 |
73 for (i = 5; i < argc; ++i) { |
73 for (i = 5; i < argc; ++i) { |
74 if (strcmp (argv[i], "-s") == 0 || |
74 if (strcmp (argv[i], "-s") == 0 || |
75 strcmp (argv[i], "--ssl") == 0) { |
75 strcmp (argv[i], "--ssl") == 0) { |
76 use_ssl = TRUE; |
76 use_ssl = TRUE; |
77 } |
77 } |
78 else if (strcmp (argv[i], "-h") == 0 || |
78 else if (strcmp (argv[i], "-h") == 0 || |
79 strcmp (argv[i], "--host") == 0) { |
79 strcmp (argv[i], "--host") == 0) { |
80 if (++i >= argc) { |
80 if (++i >= argc) { |
81 print_usage (argv[0]); |
81 print_usage (argv[0]); |
82 return -1; |
82 return -1; |
83 } |
83 } |
84 |
84 |
85 host = argv[i]; |
85 host = argv[i]; |
86 g_print ("HOST: %s\n", host); |
86 g_print ("HOST: %s\n", host); |
87 } |
87 } |
88 } |
88 } |
89 } |
89 } |
90 |
90 |
91 connection = lm_connection_new (server); |
91 connection = lm_connection_new (server); |
92 |
92 |
93 if (host) { |
93 if (host) { |
94 gchar *jid; |
94 gchar *jid; |
95 |
95 |
96 jid = g_strdup_printf ("%s@%s", username, host); |
96 jid = g_strdup_printf ("%s@%s", username, host); |
97 g_print ("Setting jid to %s\n", jid); |
97 g_print ("Setting jid to %s\n", jid); |
98 lm_connection_set_jid (connection, jid); |
98 lm_connection_set_jid (connection, jid); |
99 g_free (jid); |
99 g_free (jid); |
100 } |
100 } |
101 |
101 |
102 if (use_ssl) { |
102 if (use_ssl) { |
103 LmSSL *ssl; |
103 LmSSL *ssl; |
104 |
104 |
105 if (!lm_ssl_is_supported ()) { |
105 if (!lm_ssl_is_supported ()) { |
106 g_print ("This loudmouth installation doesn't support SSL\n"); |
106 g_print ("This loudmouth installation doesn't support SSL\n"); |
107 return 1; |
107 return 1; |
108 } |
108 } |
109 |
109 |
110 g_print ("Setting ssl\n"); |
110 g_print ("Setting ssl\n"); |
111 ssl = lm_ssl_new (NULL, ssl_func, NULL, NULL); |
111 ssl = lm_ssl_new (NULL, ssl_func, NULL, NULL); |
112 lm_connection_set_ssl (connection, ssl); |
112 lm_connection_set_ssl (connection, ssl); |
113 lm_ssl_unref (ssl); |
113 lm_ssl_unref (ssl); |
114 |
114 |
115 lm_connection_set_port (connection, |
115 lm_connection_set_port (connection, |
116 LM_CONNECTION_DEFAULT_PORT_SSL); |
116 LM_CONNECTION_DEFAULT_PORT_SSL); |
117 } |
117 } |
118 |
118 |
119 if (!lm_connection_open_and_block (connection, &error)) { |
119 if (!lm_connection_open_and_block (connection, &error)) { |
120 g_error ("Failed to open: %s\n", error->message); |
120 g_error ("Failed to open: %s\n", error->message); |
121 } |
121 } |
122 |
122 |
123 if (!lm_connection_authenticate_and_block (connection, |
123 if (!lm_connection_authenticate_and_block (connection, |
124 username, old_pass, |
124 username, old_pass, |
125 "Password changer", |
125 "Password changer", |
126 &error)) { |
126 &error)) { |
127 g_error ("Failed to authenticate: %s\n", error->message); |
127 g_error ("Failed to authenticate: %s\n", error->message); |
128 } |
128 } |
129 |
129 |
130 m = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, |
130 m = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, |
131 LM_MESSAGE_SUB_TYPE_SET); |
131 LM_MESSAGE_SUB_TYPE_SET); |
132 |
132 |
133 query = lm_message_node_add_child (m->node, "query", NULL); |
133 query = lm_message_node_add_child (m->node, "query", NULL); |
134 |
134 |
135 lm_message_node_set_attributes (query, "xmlns", "jabber:iq:register", |
135 lm_message_node_set_attributes (query, "xmlns", "jabber:iq:register", |
136 NULL); |
136 NULL); |
137 lm_message_node_add_child (query, "username", username); |
137 lm_message_node_add_child (query, "username", username); |
138 lm_message_node_add_child (query, "password", new_pass); |
138 lm_message_node_add_child (query, "password", new_pass); |
139 |
139 |
140 reply = lm_connection_send_with_reply_and_block (connection, m, &error); |
140 reply = lm_connection_send_with_reply_and_block (connection, m, &error); |
141 if (!reply) { |
141 if (!reply) { |
142 g_error ("Failed to change password: %s\n", error->message); |
142 g_error ("Failed to change password: %s\n", error->message); |
143 } |
143 } |
144 |
144 |
145 if (lm_message_get_sub_type (reply) == LM_MESSAGE_SUB_TYPE_RESULT) { |
145 if (lm_message_get_sub_type (reply) == LM_MESSAGE_SUB_TYPE_RESULT) { |
146 g_print ("Password changed\n"); |
146 g_print ("Password changed\n"); |
147 } else { |
147 } else { |
148 g_print ("Failed to change password\n"); |
148 g_print ("Failed to change password\n"); |
149 /* If this wasn't only an example we should check error code |
149 /* If this wasn't only an example we should check error code |
150 * here to tell the user why it failed */ |
150 * here to tell the user why it failed */ |
151 } |
151 } |
152 |
152 |
153 lm_connection_close (connection, NULL); |
153 lm_connection_close (connection, NULL); |
154 |
154 |
155 return 0; |
155 return 0; |
156 } |
156 } |
157 |
157 |
158 |
158 |