1 /* HexChat
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18 #include "config.h"
19
20 #include <string.h>
21 #include <gio/gio.h>
22 #include "hexchat-plugin.h"
23
24 #define _(x) hexchat_gettext(ph,x)
25 static void identd_start_server (void);
26
27 static hexchat_plugin *ph;
28 static GSocketService *service;
29 static GHashTable *responses;
30
31 typedef struct ident_info
32 {
33 GSocketConnection *conn;
34 gchar *username;
35 } ident_info;
36
37 static void
stream_close_ready(GObject * source,GAsyncResult * res,gpointer userdata)38 stream_close_ready (GObject *source, GAsyncResult *res, gpointer userdata)
39 {
40 GError *err = NULL;
41
42 if (!g_io_stream_close_finish (G_IO_STREAM(source), res, &err))
43 {
44 g_warning ("%s", err->message);
45 g_error_free (err);
46 }
47
48 g_object_unref (source);
49 }
50
51 static void
ident_info_free(ident_info * info)52 ident_info_free (ident_info *info)
53 {
54 if (G_LIKELY(info))
55 {
56 g_io_stream_close_async (G_IO_STREAM(info->conn), G_PRIORITY_DEFAULT,
57 NULL, stream_close_ready, NULL);
58 g_free (info->username);
59 g_free (info);
60 }
61 }
62
63 static int
identd_cleanup_response_cb(gpointer userdata)64 identd_cleanup_response_cb (gpointer userdata)
65 {
66 g_return_val_if_fail (responses != NULL, 0);
67
68 g_hash_table_remove (responses, userdata);
69
70 return 0;
71 }
72
73 static int
identd_command_cb(char * word[],char * word_eol[],void * userdata)74 identd_command_cb (char *word[], char *word_eol[], void *userdata)
75 {
76 g_return_val_if_fail (responses != NULL, HEXCHAT_EAT_ALL);
77
78 if (!g_strcmp0 (word[2], "reload"))
79 {
80 if (service)
81 {
82 g_socket_service_stop (service);
83 g_clear_object (&service);
84 }
85
86 identd_start_server ();
87
88 if (service)
89 return HEXCHAT_EAT_ALL;
90 }
91
92 if (service == NULL) /* If we are not running plugins can handle it */
93 return HEXCHAT_EAT_HEXCHAT;
94
95 if (word[2] && *word[2] && word[3] && *word[3])
96 {
97 guint64 port = g_ascii_strtoull (word[2], NULL, 0);
98
99 if (port && port <= G_MAXUINT16)
100 {
101 g_hash_table_insert (responses, GINT_TO_POINTER (port), g_strdup (word[3]));
102 /* Automatically remove entry after 30 seconds */
103 hexchat_hook_timer (ph, 30000, identd_cleanup_response_cb, GINT_TO_POINTER (port));
104 }
105 }
106 else
107 {
108 hexchat_command (ph, "HELP IDENTD");
109 }
110
111 return HEXCHAT_EAT_ALL;
112 }
113
114 static void
identd_write_ready(GOutputStream * stream,GAsyncResult * res,ident_info * info)115 identd_write_ready (GOutputStream *stream, GAsyncResult *res, ident_info *info)
116 {
117 g_output_stream_write_finish (stream, res, NULL);
118
119 ident_info_free (info);
120 }
121
122 static void
identd_read_ready(GDataInputStream * in_stream,GAsyncResult * res,ident_info * info)123 identd_read_ready (GDataInputStream *in_stream, GAsyncResult *res, ident_info *info)
124 {
125 GSocketAddress *sok_addr;
126 GOutputStream *out_stream;
127 guint64 local, remote;
128 gchar *read_buf, buf[512], *p;
129
130 if ((read_buf = g_data_input_stream_read_line_finish (in_stream, res, NULL, NULL)))
131 {
132 local = g_ascii_strtoull (read_buf, NULL, 0);
133 p = strchr (read_buf, ',');
134 if (!p)
135 {
136 g_free (read_buf);
137 goto cleanup;
138 }
139
140 remote = g_ascii_strtoull (p + 1, NULL, 0);
141 g_free (read_buf);
142
143 g_snprintf (buf, sizeof (buf), "%"G_GUINT16_FORMAT", %"G_GUINT16_FORMAT" : ",
144 (guint16)MIN(local, G_MAXUINT16), (guint16)MIN(remote, G_MAXUINT16));
145
146 if (!local || !remote || local > G_MAXUINT16 || remote > G_MAXUINT16)
147 {
148 g_strlcat (buf, "ERROR : INVALID-PORT\r\n", sizeof (buf));
149 g_debug ("Identd: Received invalid port");
150 }
151 else
152 {
153 info->username = g_hash_table_lookup (responses, GINT_TO_POINTER (local));
154 if (!info->username)
155 {
156 g_strlcat (buf, "ERROR : NO-USER\r\n", sizeof (buf));
157 g_debug ("Identd: Received invalid local port");
158 }
159 else
160 {
161 const gsize len = strlen (buf);
162
163 g_hash_table_steal (responses, GINT_TO_POINTER (local));
164
165 g_snprintf (buf + len, sizeof (buf) - len, "USERID : UNIX : %s\r\n", info->username);
166
167 if ((sok_addr = g_socket_connection_get_remote_address (info->conn, NULL)))
168 {
169 GInetAddress *inet_addr;
170 gchar *addr;
171
172 inet_addr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (sok_addr));
173 addr = g_inet_address_to_string (inet_addr);
174
175 hexchat_printf (ph, _("*\tServicing ident request from %s as %s"), addr, info->username);
176
177 g_object_unref (sok_addr);
178 g_object_unref (inet_addr);
179 g_free (addr);
180 }
181 }
182 }
183
184 out_stream = g_io_stream_get_output_stream (G_IO_STREAM (info->conn));
185 g_output_stream_write_async (out_stream, buf, strlen (buf), G_PRIORITY_DEFAULT,
186 NULL, (GAsyncReadyCallback)identd_write_ready, info);
187 }
188
189 return;
190
191 cleanup:
192 ident_info_free (info);
193 }
194
195 static gboolean
identd_incoming_cb(GSocketService * service,GSocketConnection * conn,GObject * source,gpointer userdata)196 identd_incoming_cb (GSocketService *service, GSocketConnection *conn,
197 GObject *source, gpointer userdata)
198 {
199 GDataInputStream *data_stream;
200 GInputStream *stream;
201 ident_info *info;
202
203 info = g_new0 (ident_info, 1);
204
205 info->conn = conn;
206 g_object_ref (conn);
207
208 stream = g_io_stream_get_input_stream (G_IO_STREAM (conn));
209 data_stream = g_data_input_stream_new (stream);
210 g_data_input_stream_set_newline_type (data_stream, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
211 g_data_input_stream_read_line_async (data_stream, G_PRIORITY_DEFAULT,
212 NULL, (GAsyncReadyCallback)identd_read_ready, info);
213
214 return TRUE;
215 }
216
217 static void
identd_start_server(void)218 identd_start_server (void)
219 {
220 GError *error = NULL;
221 int enabled, port = 113;
222
223 if (hexchat_get_prefs (ph, "identd_server", NULL, &enabled) == 3)
224 {
225 if (!enabled)
226 return;
227 }
228 if (hexchat_get_prefs (ph, "identd_port", NULL, &port) == 2 && (port <= 0 || port > G_MAXUINT16))
229 {
230 port = 113;
231 }
232
233 service = g_socket_service_new ();
234
235 g_socket_listener_add_inet_port (G_SOCKET_LISTENER (service), port, NULL, &error);
236 if (error)
237 {
238 hexchat_printf (ph, _("*\tError starting identd server: %s"), error->message);
239
240 g_error_free (error);
241 g_clear_object (&service);
242 return;
243 }
244 /*hexchat_printf (ph, "*\tIdentd listening on port: %d", port); */
245
246 g_signal_connect (G_OBJECT (service), "incoming", G_CALLBACK(identd_incoming_cb), NULL);
247 g_socket_service_start (service);
248 }
249
250 int
identd_plugin_init(hexchat_plugin * plugin_handle,char ** plugin_name,char ** plugin_desc,char ** plugin_version,char * arg)251 identd_plugin_init (hexchat_plugin *plugin_handle, char **plugin_name,
252 char **plugin_desc, char **plugin_version, char *arg)
253 {
254 ph = plugin_handle;
255 *plugin_name = "";
256 *plugin_desc = "";
257 *plugin_version = "";
258
259
260 responses = g_hash_table_new_full (NULL, NULL, NULL, g_free);
261 hexchat_hook_command (ph, "IDENTD", HEXCHAT_PRI_NORM, identd_command_cb,
262 _("IDENTD <port> <username>"), NULL);
263
264 identd_start_server ();
265
266 return 1; /* This must always succeed for /identd to work */
267 }
268
269 int
identd_plugin_deinit(void)270 identd_plugin_deinit (void)
271 {
272 if (service)
273 {
274 g_socket_service_stop (service);
275 g_object_unref (service);
276 }
277
278 g_hash_table_destroy (responses);
279
280 return 1;
281 }
282