Debian Codegraph @

modules/transport/xmlrpc/main.c

raw
1 /*
2 * Copyright (c) 2005-2006 Jilles Tjoelker et al.
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * New xmlrpc implementation
6 *
7 */
8
9 #include "atheme.h"
10 #include "httpd.h"
11 #include "xmlrpclib.h"
12 #include "datastream.h"
13 #include "authcookie.h"
14
15 DECLARE_MODULE_V1
16 (
17 "transport/xmlrpc", false, _modinit, _moddeinit,
18 PACKAGE_STRING,
19 VENDOR_STRING
20 );
21
22 static void handle_request(connection_t *cptr, void *requestbuf);
23
24 path_handler_t handle_xmlrpc = { NULL, handle_request };
25
26 struct
27 {
28 char *path;
29 } xmlrpc_config;
30
31 connection_t *current_cptr; /* XXX: Hack: src/xmlrpc.c requires us to do this */
32
33 mowgli_list_t *httpd_path_handlers;
34
35 static void xmlrpc_command_fail(sourceinfo_t *si, cmd_faultcode_t code, const char *message);
36 static void xmlrpc_command_success_nodata(sourceinfo_t *si, const char *message);
37 static void xmlrpc_command_success_string(sourceinfo_t *si, const char *result, const char *message);
38
39 static int xmlrpcmethod_login(void *conn, int parc, char *parv[]);
40 static int xmlrpcmethod_logout(void *conn, int parc, char *parv[]);
41 static int xmlrpcmethod_command(void *conn, int parc, char *parv[]);
42 static int xmlrpcmethod_privset(void *conn, int parc, char *parv[]);
43 static int xmlrpcmethod_ison(void *conn, int parc, char *parv[]);
44 static int xmlrpcmethod_metadata(void *conn, int parc, char *parv[]);
45
46 /* Configuration */
47 mowgli_list_t conf_xmlrpc_table;
48
49 struct sourceinfo_vtable xmlrpc_vtable = {
50 .description = "xmlrpc",
51 .cmd_fail = xmlrpc_command_fail,
52 .cmd_success_nodata = xmlrpc_command_success_nodata,
53 .cmd_success_string = xmlrpc_command_success_string
54 };
55
56 static char *dump_buffer(char *buf, int length)
57 {
58 struct httpddata *hd;
59 char buf1[300];
60
61 hd = current_cptr->userdata;
62 snprintf(buf1, sizeof buf1, "HTTP/1.1 200 OK\r\n"
63 "%s"
64 "Server: Atheme/%s\r\n"
65 "Content-Type: text/xml\r\n"
66 "Content-Length: %d\r\n\r\n",
67 hd->connection_close ? "Connection: close\r\n" : "",
68 PACKAGE_VERSION, length);
69 sendq_add(current_cptr, buf1, strlen(buf1));
70 sendq_add(current_cptr, buf, length);
71 if (hd->connection_close)
72 sendq_add_eof(current_cptr);
73 return buf;
74 }
75
76 static void handle_request(connection_t *cptr, void *requestbuf)
77 {
78 current_cptr = cptr;
79 xmlrpc_process(requestbuf, cptr);
80 current_cptr = NULL;
81
82 return;
83 }
84
85 static void xmlrpc_config_ready(void *vptr)
86 {
87 /* Note: handle_xmlrpc.path may point to freed memory between
88 * reading the config and here.
89 */
90 handle_xmlrpc.path = xmlrpc_config.path;
91
92 if (handle_xmlrpc.handler != NULL)
93 {
94 if (mowgli_node_find(&handle_xmlrpc, httpd_path_handlers))
95 return;
96
97 mowgli_node_add(&handle_xmlrpc, mowgli_node_create(), httpd_path_handlers);
98 }
99 else
100 slog(LG_ERROR, "xmlrpc_config_ready(): xmlrpc {} block missing or invalid");
101 }
102
103 void _modinit(module_t *m)
104 {
105 MODULE_TRY_REQUEST_SYMBOL(m, httpd_path_handlers, "misc/httpd", "httpd_path_handlers");
106
107 hook_add_event("config_ready");
108 hook_add_config_ready(xmlrpc_config_ready);
109
110 xmlrpc_config.path = sstrdup("/xmlrpc");
111
112 add_subblock_top_conf("XMLRPC", &conf_xmlrpc_table);
113 add_dupstr_conf_item("PATH", &conf_xmlrpc_table, 0, &xmlrpc_config.path, NULL);
114
115 xmlrpc_set_buffer(dump_buffer);
116 xmlrpc_set_options(XMLRPC_HTTP_HEADER, XMLRPC_OFF);
117 xmlrpc_register_method("atheme.login", xmlrpcmethod_login);
118 xmlrpc_register_method("atheme.logout", xmlrpcmethod_logout);
119 xmlrpc_register_method("atheme.command", xmlrpcmethod_command);
120 xmlrpc_register_method("atheme.privset", xmlrpcmethod_privset);
121 xmlrpc_register_method("atheme.ison", xmlrpcmethod_ison);
122 xmlrpc_register_method("atheme.metadata", xmlrpcmethod_metadata);
123 }
124
125 void _moddeinit(module_unload_intent_t intent)
126 {
127 mowgli_node_t *n;
128
129 xmlrpc_unregister_method("atheme.login");
130 xmlrpc_unregister_method("atheme.logout");
131 xmlrpc_unregister_method("atheme.command");
132 xmlrpc_unregister_method("atheme.privset");
133 xmlrpc_unregister_method("atheme.ison");
134 xmlrpc_unregister_method("atheme.metadata");
135
136 if ((n = mowgli_node_find(&handle_xmlrpc, httpd_path_handlers)) != NULL)
137 {
138 mowgli_node_delete(n, httpd_path_handlers);
139 mowgli_node_free(n);
140 }
141
142 del_conf_item("PATH", &conf_xmlrpc_table);
143 del_top_conf("XMLRPC");
144
145 free(xmlrpc_config.path);
146
147 hook_del_config_ready(xmlrpc_config_ready);
148 }
149
150 static void xmlrpc_command_fail(sourceinfo_t *si, cmd_faultcode_t code, const char *message)
151 {
152 connection_t *cptr;
153 struct httpddata *hd;
154 char *newmessage;
155
156 cptr = si->connection;
157 hd = cptr->userdata;
158 if (hd->sent_reply)
159 return;
160 newmessage = xmlrpc_normalizeBuffer(message);
161 xmlrpc_generic_error(code, newmessage);
162 free(newmessage);
163 hd->sent_reply = true;
164 }
165
166 static void xmlrpc_command_success_nodata(sourceinfo_t *si, const char *message)
167 {
168 connection_t *cptr;
169 struct httpddata *hd;
170 char *newmessage;
171 char *p;
172
173 newmessage = xmlrpc_normalizeBuffer(message);
174
175 cptr = si->connection;
176 hd = cptr->userdata;
177 if (hd->sent_reply)
178 {
179 free(newmessage);
180 return;
181 }
182 if (hd->replybuf != NULL)
183 {
184 hd->replybuf = srealloc(hd->replybuf, strlen(hd->replybuf) + strlen(newmessage) + 2);
185 p = hd->replybuf + strlen(hd->replybuf);
186 *p++ = '\n';
187 }
188 else
189 {
190 hd->replybuf = smalloc(strlen(newmessage) + 1);
191 p = hd->replybuf;
192 }
193 strcpy(p, newmessage);
194 free(newmessage);
195 }
196
197 static void xmlrpc_command_success_string(sourceinfo_t *si, const char *result, const char *message)
198 {
199 connection_t *cptr;
200 struct httpddata *hd;
201
202 cptr = si->connection;
203 hd = cptr->userdata;
204 if (hd->sent_reply)
205 return;
206 xmlrpc_send_string(result);
207 hd->sent_reply = true;
208 }
209
210 /* These taken from the old modules/xmlrpc/account.c */
211 /*
212 * atheme.login
213 *
214 * XML Inputs:
215 * account name, password, source ip (optional)
216 *
217 * XML Outputs:
218 * fault 1 - insufficient parameters
219 * fault 3 - account is not registered
220 * fault 5 - invalid username and password
221 * fault 6 - account is frozen
222 * default - success (authcookie)
223 *
224 * Side Effects:
225 * an authcookie ticket is created for the myuser_t.
226 * the user's lastlogin is updated
227 */
228 static int xmlrpcmethod_login(void *conn, int parc, char *parv[])
229 {
230 myuser_t *mu;
231 authcookie_t *ac;
232 const char *sourceip;
233
234 if (parc < 2)
235 {
236 xmlrpc_generic_error(fault_needmoreparams, "Insufficient parameters.");
237 return 0;
238 }
239
240 sourceip = parc >= 3 && *parv[2] != '\0' ? parv[2] : NULL;
241
242 if (!(mu = myuser_find(parv[0])))
243 {
244 xmlrpc_generic_error(fault_nosuch_source, "The account is not registered.");
245 return 0;
246 }
247
248 if (metadata_find(mu, "private:freeze:freezer") != NULL)
249 {
250 logcommand_external(nicksvs.me, "xmlrpc", conn, sourceip, NULL, CMDLOG_LOGIN, "failed LOGIN to \2%s\2 (frozen)", entity(mu)->name);
251 xmlrpc_generic_error(fault_noprivs, "The account has been frozen.");
252 return 0;
253 }
254
255 if (!verify_password(mu, parv[1]))
256 {
257 sourceinfo_t *si;
258
259 logcommand_external(nicksvs.me, "xmlrpc", conn, sourceip, NULL, CMDLOG_LOGIN, "failed LOGIN to \2%s\2 (bad password)", entity(mu)->name);
260 xmlrpc_generic_error(fault_authfail, "The password is not valid for this account.");
261
262 si = sourceinfo_create();
263 si->service = NULL;
264 si->sourcedesc = parv[2] != NULL && *parv[2] ? parv[2] : NULL;
265 si->connection = conn;
266 si->v = &xmlrpc_vtable;
267 si->force_language = language_find("en");
268
269 bad_password(si, mu);
270
271 object_unref(si);
272
273 return 0;
274 }
275
276 mu->lastlogin = CURRTIME;
277
278 ac = authcookie_create(mu);
279
280 logcommand_external(nicksvs.me, "xmlrpc", conn, sourceip, mu, CMDLOG_LOGIN, "LOGIN");
281
282 xmlrpc_send_string(ac->ticket);
283
284 return 0;
285 }
286
287 /*
288 * atheme.logout
289 *
290 * XML inputs:
291 * authcookie, and account name.
292 *
293 * XML outputs:
294 * fault 1 - insufficient parameters
295 * fault 3 - unknown user
296 * fault 15 - validation failed
297 * default - success message
298 *
299 * Side Effects:
300 * an authcookie ticket is destroyed.
301 */
302 static int xmlrpcmethod_logout(void *conn, int parc, char *parv[])
303 {
304 authcookie_t *ac;
305 myuser_t *mu;
306
307 if (parc < 2)
308 {
309 xmlrpc_generic_error(fault_needmoreparams, "Insufficient parameters.");
310 return 0;
311 }
312
313 if ((mu = myuser_find(parv[1])) == NULL)
314 {
315 xmlrpc_generic_error(fault_nosuch_source, "Unknown user.");
316 return 0;
317 }
318
319 if (authcookie_validate(parv[0], mu) == false)
320 {
321 xmlrpc_generic_error(fault_badauthcookie, "Invalid authcookie for this account.");
322 return 0;
323 }
324
325 logcommand_external(nicksvs.me, "xmlrpc", conn, NULL, mu, CMDLOG_LOGIN, "LOGOUT");
326
327 ac = authcookie_find(parv[0], mu);
328 authcookie_destroy(ac);
329
330 xmlrpc_send_string("You are now logged out.");
331
332 return 0;
333 }
334
335 /*
336 * atheme.command
337 *
338 * XML inputs:
339 * authcookie, account name, source ip, service name, command name,
340 * parameters.
341 *
342 * XML outputs:
343 * depends on command
344 *
345 * Side Effects:
346 * command is executed
347 */
348 static int xmlrpcmethod_command(void *conn, int parc, char *parv[])
349 {
350 myuser_t *mu;
351 service_t *svs;
352 command_t *cmd;
353 sourceinfo_t *si;
354 int newparc;
355 char *newparv[20];
356 struct httpddata *hd = ((connection_t *)conn)->userdata;
357 int i;
358
359 for (i = 0; i < parc; i++)
360 {
361 if (*parv[i] == '\0' || strchr(parv[i], '\r') || strchr(parv[i], '\n'))
362 {
363 xmlrpc_generic_error(fault_badparams, "Invalid parameters.");
364 return 0;
365 }
366 }
367
368 if (parc < 5)
369 {
370 xmlrpc_generic_error(fault_needmoreparams, "Insufficient parameters.");
371 return 0;
372 }
373
374 if (*parv[1] != '\0' && strlen(parv[0]) > 1)
375 {
376 if ((mu = myuser_find(parv[1])) == NULL)
377 {
378 xmlrpc_generic_error(fault_nosuch_source, "Unknown user.");
379 return 0;
380 }
381
382 if (authcookie_validate(parv[0], mu) == false)
383 {
384 xmlrpc_generic_error(fault_badauthcookie, "Invalid authcookie for this account.");
385 return 0;
386 }
387 }
388 else
389 mu = NULL;
390
391 /* try literal service name first, then user-configured nickname. */
392 svs = service_find(parv[3]);
393 if ((svs == NULL && (svs = service_find_nick(parv[3])) == NULL) || svs->commands == NULL)
394 {
395 slog(LG_DEBUG, "xmlrpcmethod_command(): invalid service %s", parv[3]);
396 xmlrpc_generic_error(fault_nosuch_source, "Invalid service name.");
397 return 0;
398 }
399 cmd = command_find(svs->commands, parv[4]);
400 if (cmd == NULL)
401 {
402 xmlrpc_generic_error(fault_nosuch_source, "Invalid command name.");
403 return 0;
404 }
405
406 memset(newparv, '\0', sizeof newparv);
407 newparc = parc - 5;
408 if (newparc > 20)
409 newparc = 20;
410 if (newparc > 0)
411 memcpy(newparv, parv + 5, newparc * sizeof(parv[0]));
412
413 si = sourceinfo_create();
414 si->smu = mu;
415 si->service = svs;
416 si->sourcedesc = parv[2][0] != '\0' ? parv[2] : NULL;
417 si->connection = conn;
418 si->v = &xmlrpc_vtable;
419 si->force_language = language_find("en");
420 command_exec(svs, si, cmd, newparc, newparv);
421
422 /* XXX: needs to be fixed up for restartable commands... */
423 if (!hd->sent_reply)
424 {
425 if (hd->replybuf != NULL)
426 xmlrpc_send_string(hd->replybuf);
427 else
428 xmlrpc_generic_error(fault_unimplemented, "Command did not return a result.");
429 }
430
431 object_unref(si);
432
433 return 0;
434 }
435
436 /*
437 * atheme.privset
438 *
439 * XML inputs:
440 * authcookie, account name, source ip
441 *
442 * XML outputs:
443 * depends on command
444 *
445 * Side Effects:
446 * command is executed
447 */
448 static int xmlrpcmethod_privset(void *conn, int parc, char *parv[])
449 {
450 myuser_t *mu;
451 int i;
452
453 for (i = 0; i < parc; i++)
454 {
455 if (strchr(parv[i], '\r') || strchr(parv[i], '\n'))
456 {
457 xmlrpc_generic_error(fault_badparams, "Invalid parameters.");
458 return 0;
459 }
460 }
461
462 if (parc < 2)
463 {
464 xmlrpc_generic_error(fault_needmoreparams, "Insufficient parameters.");
465 return 0;
466 }
467
468 if (*parv[1] != '\0' && strlen(parv[0]) > 1)
469 {
470 if ((mu = myuser_find(parv[1])) == NULL)
471 {
472 xmlrpc_generic_error(fault_nosuch_source, "Unknown user.");
473 return 0;
474 }
475
476 if (authcookie_validate(parv[0], mu) == false)
477 {
478 xmlrpc_generic_error(fault_badauthcookie, "Invalid authcookie for this account.");
479 return 0;
480 }
481 }
482 else
483 mu = NULL;
484
485 if (mu == NULL || !is_soper(mu))
486 {
487 /* no privileges */
488 xmlrpc_send_string("");
489 return 0;
490 }
491 xmlrpc_send_string(mu->soper->operclass->privs);
492
493 return 0;
494 }
495
496 /*
497 * atheme.ison
498 *
499 * XML inputs:
500 * nickname
501 *
502 * XML outputs:
503 * boolean: if nickname is online
504 * string: if nickname is authenticated, what entity they are authed to,
505 * else '*'
506 */
507 static int xmlrpcmethod_ison(void *conn, int parc, char *parv[])
508 {
509 user_t *u;
510 int i;
511 char buf[XMLRPC_BUFSIZE], buf2[XMLRPC_BUFSIZE];
512
513 for (i = 0; i < parc; i++)
514 {
515 if (strchr(parv[i], '\r') || strchr(parv[i], '\n'))
516 {
517 xmlrpc_generic_error(fault_badparams, "Invalid parameters.");
518 return 0;
519 }
520 }
521
522 if (parc < 1)
523 {
524 xmlrpc_generic_error(fault_needmoreparams, "Insufficient parameters.");
525 return 0;
526 }
527
528 u = user_find(parv[0]);
529 if (u == NULL)
530 {
531 xmlrpc_boolean(buf, false);
532 xmlrpc_string(buf2, "*");
533 xmlrpc_send(2, buf, buf2);
534 return 0;
535 }
536
537 xmlrpc_boolean(buf, true);
538 xmlrpc_string(buf2, u->myuser != NULL ? entity(u->myuser)->name : "*");
539 xmlrpc_send(2, buf, buf2);
540
541 return 0;
542 }
543
544 /*
545 * atheme.metadata
546 *
547 * XML inputs:
548 * entity name, UID or channel name
549 * metadata key
550 * metadata value (optional)
551 *
552 * XML outputs:
553 * string: metadata value
554 */
555 static int xmlrpcmethod_metadata(void *conn, int parc, char *parv[])
556 {
557 metadata_t *md;
558 int i;
559 char buf[XMLRPC_BUFSIZE];
560
561 for (i = 0; i < parc; i++)
562 {
563 if (strchr(parv[i], '\r') || strchr(parv[i], '\n'))
564 {
565 xmlrpc_generic_error(fault_badparams, "Invalid parameters.");
566 return 0;
567 }
568 }
569
570 if (parc < 2)
571 {
572 xmlrpc_generic_error(fault_needmoreparams, "Insufficient parameters.");
573 return 0;
574 }
575
576 if (*parv[0] == '#')
577 {
578 mychan_t *mc;
579
580 mc = mychan_find(parv[0]);
581 if (mc == NULL)
582 {
583 xmlrpc_generic_error(fault_nosuch_source, "No channel registration was found for the provided channel name.");
584 return 0;
585 }
586
587 md = metadata_find(mc, parv[1]);
588 }
589 else
590 {
591 myentity_t *mt;
592
593 mt = myentity_find(parv[0]);
594 if (mt == NULL)
595 mt = myentity_find_uid(parv[0]);
596
597 if (mt == NULL)
598 {
599 xmlrpc_generic_error(fault_nosuch_source, "No account was found for this accountname or UID.");
600 return 0;
601 }
602
603 md = metadata_find(mt, parv[1]);
604 }
605
606 if (md == NULL)
607 {
608 xmlrpc_generic_error(fault_nosuch_source, "No metadata found matching this account/channel and key.");
609 return 0;
610 }
611
612 xmlrpc_string(buf, md->value);
613 xmlrpc_send(1, buf);
614
615 return 0;
616 }
617
618 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs ts=8 sw=8 noexpandtab
619 */