| 1 |
/* |
| 2 |
* Copyright (c) 2014 Atheme Development Group |
| 3 |
* Rights to this code are as documented in doc/LICENSE. |
| 4 |
* |
| 5 |
* JSONRPC implementation |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "atheme.h" |
| 10 |
#include "httpd.h" |
| 11 |
#include "jsonrpclib.h" |
| 12 |
#include "datastream.h" |
| 13 |
#include "authcookie.h" |
| 14 |
|
| 15 |
DECLARE_MODULE_V1 |
| 16 |
( |
| 17 |
"transport/jsonrpc", false, _modinit, _moddeinit, |
| 18 |
PACKAGE_STRING, |
| 19 |
VENDOR_STRING |
| 20 |
); |
| 21 |
|
| 22 |
static void handle_request(connection_t *cptr, void *requestbuf); |
| 23 |
|
| 24 |
mowgli_list_t *httpd_path_handlers; |
| 25 |
static mowgli_patricia_t *json_methods; |
| 26 |
|
| 27 |
static bool jsonrpcmethod_login(void *conn, mowgli_list_t *params, char *id); |
| 28 |
static bool jsonrpcmethod_logout(void *conn, mowgli_list_t *params, char *id); |
| 29 |
static bool jsonrpcmethod_command(void *conn, mowgli_list_t *params, char *id); |
| 30 |
static bool jsonrpcmethod_privset(void *conn, mowgli_list_t *params, char *id); |
| 31 |
static bool jsonrpcmethod_ison(void *conn, mowgli_list_t *params, char *id); |
| 32 |
static bool jsonrpcmethod_metadata(void *conn, mowgli_list_t *params, char *id); |
| 33 |
|
| 34 |
|
| 35 |
static void jsonrpc_command_fail(sourceinfo_t *si, cmd_faultcode_t code, const char *message); |
| 36 |
static void jsonrpc_command_success_string(sourceinfo_t *si, const char *result, const char *message); |
| 37 |
static void jsonrpc_command_success_nodata(sourceinfo_t *si, const char *message); |
| 38 |
|
| 39 |
struct sourceinfo_vtable jsonrpc_vtable = { |
| 40 |
.description = "jsonrpc", |
| 41 |
.cmd_fail = jsonrpc_command_fail, |
| 42 |
.cmd_success_string = jsonrpc_command_success_string, |
| 43 |
.cmd_success_nodata = jsonrpc_command_success_nodata |
| 44 |
}; |
| 45 |
|
| 46 |
path_handler_t handle_jsonrpc = { NULL, handle_request }; |
| 47 |
|
| 48 |
static void handle_request(connection_t *cptr, void *requestbuf) |
| 49 |
{ |
| 50 |
|
| 51 |
jsonrpc_process(requestbuf, cptr); |
| 52 |
|
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
void _modinit(module_t *m) |
| 57 |
{ |
| 58 |
MODULE_TRY_REQUEST_SYMBOL(m, httpd_path_handlers, "misc/httpd", "httpd_path_handlers"); |
| 59 |
|
| 60 |
handle_jsonrpc.path = "/jsonrpc"; |
| 61 |
mowgli_node_add(&handle_jsonrpc, mowgli_node_create(), httpd_path_handlers); |
| 62 |
|
| 63 |
json_methods = mowgli_patricia_create(strcasecanon); |
| 64 |
|
| 65 |
jsonrpc_register_method("atheme.login", jsonrpcmethod_login); |
| 66 |
jsonrpc_register_method("atheme.logout", jsonrpcmethod_logout); |
| 67 |
jsonrpc_register_method("atheme.command", jsonrpcmethod_command); |
| 68 |
|
| 69 |
jsonrpc_register_method("atheme.privset", jsonrpcmethod_privset); |
| 70 |
jsonrpc_register_method("atheme.ison", jsonrpcmethod_ison); |
| 71 |
jsonrpc_register_method("atheme.metadata", jsonrpcmethod_metadata); |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
void _moddeinit(module_unload_intent_t intent) |
| 76 |
{ |
| 77 |
mowgli_node_t *n; |
| 78 |
|
| 79 |
jsonrpc_unregister_method("atheme.login"); |
| 80 |
jsonrpc_unregister_method("atheme.logout"); |
| 81 |
jsonrpc_unregister_method("atheme.command"); |
| 82 |
|
| 83 |
jsonrpc_unregister_method("atheme.privset"); |
| 84 |
jsonrpc_unregister_method("atheme.ison"); |
| 85 |
jsonrpc_unregister_method("atheme.metadata"); |
| 86 |
|
| 87 |
if ((n = mowgli_node_find(&handle_jsonrpc, httpd_path_handlers)) != NULL) |
| 88 |
{ |
| 89 |
mowgli_node_delete(n, httpd_path_handlers); |
| 90 |
mowgli_node_free(n); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void jsonrpc_register_method(const char *method_name, jsonrpc_method_t method) { |
| 95 |
mowgli_patricia_add(json_methods, method_name, method); |
| 96 |
} |
| 97 |
|
| 98 |
void jsonrpc_unregister_method(const char *method_name) { |
| 99 |
mowgli_patricia_delete(json_methods, method_name); |
| 100 |
} |
| 101 |
|
| 102 |
jsonrpc_method_t get_json_method(const char *method_name) { |
| 103 |
return mowgli_patricia_retrieve(json_methods, method_name); |
| 104 |
} |
| 105 |
|
| 106 |
/* These taken from modules/transport/xmlrpc/main.c */ |
| 107 |
/* |
| 108 |
* atheme.login |
| 109 |
* |
| 110 |
* Parameters: |
| 111 |
* account name, password, source ip (optional) |
| 112 |
* |
| 113 |
* Outputs: |
| 114 |
* fault 1 - insufficient parameters |
| 115 |
* fault 3 - account is not registered |
| 116 |
* fault 5 - invalid username and password |
| 117 |
* fault 6 - account is frozen |
| 118 |
* default - success (authcookie) |
| 119 |
* |
| 120 |
* Side Effects: |
| 121 |
* an authcookie ticket is created for the myuser_t. |
| 122 |
* the user's lastlogin is updated |
| 123 |
*/ |
| 124 |
|
| 125 |
static bool jsonrpcmethod_login(void *conn, mowgli_list_t *params, char *id) |
| 126 |
{ |
| 127 |
myuser_t *mu; |
| 128 |
authcookie_t *ac; |
| 129 |
char *sourceip, *accountname, *password; |
| 130 |
|
| 131 |
size_t len = MOWGLI_LIST_LENGTH(params); |
| 132 |
|
| 133 |
if (len < 2) |
| 134 |
{ |
| 135 |
jsonrpc_failure_string(conn, fault_needmoreparams, "Insufficient parameters.", id); |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
accountname = mowgli_node_nth_data(params, 0); |
| 140 |
password = mowgli_node_nth_data(params, 1); |
| 141 |
sourceip = len >= 3 ? mowgli_node_nth_data(params, 2) : NULL; |
| 142 |
|
| 143 |
if (!(mu = myuser_find(accountname))) |
| 144 |
{ |
| 145 |
jsonrpc_failure_string(conn, fault_nosuch_source, "The account is not registered.", id); |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
if (metadata_find(mu, "private:freeze:freezer") != NULL) |
| 150 |
{ |
| 151 |
logcommand_external(nicksvs.me, "jsonrpc", conn, sourceip, NULL, CMDLOG_LOGIN, "failed LOGIN to \2%s\2 (frozen)", entity(mu)->name); |
| 152 |
jsonrpc_failure_string(conn, fault_noprivs, "The account has been frozen.", id); |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
if (!verify_password(mu, password)) |
| 157 |
{ |
| 158 |
sourceinfo_t *si; |
| 159 |
|
| 160 |
logcommand_external(nicksvs.me, "jsonrpc", conn, sourceip, NULL, CMDLOG_LOGIN, "failed LOGIN to \2%s\2 (bad password)", entity(mu)->name); |
| 161 |
jsonrpc_failure_string(conn, fault_authfail, "The password is incorrect.", id); |
| 162 |
|
| 163 |
si = sourceinfo_create(); |
| 164 |
|
| 165 |
jsonrpc_sourceinfo_t *jsi = (jsonrpc_sourceinfo_t *)si; |
| 166 |
|
| 167 |
si->service = NULL; |
| 168 |
si->sourcedesc = sourceip; |
| 169 |
si->connection = conn; |
| 170 |
si->v = &jsonrpc_vtable; |
| 171 |
si->force_language = language_find("en"); |
| 172 |
|
| 173 |
jsi->base = si; |
| 174 |
jsi->id = id; |
| 175 |
|
| 176 |
bad_password(si, mu); |
| 177 |
|
| 178 |
object_unref(si); |
| 179 |
|
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
mu->lastlogin = CURRTIME; |
| 184 |
|
| 185 |
ac = authcookie_create(mu); |
| 186 |
|
| 187 |
logcommand_external(nicksvs.me, "jsonrpc", conn, sourceip, mu, CMDLOG_LOGIN, "LOGIN"); |
| 188 |
|
| 189 |
jsonrpc_success_string(conn, ac->ticket, id); |
| 190 |
|
| 191 |
return true; |
| 192 |
} |
| 193 |
|
| 194 |
/* |
| 195 |
* atheme.logout |
| 196 |
* |
| 197 |
* JSON inputs: |
| 198 |
* authcookie, and account name. |
| 199 |
* |
| 200 |
* JSON outputs: |
| 201 |
* fault 1 - insufficient parameters |
| 202 |
* fault 3 - unknown user |
| 203 |
* fault 15 - validation failed |
| 204 |
* default - success message |
| 205 |
* |
| 206 |
* JSON Effects: |
| 207 |
* an authcookie ticket is destroyed. |
| 208 |
*/ |
| 209 |
|
| 210 |
static bool jsonrpcmethod_logout(void *conn, mowgli_list_t *params, char *id) |
| 211 |
{ |
| 212 |
authcookie_t *ac; |
| 213 |
myuser_t *mu; |
| 214 |
char *accountname; |
| 215 |
char *cookie; |
| 216 |
|
| 217 |
size_t len = MOWGLI_LIST_LENGTH(params); |
| 218 |
cookie = mowgli_node_nth_data(params, 0); |
| 219 |
accountname = mowgli_node_nth_data(params, 1); |
| 220 |
|
| 221 |
if (len < 2) |
| 222 |
{ |
| 223 |
jsonrpc_failure_string(conn, fault_needmoreparams, "Insufficient parameters.", id); |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
if ((mu = myuser_find(accountname)) == NULL) |
| 228 |
{ |
| 229 |
jsonrpc_failure_string(conn, fault_nosuch_source, "Unknown user.", id); |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
if (authcookie_validate(cookie, mu) == false) |
| 234 |
{ |
| 235 |
jsonrpc_failure_string(conn, fault_badauthcookie, "Invalid authcookie for this account.", id); |
| 236 |
return false; |
| 237 |
} |
| 238 |
|
| 239 |
logcommand_external(nicksvs.me, "jsonrpc", conn, NULL, mu, CMDLOG_LOGIN, "LOGOUT"); |
| 240 |
|
| 241 |
ac = authcookie_find(cookie, mu); |
| 242 |
authcookie_destroy(ac); |
| 243 |
|
| 244 |
jsonrpc_success_string(conn, "You are now logged out.", id); |
| 245 |
|
| 246 |
return 0; |
| 247 |
} |
| 248 |
|
| 249 |
static void jsonrpc_command_fail(sourceinfo_t *si, cmd_faultcode_t code, const char *message) |
| 250 |
{ |
| 251 |
connection_t *cptr; |
| 252 |
struct httpddata *hd; |
| 253 |
char *newmessage; |
| 254 |
|
| 255 |
cptr = si->connection; |
| 256 |
hd = cptr->userdata; |
| 257 |
if (hd->sent_reply) |
| 258 |
return; |
| 259 |
newmessage = jsonrpc_normalizeBuffer(message); |
| 260 |
|
| 261 |
jsonrpc_sourceinfo_t *jsi = (jsonrpc_sourceinfo_t *)si; |
| 262 |
|
| 263 |
jsonrpc_failure_string(cptr, code, newmessage, jsi->id); |
| 264 |
|
| 265 |
free(newmessage); |
| 266 |
hd->sent_reply = true; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
static void jsonrpc_command_success_string(sourceinfo_t *si, const char *result, const char *message) |
| 271 |
{ |
| 272 |
connection_t *cptr; |
| 273 |
struct httpddata *hd; |
| 274 |
|
| 275 |
cptr = si->connection; |
| 276 |
hd = cptr->userdata; |
| 277 |
if (hd->sent_reply) |
| 278 |
return; |
| 279 |
|
| 280 |
jsonrpc_sourceinfo_t *jsi = (jsonrpc_sourceinfo_t *)si; |
| 281 |
|
| 282 |
jsonrpc_success_string(cptr, result, jsi->id); |
| 283 |
hd->sent_reply = true; |
| 284 |
} |
| 285 |
|
| 286 |
static void jsonrpc_command_success_nodata(sourceinfo_t *si, const char *message) |
| 287 |
{ |
| 288 |
connection_t *cptr; |
| 289 |
struct httpddata *hd; |
| 290 |
char *p; |
| 291 |
|
| 292 |
char *newmessage = jsonrpc_normalizeBuffer(message); |
| 293 |
|
| 294 |
cptr = si->connection; |
| 295 |
hd = cptr->userdata; |
| 296 |
if (hd->sent_reply) |
| 297 |
{ |
| 298 |
free(newmessage); |
| 299 |
return; |
| 300 |
} |
| 301 |
if (hd->replybuf != NULL) |
| 302 |
{ |
| 303 |
hd->replybuf = srealloc(hd->replybuf, strlen(hd->replybuf) + strlen(newmessage) + 2); |
| 304 |
p = hd->replybuf + strlen(hd->replybuf); |
| 305 |
*p++ = '\n'; |
| 306 |
} |
| 307 |
else |
| 308 |
{ |
| 309 |
hd->replybuf = smalloc(strlen(newmessage) + 1); |
| 310 |
p = hd->replybuf; |
| 311 |
} |
| 312 |
|
| 313 |
strcpy(p, newmessage); |
| 314 |
free(newmessage); |
| 315 |
} |
| 316 |
|
| 317 |
/* |
| 318 |
* atheme.command |
| 319 |
* |
| 320 |
* JSON inputs: |
| 321 |
* authcookie, account name, source ip, service name, command name, |
| 322 |
* parameters. |
| 323 |
* |
| 324 |
* JSON outputs: |
| 325 |
* depends on command |
| 326 |
* |
| 327 |
* JSON Effects: |
| 328 |
* command is executed |
| 329 |
*/ |
| 330 |
|
| 331 |
static bool jsonrpcmethod_command(void *conn, mowgli_list_t *params, char *id) |
| 332 |
{ |
| 333 |
myuser_t *mu; |
| 334 |
service_t *svs; |
| 335 |
command_t *cmd; |
| 336 |
sourceinfo_t *si; |
| 337 |
int newparc; |
| 338 |
char *newparv[20]; |
| 339 |
struct httpddata *hd = ((connection_t *)conn)->userdata; |
| 340 |
|
| 341 |
authcookie_t *ac; |
| 342 |
char *accountname, *cookie, *service, *command, *sourceip; |
| 343 |
|
| 344 |
size_t len = MOWGLI_LIST_LENGTH(params); |
| 345 |
cookie = mowgli_node_nth_data(params, 0); |
| 346 |
accountname = mowgli_node_nth_data(params, 1); |
| 347 |
sourceip = mowgli_node_nth_data(params, 2); |
| 348 |
service = mowgli_node_nth_data(params, 3); |
| 349 |
command = mowgli_node_nth_data(params, 4); |
| 350 |
|
| 351 |
mowgli_node_t *n; |
| 352 |
char *param; |
| 353 |
|
| 354 |
MOWGLI_LIST_FOREACH(n, params->head) |
| 355 |
{ |
| 356 |
param = n->data; |
| 357 |
|
| 358 |
if (*param == '\0' || strchr(param, '\r') || strchr(param, '\n')) |
| 359 |
{ |
| 360 |
jsonrpc_failure_string(conn, fault_badparams, "Invalid authcookie for this account.", id); |
| 361 |
return 0; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
if (len < 5) |
| 366 |
{ |
| 367 |
jsonrpc_failure_string(conn, fault_needmoreparams, "Insufficient parameters.", id); |
| 368 |
return 0; |
| 369 |
} |
| 370 |
|
| 371 |
if (*accountname != '\0' && strlen(cookie) > 1) |
| 372 |
{ |
| 373 |
if ((mu = myuser_find(accountname)) == NULL) |
| 374 |
{ |
| 375 |
jsonrpc_failure_string(conn, fault_nosuch_source, "Unknown user.", id); |
| 376 |
return 0; |
| 377 |
} |
| 378 |
|
| 379 |
if (authcookie_validate(cookie, mu) == false) |
| 380 |
{ |
| 381 |
jsonrpc_failure_string(conn, fault_badauthcookie, "Invalid authcookie for this account.", id); |
| 382 |
return 0; |
| 383 |
} |
| 384 |
} |
| 385 |
else |
| 386 |
mu = NULL; |
| 387 |
|
| 388 |
/* try literal service name first, then user-configured nickname. */ |
| 389 |
svs = service_find(service); |
| 390 |
if ((svs == NULL && (svs = service_find_nick(service)) == NULL) || svs->commands == NULL) |
| 391 |
{ |
| 392 |
slog(LG_DEBUG, "xmlrpcmethod_command(): invalid service %s", service); |
| 393 |
jsonrpc_failure_string(conn, fault_nosuch_source, "Invalid service name.", id); |
| 394 |
return 0; |
| 395 |
} |
| 396 |
cmd = command_find(svs->commands, command); |
| 397 |
if (cmd == NULL) |
| 398 |
{ |
| 399 |
jsonrpc_failure_string(conn, fault_nosuch_source, "Invalid command name.", id); |
| 400 |
return 0; |
| 401 |
} |
| 402 |
|
| 403 |
//command = mowgli_node_nth_data(params, 4); |
| 404 |
|
| 405 |
memset(newparv, '\0', sizeof newparv); |
| 406 |
newparc = len; |
| 407 |
if (newparc > 20) |
| 408 |
newparc = 20; |
| 409 |
|
| 410 |
for (int i = 5; i < newparc; i++) { |
| 411 |
newparv[i-5] = mowgli_node_nth_data(params, i); |
| 412 |
} |
| 413 |
|
| 414 |
si = sourceinfo_create(); |
| 415 |
|
| 416 |
jsonrpc_sourceinfo_t *jsi = (jsonrpc_sourceinfo_t *)si; |
| 417 |
|
| 418 |
si->smu = mu; |
| 419 |
si->service = svs; |
| 420 |
si->sourcedesc = sourceip[0] != '\0' ? sourceip : NULL; |
| 421 |
si->connection = conn; |
| 422 |
si->v = &jsonrpc_vtable; |
| 423 |
si->force_language = language_find("en"); |
| 424 |
|
| 425 |
jsi->base = si; |
| 426 |
jsi->id = id; |
| 427 |
|
| 428 |
command_exec(svs, si, cmd, newparc-5, newparv); |
| 429 |
|
| 430 |
/* XXX: needs to be fixed up for restartable commands... */ |
| 431 |
if (!hd->sent_reply) |
| 432 |
{ |
| 433 |
if (hd->replybuf != NULL) |
| 434 |
jsonrpc_success_string(conn, hd->replybuf, id); |
| 435 |
else |
| 436 |
jsonrpc_failure_string(conn, fault_unimplemented, "Command did not return a result", id); |
| 437 |
} |
| 438 |
|
| 439 |
object_unref(si); |
| 440 |
|
| 441 |
return 0; |
| 442 |
} |
| 443 |
|
| 444 |
/* |
| 445 |
* atheme.privset |
| 446 |
* |
| 447 |
* JSON inputs: |
| 448 |
* authcookie, account name |
| 449 |
* |
| 450 |
* JSON outputs: |
| 451 |
* Privset for user, or error message |
| 452 |
* |
| 453 |
*/ |
| 454 |
|
| 455 |
static bool jsonrpcmethod_privset(void *conn, mowgli_list_t *params, char *id) |
| 456 |
{ |
| 457 |
myuser_t *mu; |
| 458 |
mowgli_node_t *n; |
| 459 |
|
| 460 |
char *param, *accountname, *cookie; |
| 461 |
|
| 462 |
size_t len = MOWGLI_LIST_LENGTH(params); |
| 463 |
cookie = mowgli_node_nth_data(params, 0); |
| 464 |
accountname = mowgli_node_nth_data(params, 1); |
| 465 |
|
| 466 |
MOWGLI_LIST_FOREACH(n, params->head) |
| 467 |
{ |
| 468 |
param = n->data; |
| 469 |
|
| 470 |
if (*param == '\0' || strchr(param, '\r') || strchr(param, '\n')) |
| 471 |
{ |
| 472 |
jsonrpc_failure_string(conn, fault_badparams, "Invalid authcookie for this account.", id); |
| 473 |
return 0; |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
if (len < 2) |
| 478 |
{ |
| 479 |
jsonrpc_failure_string(conn, fault_needmoreparams, "Insufficient parameters.", id); |
| 480 |
return 0; |
| 481 |
} |
| 482 |
|
| 483 |
if (*accountname != '\0' && strlen(cookie) > 1) |
| 484 |
{ |
| 485 |
if ((mu = myuser_find(accountname)) == NULL) |
| 486 |
{ |
| 487 |
jsonrpc_failure_string(conn, fault_nosuch_source, "Unknown user.", id); |
| 488 |
return 0; |
| 489 |
} |
| 490 |
|
| 491 |
if (authcookie_validate(cookie, mu) == false) |
| 492 |
{ |
| 493 |
jsonrpc_failure_string(conn, fault_badauthcookie, "Invalid authcookie for this account.", id); |
| 494 |
return 0; |
| 495 |
} |
| 496 |
} |
| 497 |
else |
| 498 |
mu = NULL; |
| 499 |
|
| 500 |
if (mu == NULL || !is_soper(mu)) |
| 501 |
{ |
| 502 |
jsonrpc_success_string(conn, "", id); |
| 503 |
return 0; |
| 504 |
} |
| 505 |
|
| 506 |
jsonrpc_success_string(conn, mu->soper->operclass->privs, id); |
| 507 |
|
| 508 |
return 0; |
| 509 |
} |
| 510 |
|
| 511 |
/* |
| 512 |
* atheme.ison |
| 513 |
* |
| 514 |
* JSON inputs: |
| 515 |
* nickname |
| 516 |
* |
| 517 |
* JSON outputs: |
| 518 |
* An object with the following properties: |
| 519 |
* online: boolean: if nickname is online |
| 520 |
* accountname: string: if nickname is authenticated, what entity they |
| 521 |
* are authed to, else '*' |
| 522 |
*/ |
| 523 |
|
| 524 |
static bool jsonrpcmethod_ison(void *conn, mowgli_list_t *params, char *id) |
| 525 |
{ |
| 526 |
user_t *u; |
| 527 |
|
| 528 |
char *param, *user; |
| 529 |
user = mowgli_node_nth_data(params, 0); |
| 530 |
|
| 531 |
size_t len = MOWGLI_LIST_LENGTH(params); |
| 532 |
mowgli_node_t *n; |
| 533 |
|
| 534 |
MOWGLI_LIST_FOREACH(n, params->head) |
| 535 |
{ |
| 536 |
param = n->data; |
| 537 |
|
| 538 |
if (*param == '\0' || strchr(param, '\r') || strchr(param, '\n')) |
| 539 |
{ |
| 540 |
jsonrpc_failure_string(conn, fault_badparams, "Invalid authcookie for this account.", id); |
| 541 |
return 0; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
if (len < 1) |
| 547 |
{ |
| 548 |
jsonrpc_failure_string(conn, fault_needmoreparams, "Insufficient parameters.", id); |
| 549 |
return 0; |
| 550 |
} |
| 551 |
|
| 552 |
u = user_find(user); |
| 553 |
if (u == NULL) |
| 554 |
{ |
| 555 |
mowgli_json_t *resultobj = mowgli_json_create_object(); |
| 556 |
mowgli_patricia_t *patricia = MOWGLI_JSON_OBJECT(resultobj); |
| 557 |
|
| 558 |
mowgli_patricia_add(patricia, "online", mowgli_json_false); |
| 559 |
mowgli_patricia_add(patricia, "accountname", mowgli_json_create_string("*")); |
| 560 |
|
| 561 |
mowgli_json_t *obj = mowgli_json_create_object(); |
| 562 |
patricia = MOWGLI_JSON_OBJECT(obj); |
| 563 |
|
| 564 |
mowgli_json_t *idobj = mowgli_json_create_string(id); |
| 565 |
|
| 566 |
mowgli_patricia_add(patricia, "result", resultobj); |
| 567 |
mowgli_patricia_add(patricia, "id", idobj); |
| 568 |
mowgli_patricia_add(patricia, "error", mowgli_json_null); |
| 569 |
|
| 570 |
mowgli_string_t *str = mowgli_string_create(); |
| 571 |
|
| 572 |
mowgli_json_serialize_to_string(obj, str, 0); |
| 573 |
|
| 574 |
jsonrpc_send_data(conn, str->str); |
| 575 |
|
| 576 |
return 0; |
| 577 |
} |
| 578 |
|
| 579 |
mowgli_json_t *resultobj = mowgli_json_create_object(); |
| 580 |
mowgli_patricia_t *patricia = MOWGLI_JSON_OBJECT(resultobj); |
| 581 |
|
| 582 |
mowgli_patricia_add(patricia, "online", mowgli_json_true); |
| 583 |
mowgli_patricia_add(patricia, "accountname", mowgli_json_create_string(u->myuser != NULL ? entity(u->myuser)->name : "*")); |
| 584 |
|
| 585 |
mowgli_json_t *obj = mowgli_json_create_object(); |
| 586 |
patricia = MOWGLI_JSON_OBJECT(obj); |
| 587 |
|
| 588 |
mowgli_json_t *idobj = mowgli_json_create_string(id); |
| 589 |
|
| 590 |
mowgli_patricia_add(patricia, "result", resultobj); |
| 591 |
mowgli_patricia_add(patricia, "id", idobj); |
| 592 |
mowgli_patricia_add(patricia, "error", mowgli_json_null); |
| 593 |
|
| 594 |
mowgli_string_t *str = mowgli_string_create(); |
| 595 |
|
| 596 |
mowgli_json_serialize_to_string(obj, str, 0); |
| 597 |
|
| 598 |
jsonrpc_send_data(conn, str->str); |
| 599 |
|
| 600 |
return 0; |
| 601 |
} |
| 602 |
|
| 603 |
/* |
| 604 |
* atheme.metadata |
| 605 |
* |
| 606 |
* JSON inputs: |
| 607 |
* entity name, UID or channel name |
| 608 |
* metadata key |
| 609 |
* |
| 610 |
* JSON outputs: |
| 611 |
* metadata value |
| 612 |
*/ |
| 613 |
|
| 614 |
|
| 615 |
static bool jsonrpcmethod_metadata(void *conn, mowgli_list_t *params, char *id) |
| 616 |
{ |
| 617 |
metadata_t *md; |
| 618 |
|
| 619 |
char *param, *name, *metadata; |
| 620 |
|
| 621 |
name = mowgli_node_nth_data(params, 0); |
| 622 |
metadata = mowgli_node_nth_data(params, 1); |
| 623 |
|
| 624 |
size_t len = MOWGLI_LIST_LENGTH(params); |
| 625 |
mowgli_node_t *n; |
| 626 |
|
| 627 |
MOWGLI_LIST_FOREACH(n, params->head) |
| 628 |
{ |
| 629 |
param = n->data; |
| 630 |
|
| 631 |
if (*param == '\0' || strchr(param, '\r') || strchr(param, '\n')) |
| 632 |
{ |
| 633 |
jsonrpc_failure_string(conn, fault_badparams, "Invalid authcookie for this account.", id); |
| 634 |
return 0; |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
if (len < 2) |
| 639 |
{ |
| 640 |
jsonrpc_failure_string(conn, fault_needmoreparams, "Insufficient parameters.", id); |
| 641 |
return 0; |
| 642 |
} |
| 643 |
|
| 644 |
if (*name == '#') |
| 645 |
{ |
| 646 |
mychan_t *mc; |
| 647 |
|
| 648 |
mc = mychan_find(name); |
| 649 |
if (mc == NULL) |
| 650 |
{ |
| 651 |
jsonrpc_failure_string(conn, fault_nosuch_source, "No channel registration was found for the provided channel name.", id); |
| 652 |
return 0; |
| 653 |
} |
| 654 |
|
| 655 |
md = metadata_find(mc, metadata); |
| 656 |
} |
| 657 |
else |
| 658 |
{ |
| 659 |
myentity_t *mt; |
| 660 |
|
| 661 |
mt = myentity_find(name); |
| 662 |
if (mt == NULL) |
| 663 |
mt = myentity_find_uid(name); |
| 664 |
|
| 665 |
if (mt == NULL) |
| 666 |
{ |
| 667 |
jsonrpc_failure_string(conn, fault_nosuch_source, "No account was found for this accountname or UID.", id); |
| 668 |
return 0; |
| 669 |
} |
| 670 |
|
| 671 |
md = metadata_find(mt, metadata); |
| 672 |
} |
| 673 |
|
| 674 |
if (md == NULL) |
| 675 |
{ |
| 676 |
jsonrpc_failure_string(conn, fault_nosuch_source, "No metadata found matching this account/channel and key.", id); |
| 677 |
return 0; |
| 678 |
} |
| 679 |
|
| 680 |
jsonrpc_success_string(conn, md->value, id); |
| 681 |
|
| 682 |
return 0; |
| 683 |
} |
| 684 |
|
| 685 |
void jsonrpc_send_data(void *conn, char *str) { |
| 686 |
struct httpddata *hd = ((connection_t *) conn)->userdata; |
| 687 |
|
| 688 |
char buf[300]; |
| 689 |
|
| 690 |
size_t len = strlen(str); |
| 691 |
|
| 692 |
snprintf(buf, sizeof buf, "HTTP/1.1 200 OK\r\n" |
| 693 |
"%s" |
| 694 |
"Server: Atheme/%s\r\n" |
| 695 |
"Content-Type: application/json\r\n" |
| 696 |
"Content-Length: %lu\r\n\r\n", |
| 697 |
hd->connection_close ? "Connection: close\r\n" : "", |
| 698 |
PACKAGE_VERSION, (unsigned long)len); |
| 699 |
|
| 700 |
sendq_add((connection_t *)conn, buf, strlen(buf)); |
| 701 |
sendq_add((connection_t *)conn, str, len); |
| 702 |
|
| 703 |
if (hd->connection_close) { |
| 704 |
sendq_add_eof((connection_t *) conn); |
| 705 |
} |
| 706 |
} |