5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2002 - 2005, 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 #include "serverincludes.h"
22 #include "server_internal.h"
25 SilcPacketStream sock; /* Connection of this query */
26 unsigned char **arg; /* Query argument */
27 SilcUInt32 *arg_lens; /* Query argument lengths */
28 SilcUInt32 *arg_types; /* Query argument types */
29 SilcUInt32 argc; /* Number of query arguments */
30 SilcUInt32 timeout; /* Max timeout for query to complete */
31 SilcUInt16 ident; /* Query command identifier */
32 } *SilcServerQueryList;
34 /* Represents an SILC ID */
37 SilcIdType id_type; /* ID type */
38 SilcUInt16 ident; /* Command identifier */
41 /* Represents one error occurred during query */
44 SilcIdType id_type; /* ID type */
45 unsigned int index : 15; /* Index to IDs */
46 unsigned int type : 2; /* 0 = take from query->ids, 1 = take
47 from args, 2 = no args in error. */
48 unsigned int error : 7; /* The actual error (SilcStatus) */
49 } *SilcServerQueryError;
51 /* Query session context */
54 char nickname[128 + 1]; /* Queried nickname, normalized */
55 char nick_server[128 + 1]; /* Queried nickname's server */
56 char *server_name; /* Queried server name, normalized */
57 char *channel_name; /* Queried channel name, normalized */
58 SilcServerQueryID ids; /* Queried IDs */
59 SilcUInt32 ids_count; /* number of queried IDs */
60 SilcUInt32 reply_count; /* Requested reply count */
61 SilcDList attrs; /* Requested Attributes in WHOIS */
63 /* Query session data */
64 SilcPacketStream router; /* Router to send our query */
65 SilcServerCommandContext cmd; /* Command context for query */
66 SilcServerQueryList querylist; /* Temporary query list context */
67 SilcServerQueryID queries; /* Ongoing queries */
68 SilcServerQueryError errors; /* Query errors */
69 SilcUInt16 querylist_count; /* Number of query lists */
70 SilcUInt16 queries_count; /* Number of ongoing queries */
71 SilcUInt16 queries_left; /* Number of ongoing queries left */
72 SilcUInt16 errors_count; /* number of errors */
73 unsigned int querycmd : 7; /* Query command (SilcCommand) */
74 unsigned int resolved : 1; /* TRUE if normal server has resolved
75 information from router */
76 unsigned int dynamic_prim : 1; /* Dynamic connection attempt to primary */
77 unsigned int dynamic_retry : 1; /* Primary returned error, send to
79 unsigned int parsed : 1; /* Set when query is parsed */
83 void silc_server_query_free(SilcServerQuery query);
84 void silc_server_query_send_error(SilcServer server,
85 SilcServerQuery query,
86 SilcStatus error, ...);
87 void silc_server_query_add_error(SilcServer server,
88 SilcServerQuery query,
92 void silc_server_query_add_error_id(SilcServer server,
93 SilcServerQuery query,
95 void *id, SilcIdType id_type);
96 void silc_server_query_send_router(SilcServer server, SilcServerQuery query);
97 void silc_server_query_send_router_reply(void *context, void *reply);
98 SilcBool silc_server_query_parse(SilcServer server, SilcServerQuery query,
100 void silc_server_query_process(SilcServer server, SilcServerQuery query,
102 void silc_server_query_resolve(SilcServer server, SilcServerQuery query,
103 SilcPacketStream sock,
104 SilcClientEntry client_entry);
105 void silc_server_query_resolve_reply(void *context, void *reply);
106 void silc_server_query_send_reply(SilcServer server,
107 SilcServerQuery query,
108 SilcClientEntry *clients,
109 SilcUInt32 clients_count,
110 SilcServerEntry *servers,
111 SilcUInt32 servers_count,
112 SilcChannelEntry *channels,
113 SilcUInt32 channels_count);
114 SilcBuffer silc_server_query_reply_attrs(SilcServer server,
115 SilcServerQuery query,
116 SilcClientEntry client_entry);
118 /* Free the query context structure and all allocated resources. */
120 void silc_server_query_free(SilcServerQuery query)
124 silc_server_command_free(query->cmd);
126 for (i = 0; i < query->queries_count; i++)
127 silc_free(query->queries[i].id);
128 silc_free(query->queries);
130 silc_free(query->server_name);
131 silc_free(query->channel_name);
133 for (i = 0; i < query->ids_count; i++)
134 silc_free(query->ids[i].id);
135 silc_free(query->ids);
138 silc_attribute_payload_list_free(query->attrs);
140 for (i = 0; i < query->errors_count; i++)
141 silc_free(query->errors[i].id);
142 silc_free(query->errors);
144 memset(query, 'F', sizeof(*query));
148 /* Send error reply indicated by the `error' to the original sender of
151 void silc_server_query_send_error(SilcServer server,
152 SilcServerQuery query,
153 SilcStatus error, ...)
156 unsigned char *data = NULL;
157 SilcUInt32 data_len = 0, data_type = 0, argc = 0;
160 data_type = va_arg(va, SilcUInt32);
163 data = va_arg(va, unsigned char *);
164 data_len = va_arg(va, SilcUInt32);
167 SILC_LOG_DEBUG(("ERROR: %s (%d)", silc_get_status_message(error), error));
169 /* Send the command reply with error */
170 silc_server_send_command_reply(server, query->cmd->sock,
171 query->querycmd, error, 0,
172 silc_command_get_ident(query->cmd->payload),
173 argc, data_type, data, data_len);
177 /* Add error to error list. Multiple errors may occur during the query
178 processing and this function can be used to add one error. The
179 `index' is the index to the command context which includes the argument
180 which caused the error, or it is the index to query->ids, depending
181 on value of `type'. If `type' is 0 the index is to query->ids, if
182 it is 1 it is index to the command context arguments, and if it is
183 2 the index is ignored and no argument is included in the error. */
185 void silc_server_query_add_error(SilcServer server,
186 SilcServerQuery query,
191 query->errors = silc_realloc(query->errors, sizeof(*query->errors) *
192 (query->errors_count + 1));
195 query->errors[query->errors_count].index = index;
196 query->errors[query->errors_count].type = type;
197 query->errors[query->errors_count].error = error;
198 query->errors[query->errors_count].id = NULL;
199 query->errors[query->errors_count].id_type = 0;
200 query->errors_count++;
203 /* Same as silc_server_query_add_error but adds the ID data to be used
204 with error sending with this error type. */
206 void silc_server_query_add_error_id(SilcServer server,
207 SilcServerQuery query,
209 void *id, SilcIdType id_type)
211 query->errors = silc_realloc(query->errors, sizeof(*query->errors) *
212 (query->errors_count + 1));
215 query->errors[query->errors_count].index = 0;
216 query->errors[query->errors_count].type = 0;
217 query->errors[query->errors_count].error = error;
218 query->errors[query->errors_count].id = silc_id_dup(id, id_type);
219 query->errors[query->errors_count].id_type = id_type;
220 query->errors_count++;
223 /* Processes query as command. The `query' is the command that is
224 being processed indicated by the `cmd'. The `query' can be one of
225 the following: SILC_COMMAND_WHOIS, SILC_COMMAND_WHOWAS or
226 SILC_COMMAND_IDENTIFY. This function handles the reply sending
227 to the entity who sent this query to us automatically. Returns
228 TRUE if the query is being processed or FALSE on error. */
230 SilcBool silc_server_query_command(SilcServer server,
231 SilcCommand querycmd,
232 SilcServerCommandContext cmd,
235 SilcServerQuery query;
237 SILC_LOG_DEBUG(("Query %s command", silc_get_command_name(querycmd)));
240 query = silc_calloc(1, sizeof(*query));
241 query->querycmd = querycmd;
242 query->cmd = silc_server_command_dup(cmd);
243 query->router = SILC_PRIMARY_ROUTE(server);
249 case SILC_COMMAND_WHOIS:
250 /* If we are normal server and query contains nickname OR query
251 doesn't contain nickname or ids BUT attributes, send it to the
253 if (server->server_type != SILC_ROUTER && !server->standalone &&
254 cmd->sock != SILC_PRIMARY_ROUTE(server) &&
255 (silc_argument_get_arg_type(cmd->args, 1, NULL) ||
256 (!silc_argument_get_arg_type(cmd->args, 1, NULL) &&
257 !silc_argument_get_arg_type(cmd->args, 4, NULL) &&
258 silc_argument_get_arg_type(cmd->args, 3, NULL)))) {
259 if (!silc_server_query_parse(server, query, TRUE))
261 silc_server_query_send_router(server, query);
266 case SILC_COMMAND_WHOWAS:
267 /* WHOWAS query is always sent to router if we are normal server */
268 if (server->server_type == SILC_SERVER && !server->standalone &&
269 cmd->sock != SILC_PRIMARY_ROUTE(server)) {
270 silc_server_query_send_router(server, query);
275 case SILC_COMMAND_IDENTIFY:
276 /* If we are normal server and query does not contain IDs, send it
277 directly to router (it contains nickname, server name or channel
279 if (server->server_type == SILC_SERVER && !server->standalone &&
280 cmd->sock != SILC_PRIMARY_ROUTE(server) &&
281 !silc_argument_get_arg_type(cmd->args, 5, NULL)) {
282 if (!silc_server_query_parse(server, query, TRUE))
284 silc_server_query_send_router(server, query);
290 SILC_LOG_ERROR(("Bad query using %d command", querycmd));
291 silc_server_query_free(query);
295 /* Now parse the request */
296 silc_server_query_parse(server, query, FALSE);
301 /* Remote server connected callback. */
303 void silc_server_query_connected(SilcServer server,
304 SilcServerEntry server_entry,
307 SilcServerQuery query = context;
310 /* Connecting failed */
311 SilcConnectionType type = (server->server_type == SILC_ROUTER ?
312 SILC_CONN_SERVER : SILC_CONN_ROUTER);
314 if (query->dynamic_prim /* && @serv != prim.host.name */ &&
315 !silc_server_num_sockets_by_remote(server, query->nick_server,
316 query->nick_server, 706, type)) {
317 /* Connection attempt to primary router failed, now try to the one
318 specified in nick@server. */
319 silc_server_create_connection(server, FALSE, TRUE, query->nick_server,
320 706, silc_server_query_connected,
322 query->dynamic_prim = FALSE;
326 /* Process the query after failed connect. This will send error back
327 because such nick was not found. */
328 SILC_LOG_DEBUG(("Process query, connecting failed"));
329 silc_server_query_process(server, query, TRUE);
333 /* Reprocess the query */
334 SILC_LOG_DEBUG(("Reprocess query after creating connection to %s",
335 server_entry->server_name));
336 query->router = server_entry->data.sconn->sock;
337 silc_server_query_command(server, query->querycmd, query->cmd, query);
340 /* Send the received query to our primary router since we could not
341 handle the query directly. We will reprocess the query after our
342 router replies back. */
344 void silc_server_query_send_router(SilcServer server, SilcServerQuery query)
347 SilcUInt16 old_ident;
349 SILC_LOG_DEBUG(("Forwarding the query to router %p for processing",
353 server->stat.commands_sent++;
355 /* Send WHOIS command to our router */
356 old_ident = silc_command_get_ident(query->cmd->payload);
357 silc_command_set_ident(query->cmd->payload, ++server->cmd_ident);
358 tmpbuf = silc_command_payload_encode_payload(query->cmd->payload);
359 silc_server_packet_send(server, query->router,
360 SILC_PACKET_COMMAND, 0,
361 tmpbuf->data, silc_buffer_len(tmpbuf));
362 silc_command_set_ident(query->cmd->payload, old_ident);
363 silc_buffer_free(tmpbuf);
365 query->resolved = TRUE;
367 /* Continue parsing the query after received reply from router */
368 silc_server_command_pending(server, query->querycmd, server->cmd_ident,
369 silc_server_query_send_router_reply, query);
372 /* Reply callback called after primary router has replied to our initial
373 sending of the query to it. We will proceed the query in this function. */
375 void silc_server_query_send_router_reply(void *context, void *reply)
377 SilcServerQuery query = context;
378 SilcServer server = query->cmd->server;
379 SilcServerCommandReplyContext cmdr = reply;
381 SILC_LOG_DEBUG(("Received reply from router to query"));
383 /* If the original command caller has gone away, just stop. */
384 if (!silc_packet_stream_is_valid(query->cmd->sock)) {
385 SILC_LOG_DEBUG(("Original command caller vanished"));
386 silc_server_query_free(query);
390 /* Check if router sent error reply */
391 if (cmdr && !silc_command_get_status(cmdr->payload, NULL, NULL)) {
393 SilcConnectionType type = (server->server_type == SILC_ROUTER ?
394 SILC_CONN_SERVER : SILC_CONN_ROUTER);
396 /* If this was nick@server query, retry to @serv if the primary router
398 if (query->nick_server[0] && !query->dynamic_retry &&
399 server->config->dynamic_server &&
400 !silc_server_num_sockets_by_remote(server, query->nick_server,
401 query->nick_server, 706, type)) {
402 SILC_LOG_DEBUG(("Retry query by connecting to %s:%d",
403 query->nick_server, 706));
404 silc_server_create_connection(server, FALSE, TRUE, query->nick_server,
405 706, silc_server_query_connected,
407 query->dynamic_retry = TRUE;
408 query->resolved = FALSE;
412 SILC_LOG_DEBUG(("Sending error to original query"));
415 server->stat.commands_sent++;
417 /* Send the same command reply payload which contains the error */
418 silc_command_set_command(cmdr->payload, query->querycmd);
419 silc_command_set_ident(cmdr->payload,
420 silc_command_get_ident(query->cmd->payload));
421 buffer = silc_command_payload_encode_payload(cmdr->payload);
422 silc_server_packet_send(server, query->cmd->sock,
423 SILC_PACKET_COMMAND_REPLY, 0,
424 buffer->data, silc_buffer_len(buffer));
425 silc_buffer_free(buffer);
426 silc_server_query_free(query);
430 /* Continue with parsing */
431 silc_server_query_parse(server, query, FALSE);
434 /* Parse the command query and start processing the queries in detail. */
436 SilcBool silc_server_query_parse(SilcServer server, SilcServerQuery query,
439 SilcServerCommandContext cmd = query->cmd;
440 SilcIDListData idata = silc_packet_get_context(cmd->sock);
442 SilcUInt32 tmp_len, argc = silc_argument_get_arg_num(cmd->args);
446 SILC_LOG_DEBUG(("Parsing %s query",
447 silc_get_command_name(query->querycmd)));
452 switch (query->querycmd) {
454 case SILC_COMMAND_WHOIS:
455 /* Get requested attributes if set */
456 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
457 if (tmp && !query->attrs && tmp_len <= SILC_ATTRIBUTE_MAX_REQUEST_LEN) {
458 query->attrs = silc_attribute_payload_parse(tmp, tmp_len);
460 /* When Requested Attributes is present we will assure that this
461 client cannot execute the WHOIS command too fast. This would be
462 same as having SILC_CF_LAG_STRICT. */
463 if (idata && idata->conn_type == SILC_CONN_CLIENT)
464 ((SilcClientEntry)idata)->fast_command = 6;
467 /* Get Client IDs if present. Take IDs always instead of nickname. */
468 tmp = silc_argument_get_arg_type(cmd->args, 4, &tmp_len);
472 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
473 if (!tmp && !query->attrs) {
474 /* No nickname, no ids and no attributes - send error */
475 silc_server_query_send_error(server, query,
476 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
477 silc_server_query_free(query);
481 /* Get the nickname@server string and parse it */
482 if (tmp && ((tmp_len > 128) ||
483 !silc_parse_userfqdn(tmp, query->nickname,
484 sizeof(query->nickname),
486 sizeof(query->nick_server)))) {
487 silc_server_query_send_error(server, query,
488 SILC_STATUS_ERR_BAD_NICKNAME, 0);
489 silc_server_query_free(query);
495 tmp = silc_identifier_check(query->nickname, strlen(query->nickname),
496 SILC_STRING_UTF8, 128, &tmp_len);
498 silc_server_query_send_error(server, query,
499 SILC_STATUS_ERR_BAD_NICKNAME, 0);
500 silc_server_query_free(query);
503 memset(query->nickname, 0, sizeof(query->nickname));
504 silc_snprintf(query->nickname, sizeof(query->nickname), "%s", tmp);
509 /* Parse the IDs included in the query */
510 query->ids = silc_calloc(argc, sizeof(*query->ids));
512 for (i = 0; i < argc; i++) {
513 tmp = silc_argument_get_arg_type(cmd->args, i + 4, &tmp_len);
517 if (!silc_id_payload_parse_id(tmp, tmp_len, &id) ||
518 id.type != SILC_ID_CLIENT) {
519 silc_server_query_add_error(server, query, 1, i + 4,
520 SILC_STATUS_ERR_BAD_CLIENT_ID);
524 /* Normal server must check whether this ID exist, and if not then
525 send the query to router, unless done so already */
526 if (server->server_type == SILC_SERVER && !query->resolved) {
527 if (!silc_idlist_find_client_by_id(server->local_list,
528 &id.u.client_id, TRUE, NULL)) {
529 if (idata->conn_type != SILC_CONN_CLIENT ||
530 !silc_idlist_find_client_by_id(server->global_list,
531 &id.u.client_id, TRUE, NULL)) {
532 silc_server_query_send_router(server, query);
533 for (i = 0; i < query->ids_count; i++)
534 silc_free(query->ids[i].id);
535 silc_free(query->ids);
537 query->ids_count = 0;
543 query->ids[query->ids_count].id = silc_id_dup(&id.u.client_id,
545 query->ids[query->ids_count].id_type = SILC_ID_CLIENT;
550 /* Get the max count of reply messages allowed */
551 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
552 if (tmp && tmp_len == sizeof(SilcUInt32))
553 SILC_GET32_MSB(query->reply_count, tmp);
556 case SILC_COMMAND_WHOWAS:
558 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
560 silc_server_query_send_error(server, query,
561 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
562 silc_server_query_free(query);
566 /* Get the nickname@server string and parse it */
568 !silc_parse_userfqdn(tmp, query->nickname, sizeof(query->nickname),
569 query->nick_server, sizeof(query->nick_server))) {
570 silc_server_query_send_error(server, query,
571 SILC_STATUS_ERR_BAD_NICKNAME, 0);
572 silc_server_query_free(query);
577 tmp = silc_identifier_check(query->nickname, strlen(query->nickname),
578 SILC_STRING_UTF8, 128, &tmp_len);
580 silc_server_query_send_error(server, query,
581 SILC_STATUS_ERR_BAD_NICKNAME, 0);
582 silc_server_query_free(query);
585 memset(query->nickname, 0, sizeof(query->nickname));
586 silc_snprintf(query->nickname, sizeof(query->nickname), "%s", tmp);
589 /* Get the max count of reply messages allowed */
590 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
591 if (tmp && tmp_len == sizeof(SilcUInt32))
592 SILC_GET32_MSB(query->reply_count, tmp);
595 case SILC_COMMAND_IDENTIFY:
596 /* Get IDs if present. Take IDs always instead of names. */
597 tmp = silc_argument_get_arg_type(cmd->args, 5, &tmp_len);
600 /* Try get nickname */
601 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
603 /* Get the nickname@server string and parse it */
605 !silc_parse_userfqdn(tmp, query->nickname,
606 sizeof(query->nickname),
608 sizeof(query->nick_server)))
609 silc_server_query_add_error(server, query, 1, 1,
610 SILC_STATUS_ERR_BAD_NICKNAME);
613 tmp = silc_identifier_check(query->nickname, strlen(query->nickname),
614 SILC_STRING_UTF8, 128, &tmp_len);
616 silc_server_query_send_error(server, query,
617 SILC_STATUS_ERR_BAD_NICKNAME, 0);
618 silc_server_query_free(query);
621 memset(query->nickname, 0, sizeof(query->nickname));
622 silc_snprintf(query->nickname, sizeof(query->nickname), "%s", tmp);
626 /* Try get server name */
627 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
629 /* Check server name */
630 tmp = silc_identifier_check(tmp, tmp_len, SILC_STRING_UTF8,
633 silc_server_query_send_error(server, query,
634 SILC_STATUS_ERR_BAD_SERVER, 0);
635 silc_server_query_free(query);
638 query->server_name = tmp;
641 /* Get channel name */
642 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
643 if (tmp && tmp_len <= 256) {
644 /* Check channel name */
645 tmp = silc_identifier_check(tmp, tmp_len, SILC_STRING_UTF8,
648 silc_server_query_send_error(server, query,
649 SILC_STATUS_ERR_BAD_CHANNEL, 0);
650 silc_server_query_free(query);
653 query->channel_name = tmp;
656 if (!query->nickname[0] && !query->server_name && !query->channel_name) {
657 silc_server_query_send_error(server, query,
658 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
659 silc_server_query_free(query);
664 /* Parse the IDs included in the query */
665 query->ids = silc_calloc(argc, sizeof(*query->ids));
667 for (i = 0; i < argc; i++) {
668 tmp = silc_argument_get_arg_type(cmd->args, i + 5, &tmp_len);
672 if (!silc_id_payload_parse_id(tmp, tmp_len, &id)) {
673 silc_server_query_add_error(server, query, 1, i + 5,
674 SILC_STATUS_ERR_BAD_CLIENT_ID);
678 /* Normal server must check whether this ID exist, and if not then
679 send the query to router, unless done so already */
680 if (server->server_type == SILC_SERVER && !query->resolved) {
681 if (id.type == SILC_ID_CLIENT) {
682 if (!silc_idlist_find_client_by_id(server->local_list,
683 &id.u.client_id, TRUE, NULL)) {
684 if (idata->conn_type != SILC_CONN_CLIENT ||
685 !silc_idlist_find_client_by_id(server->global_list,
686 &id.u.client_id, TRUE,
688 silc_server_query_send_router(server, query);
689 for (i = 0; i < query->ids_count; i++)
690 silc_free(query->ids[i].id);
691 silc_free(query->ids);
693 query->ids_count = 0;
698 /* For now all other ID's except Client ID's are explicitly
699 sent to router for resolving. */
700 silc_server_query_send_router(server, query);
701 for (i = 0; i < query->ids_count; i++)
702 silc_free(query->ids[i].id);
703 silc_free(query->ids);
705 query->ids_count = 0;
710 if (id.type == SILC_ID_CLIENT)
711 query->ids[query->ids_count].id = silc_id_dup(&id.u.client_id,
713 if (id.type == SILC_ID_SERVER)
714 query->ids[query->ids_count].id = silc_id_dup(&id.u.server_id,
716 if (id.type == SILC_ID_CHANNEL)
717 query->ids[query->ids_count].id = silc_id_dup(&id.u.channel_id,
719 query->ids[query->ids_count].id_type = id.type;
724 /* Get the max count of reply messages allowed */
725 tmp = silc_argument_get_arg_type(cmd->args, 4, &tmp_len);
726 if (tmp && tmp_len == sizeof(SilcUInt32))
727 SILC_GET32_MSB(query->reply_count, tmp);
731 query->parsed = TRUE;
734 if (!parse_only && query->nickname) {
735 switch (query->querycmd) {
736 case SILC_COMMAND_WHOIS:
737 case SILC_COMMAND_IDENTIFY:
738 /* Check server name. If we are open server and don't yet have
739 connection to remote router, create it now. */
740 if (query->nick_server[0] && server->config->dynamic_server &&
742 /* If primary router is specified, use that. Otherwise connect
743 to the server in nick@server string. */
744 SilcServerConfigRouter *router;
745 SilcConnectionType type = (server->server_type == SILC_ROUTER ?
746 SILC_CONN_SERVER : SILC_CONN_ROUTER);
748 router = silc_server_config_get_primary_router(server);
749 if (router && server->standalone) {
750 /* Create connection to primary router */
751 SILC_LOG_DEBUG(("Create dynamic connection to primary router %s:%d",
752 router->host, router->port));
753 query->dynamic_prim = TRUE;
754 silc_server_create_connection(server, FALSE, TRUE,
755 router->host, router->port,
756 silc_server_query_connected, query);
758 } else if (!silc_server_num_sockets_by_remote(server,
762 /* Create connection and handle the query after connection */
763 SILC_LOG_DEBUG(("Create dynamic connection to %s:%d",
764 query->nick_server, 706));
765 silc_server_create_connection(server, FALSE, TRUE,
766 query->nick_server, 706,
767 silc_server_query_connected, query);
774 /* Start processing the query information */
776 silc_server_query_process(server, query, TRUE);
781 /* Context for holding clients searched by public key. */
783 SilcClientEntry **clients;
784 SilcUInt32 *clients_count;
786 } *SilcServerPublicKeyUser, SilcServerPublicKeyUserStruct;
788 /* SKR find callbcak */
790 static void silc_server_query_skr_callback(SilcSKR skr,
792 SilcSKRStatus status,
796 SilcServerPublicKeyUser uc = context;
800 (*uc->clients) = silc_realloc((*uc->clients),
801 sizeof((**uc->clients)) *
802 ((*uc->clients_count) +
803 silc_dlist_count(keys)));
805 silc_dlist_start(keys);
806 while ((key = silc_dlist_get(keys)))
807 (*uc->clients)[(*uc->clients_count)++] = key->key_context;
810 silc_dlist_uninit(keys);
813 silc_skr_find_free(find);
816 /* If clients are set, limit the found clients using the attributes in
817 the query. If clients are not set, try to find some clients using
820 void silc_server_query_check_attributes(SilcServer server,
821 SilcServerQuery query,
822 SilcClientEntry **clients,
823 SilcUInt32 *clients_count) {
824 SilcClientEntry entry;
825 SilcAttributePayload attr;
826 SilcAttribute attribute;
827 SilcAttributeObjPk pk;
828 SilcPublicKey publickey, cmp_pubkey;
830 SilcBool found = FALSE, no_clients = FALSE, search_pubkey = FALSE;
833 /* If no clients were found, we only check the attributes
834 if the user wasn't searching for nickname/ids */
837 if (query->nickname[0] || query->ids_count)
841 silc_dlist_start(query->attrs);
842 while ((attr = silc_dlist_get(query->attrs)) != SILC_LIST_END) {
843 attribute = silc_attribute_get_attribute(attr);
846 case SILC_ATTRIBUTE_USER_PUBLIC_KEY:
847 SILC_LOG_DEBUG(("Finding clients by public key attribute"));
849 if (!silc_attribute_get_object(attr, &pk, sizeof(pk)))
852 if (!strcmp(pk.type, "silc-rsa"))
853 type = SILC_PKCS_SILC;
854 else if (!strcmp(pk.type, "ssh-rsa"))
855 type = SILC_PKCS_SSH2;
856 else if (!strcmp(pk.type, "x509v3-sign-rsa"))
857 type = SILC_PKCS_X509V3;
858 else if (!strcmp(pk.type, "pgp-sign-rsa"))
859 type = SILC_PKCS_OPENPGP;
863 if (!silc_pkcs_public_key_alloc(type, pk.data, pk.data_len,
869 search_pubkey = TRUE;
871 /* If no clients were set on calling this function, we just search
872 for clients, otherwise we try to limit the clients. */
874 SilcServerPublicKeyUserStruct usercontext;
877 usercontext.clients = clients;
878 usercontext.clients_count = clients_count;
879 usercontext.found = FALSE;
881 find = silc_skr_find_alloc();
885 silc_skr_find_set_public_key(find, publickey);
886 silc_skr_find_set_usage(find, SILC_SKR_USAGE_IDENTIFICATION);
887 silc_skr_find(server->repository, server->schedule,
888 find, silc_server_query_skr_callback, &usercontext);
890 if (usercontext.found == TRUE)
893 for (i = 0; i < *clients_count; i++) {
894 entry = (*clients)[i];
896 if (!entry->data.public_key)
899 if (silc_server_get_public_key_by_client(server, entry,
901 if (silc_pkcs_public_key_compare(cmp_pubkey, publickey)) {
907 (*clients)[i] = NULL;
912 silc_pkcs_public_key_free(publickey);
917 if (!found && !query->nickname[0] && !query->ids)
918 silc_server_query_add_error(server, query, 2, 0,
920 SILC_STATUS_ERR_NO_SUCH_PUBLIC_KEY :
921 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
924 /* Processes the parsed query. This does the actual finding of the
925 queried information and prepares for sending reply to the original
926 sender of the query command. */
928 void silc_server_query_process(SilcServer server, SilcServerQuery query,
931 SilcServerCommandContext cmd = query->cmd;
932 SilcIDListData idata = silc_packet_get_context(cmd->sock);
933 SilcBool check_global = FALSE;
935 SilcClientEntry *clients = NULL, client_entry;
936 SilcChannelEntry *channels = NULL;
937 SilcServerEntry *servers = NULL;
938 SilcUInt32 clients_count = 0, channels_count = 0, servers_count = 0;
942 silc_server_query_free(query);
946 SILC_LOG_DEBUG(("Processing %s query",
947 silc_get_command_name(query->querycmd)));
949 /* Check global lists if query is coming from client or we are not
950 normal server (we know global information). */
951 if (idata->conn_type == SILC_CONN_CLIENT)
953 else if (server->server_type != SILC_SERVER)
956 if (query->nickname[0]) {
957 /* Get all clients matching nickname from local list */
958 if (!silc_idlist_get_clients_by_hash(server->local_list,
960 query->nick_server[0] ?
961 query->nick_server : NULL,
963 &clients, &clients_count))
964 silc_idlist_get_clients_by_nickname(server->local_list,
966 query->nick_server[0] ?
967 query->nick_server : NULL,
968 &clients, &clients_count);
970 /* Check global list as well */
972 if (!silc_idlist_get_clients_by_hash(server->global_list,
974 query->nick_server[0] ?
975 query->nick_server : NULL,
977 &clients, &clients_count))
978 silc_idlist_get_clients_by_nickname(server->global_list,
980 query->nick_server[0] ?
981 query->nick_server : NULL,
982 &clients, &clients_count);
986 silc_server_query_add_error(server, query, 1, 1,
987 SILC_STATUS_ERR_NO_SUCH_NICK);
990 if (query->server_name) {
991 /* Find server by name */
992 entry = silc_idlist_find_server_by_name(server->local_list,
993 query->server_name, TRUE, NULL);
994 if (!entry && check_global)
995 entry = silc_idlist_find_server_by_name(server->global_list,
996 query->server_name, TRUE, NULL);
998 servers = silc_realloc(servers, sizeof(*servers) * (servers_count + 1));
999 servers[servers_count++] = (SilcServerEntry)entry;
1003 silc_server_query_add_error(server, query, 1, 2,
1004 SILC_STATUS_ERR_NO_SUCH_SERVER);
1007 if (query->channel_name) {
1008 /* Find channel by name */
1009 entry = silc_idlist_find_channel_by_name(server->local_list,
1010 query->channel_name, NULL);
1011 if (!entry && check_global)
1012 entry = silc_idlist_find_channel_by_name(server->global_list,
1013 query->channel_name, NULL);
1015 channels = silc_realloc(channels, sizeof(*channels) *
1016 (channels_count + 1));
1017 channels[channels_count++] = (SilcChannelEntry)entry;
1021 silc_server_query_add_error(server, query, 1, 3,
1022 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
1025 if (query->ids_count) {
1026 /* Find entries by the queried IDs */
1027 for (i = 0; i < query->ids_count; i++) {
1028 void *id = query->ids[i].id;
1032 switch (query->ids[i].id_type) {
1034 case SILC_ID_CLIENT:
1035 /* Get client entry */
1036 entry = silc_idlist_find_client_by_id(server->local_list,
1038 if (!entry && check_global)
1039 entry = silc_idlist_find_client_by_id(server->global_list,
1042 silc_server_query_add_error(server, query, 0, i,
1043 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
1047 clients = silc_realloc(clients, sizeof(*clients) *
1048 (clients_count + 1));
1049 clients[clients_count++] = (SilcClientEntry)entry;
1052 case SILC_ID_SERVER:
1053 /* Get server entry */
1054 entry = silc_idlist_find_server_by_id(server->local_list,
1056 if (!entry && check_global)
1057 entry = silc_idlist_find_server_by_id(server->global_list,
1060 silc_server_query_add_error(server, query, 0, i,
1061 SILC_STATUS_ERR_NO_SUCH_SERVER_ID);
1065 servers = silc_realloc(servers, sizeof(*servers) *
1066 (servers_count + 1));
1067 servers[servers_count++] = (SilcServerEntry)entry;
1070 case SILC_ID_CHANNEL:
1071 /* Get channel entry */
1072 entry = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1073 if (!entry && check_global)
1074 entry = silc_idlist_find_channel_by_id(server->global_list, id,
1077 silc_server_query_add_error(server, query, 0, i,
1078 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID);
1082 channels = silc_realloc(channels, sizeof(*channels) *
1083 (channels_count + 1));
1084 channels[channels_count++] = (SilcChannelEntry)entry;
1093 /* Check the attributes to narrow down the search by using them. */
1095 silc_server_query_check_attributes(server, query, &clients,
1098 SILC_LOG_DEBUG(("Querying %d clients", clients_count));
1099 SILC_LOG_DEBUG(("Querying %d servers", servers_count));
1100 SILC_LOG_DEBUG(("Querying %d channels", channels_count));
1102 /* If nothing was found, then just send the errors */
1103 if (!clients && !channels && !servers) {
1104 silc_server_query_send_reply(server, query, NULL, 0, NULL, 0, NULL, 0);
1108 /* If caller does not want us to resolve anything (has resolved already)
1109 then just continue with sending the reply */
1111 silc_server_query_send_reply(server, query, clients, clients_count,
1112 servers, servers_count, channels,
1116 silc_free(channels);
1120 /* Now process all found information and if necessary do some more
1122 switch (query->querycmd) {
1124 case SILC_COMMAND_WHOIS:
1125 for (i = 0; i < clients_count; i++) {
1126 client_entry = clients[i];
1128 /* Check if cannot query this anyway, so take next one */
1129 if (!client_entry ||
1130 !(client_entry->data.status & SILC_IDLIST_STATUS_REGISTERED))
1133 /* If Requested Attributes is set then we always resolve the client
1134 information, if not then check whether the entry is complete or not
1135 and decide whether we need to resolve or not. */
1136 if (!query->attrs) {
1138 /* Even if nickname and stuff are present, we may need to resolve
1140 if (client_entry->nickname && client_entry->username &&
1141 client_entry->userinfo) {
1142 /* Check if cannot query this anyway, so take next one */
1143 if (!client_entry->router)
1146 /* If we are router, client is local to us, or client is on channel
1147 we do not need to resolve the client information. */
1148 if (server->server_type != SILC_SERVER || SILC_IS_LOCAL(client_entry)
1149 || silc_hash_table_count(client_entry->channels) ||
1155 /* Remove the NOATTR status periodically */
1156 if (client_entry->data.status & SILC_IDLIST_STATUS_NOATTR &&
1157 client_entry->updated + 600 < time(NULL))
1158 client_entry->data.status &= ~SILC_IDLIST_STATUS_NOATTR;
1160 /* When requested attributes is present and local client is detached
1161 we cannot send the command to the client, we'll reply on behalf of
1162 the client instead. */
1163 if (query->attrs && SILC_IS_LOCAL(client_entry) &&
1164 (client_entry->mode & SILC_UMODE_DETACHED ||
1165 client_entry->data.status & SILC_IDLIST_STATUS_NOATTR))
1168 /* If attributes are present in query, and in the entry and we have
1169 done resolvings already we don't need to resolve anymore */
1170 if (query->resolved && query->attrs && client_entry->attrs)
1173 /* Resolve the detailed client information. If client is local we
1174 know that attributes were present and we will resolve directly
1175 from the client. Otherwise resolve from client's owner. */
1176 silc_server_query_resolve(server, query,
1177 (SILC_IS_LOCAL(client_entry) ?
1178 client_entry->connection :
1179 client_entry->router->connection),
1184 case SILC_COMMAND_WHOWAS:
1185 for (i = 0; i < clients_count; i++) {
1186 client_entry = clients[i];
1188 /* Check if cannot query this anyway, so take next one */
1189 if (!client_entry || !client_entry->router ||
1190 client_entry->data.status & SILC_IDLIST_STATUS_REGISTERED)
1193 /* If both nickname and username are present no resolving is needed */
1194 if (client_entry->nickname && client_entry->username)
1197 /* Resolve the detailed client information */
1198 silc_server_query_resolve(server, query,
1199 client_entry->router->connection,
1204 case SILC_COMMAND_IDENTIFY:
1205 for (i = 0; i < clients_count; i++) {
1206 client_entry = clients[i];
1208 /* Check if cannot query this anyway, so take next one */
1209 if (!client_entry || !client_entry->router ||
1210 !(client_entry->data.status & SILC_IDLIST_STATUS_REGISTERED))
1213 /* Even if nickname is present, we may need to resolve the entry */
1214 if (client_entry->nickname) {
1216 /* If we are router, client is local to us, or client is on channel
1217 we do not need to resolve the client information. */
1218 if (server->server_type != SILC_SERVER || SILC_IS_LOCAL(client_entry)
1219 || silc_hash_table_count(client_entry->channels) ||
1224 /* Resolve the detailed client information */
1225 silc_server_query_resolve(server, query,
1226 client_entry->router->connection,
1232 if (!query->queries_count)
1233 /* If we didn't have to do any resolving, continue with sending the
1234 command reply to the original sender. */
1235 silc_server_query_send_reply(server, query, clients, clients_count,
1236 servers, servers_count, channels,
1239 /* Now actually send the resolvings we gathered earlier */
1240 silc_server_query_resolve(server, query, NULL, NULL);
1244 silc_free(channels);
1247 /* Resolve the detailed information for the `client_entry'. Only client
1248 information needs to be resolved for being incomplete. Each incomplete
1249 client entry calls this function to do the resolving. */
1251 void silc_server_query_resolve(SilcServer server, SilcServerQuery query,
1252 SilcPacketStream sock,
1253 SilcClientEntry client_entry)
1255 SilcServerCommandContext cmd = query->cmd;
1256 SilcServerQueryList r = NULL;
1263 if (!sock && client_entry)
1266 /* If arguments are NULL we will now actually send the resolvings
1267 that earlier has been gathered by calling this function. */
1268 if (!sock && !client_entry) {
1271 SILC_LOG_DEBUG(("Sending the resolvings"));
1273 /* WHOWAS resolving has been done at the same time this function
1274 was called to add the resolving for WHOWAS, so just return. */
1275 if (query->querycmd == SILC_COMMAND_WHOWAS)
1278 for (i = 0; i < query->querylist_count; i++) {
1279 r = &query->querylist[i];
1281 /* If Requested Attributes were present put them to this resolving */
1282 if (query->attrs && query->querycmd == SILC_COMMAND_WHOIS) {
1284 r->arg = silc_realloc(r->arg, sizeof(*r->arg) * len);
1285 r->arg_lens = silc_realloc(r->arg_lens, sizeof(*r->arg_lens) * len);
1286 r->arg_types = silc_realloc(r->arg_types, sizeof(*r->arg_types) * len);
1288 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
1290 r->arg[r->argc] = silc_memdup(tmp, len);
1291 r->arg_lens[r->argc] = len;
1292 r->arg_types[r->argc] = 3;
1297 server->stat.commands_sent++;
1299 /* Send WHOIS command */
1300 res_cmd = silc_command_payload_encode(SILC_COMMAND_WHOIS,
1301 r->argc, r->arg, r->arg_lens,
1302 r->arg_types, r->ident);
1303 silc_server_packet_send(server, r->sock, SILC_PACKET_COMMAND, 0,
1304 res_cmd->data, silc_buffer_len(res_cmd));
1305 silc_buffer_free(res_cmd);
1307 /* Reprocess this packet after received reply */
1308 if (silc_server_command_pending_timed(server, SILC_COMMAND_WHOIS,
1310 silc_server_query_resolve_reply,
1312 query->queries_left++;
1315 /* Cleanup this temporary context */
1316 for (i = 0; i < query->querylist_count; i++) {
1318 for (k = 0; k < query->querylist[i].argc; k++)
1319 silc_free(query->querylist[i].arg[k]);
1320 silc_free(query->querylist[i].arg);
1321 silc_free(query->querylist[i].arg_lens);
1322 silc_free(query->querylist[i].arg_types);
1324 silc_free(query->querylist);
1325 query->querylist = NULL;
1326 query->querylist_count = 0;
1330 SILC_LOG_DEBUG(("Resolving client information"));
1332 if (client_entry->data.status & SILC_IDLIST_STATUS_RESOLVING) {
1333 /* The entry is being resolved by some other external query already.
1334 Attach to that query instead of resolving again. */
1335 ident = client_entry->resolve_cmd_ident;
1336 if (silc_server_command_pending(server, SILC_COMMAND_NONE, ident,
1337 silc_server_query_resolve_reply, query))
1338 query->queries_left++;
1340 /* This entry will be resolved */
1341 ident = ++server->cmd_ident;
1343 switch (query->querycmd) {
1345 case SILC_COMMAND_WHOIS:
1346 case SILC_COMMAND_IDENTIFY:
1347 /* Take existing query context if exist for this connection */
1348 for (i = 0; i < query->querylist_count; i++)
1349 if (query->querylist[i].sock == sock) {
1350 r = &query->querylist[i];
1355 /* Allocate new temp query list context */
1356 query->querylist = silc_realloc(query->querylist,
1357 sizeof(*query->querylist) *
1358 (query->querylist_count + 1));
1359 r = &query->querylist[query->querylist_count];
1360 query->querylist_count++;
1361 memset(r, 0, sizeof(*r));
1364 if (SILC_IS_LOCAL(client_entry))
1369 r->arg = silc_realloc(r->arg, sizeof(*r->arg) * len);
1370 r->arg_lens = silc_realloc(r->arg_lens, sizeof(*r->arg_lens) * len);
1371 r->arg_types = silc_realloc(r->arg_types, sizeof(*r->arg_types) * len);
1373 /* Add the client entry to be resolved */
1374 idp = silc_id_payload_encode(client_entry->id, SILC_ID_CLIENT);
1375 r->arg[r->argc] = silc_memdup(idp->data, silc_buffer_len(idp));
1376 r->arg_lens[r->argc] = silc_buffer_len(idp);
1377 r->arg_types[r->argc] = r->argc + 4;
1379 silc_buffer_free(idp);
1383 case SILC_COMMAND_WHOWAS:
1384 /* We must send WHOWAS command since it's the only the way of
1385 resolving clients that are not present in the network anymore. */
1386 silc_server_send_command(server, sock, query->querycmd, ident, 1,
1387 1, query->nickname, strlen(query->nickname));
1388 if (silc_server_command_pending(server, query->querycmd, ident,
1389 silc_server_query_resolve_reply, query))
1390 query->queries_left++;
1395 /* Mark the entry as being resolved */
1396 client_entry->data.status |= SILC_IDLIST_STATUS_RESOLVING;
1397 client_entry->data.status &= ~SILC_IDLIST_STATUS_RESOLVED;
1398 client_entry->resolve_cmd_ident = ident;
1399 client_entry->updated = time(NULL);
1401 /* Save the queried ID, which we will reprocess after we get this and
1402 all other queries back. */
1403 query->queries = silc_realloc(query->queries, sizeof(*query->queries) *
1404 (query->queries_count + 1));
1405 if (query->queries) {
1406 i = query->queries_count;
1407 query->queries[i].id = silc_id_dup(client_entry->id, SILC_ID_CLIENT);
1408 query->queries[i].id_type = SILC_ID_CLIENT;
1409 query->queries[i].ident = ident;
1410 query->queries_count++;
1414 /* Reply callback called after one resolving has been completed. If
1415 all resolvings has been received then we will continue with sending
1416 the command reply to the original sender of the query. */
1418 void silc_server_query_resolve_reply(void *context, void *reply)
1420 SilcServerQuery query = context;
1421 SilcServer server = query->cmd->server;
1422 SilcServerCommandReplyContext cmdr = reply;
1423 SilcUInt16 ident = cmdr->ident;
1424 SilcStatus error = SILC_STATUS_OK;
1425 SilcServerQueryID id = NULL;
1426 SilcClientEntry client_entry;
1429 /* One less query left */
1430 query->queries_left--;
1432 silc_command_get_status(cmdr->payload, NULL, &error);
1433 SILC_LOG_DEBUG(("Received reply to resolving (%d left) (status=%d)",
1434 query->queries_left, error));
1436 /* If no error then skip to other stuff */
1437 if (error == SILC_STATUS_OK)
1440 /* Error occurred during resolving */
1442 /* Find the resolved client ID */
1443 for (i = 0; i < query->queries_count; i++) {
1444 if (query->queries[i].ident != ident)
1447 id = &query->queries[i];
1449 if (error == SILC_STATUS_ERR_TIMEDOUT) {
1451 /* If timeout occurred for local entry when resolving attributes
1452 mark that this client doesn't support attributes in WHOIS. This
1453 assures we won't send the request again to the client. */
1454 if (query->querycmd == SILC_COMMAND_WHOIS && query->attrs) {
1455 client_entry = silc_idlist_find_client_by_id(server->local_list,
1456 id->id, TRUE, NULL);
1457 SILC_LOG_DEBUG(("Client %s does not support Requested Attributes",
1458 silc_id_render(id->id, SILC_ID_CLIENT)));
1459 if (client_entry && SILC_IS_LOCAL(client_entry)) {
1460 client_entry->data.status |= SILC_IDLIST_STATUS_NOATTR;
1461 client_entry->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
1466 /* Remove the RESOLVING status from the client entry */
1467 if (query->querycmd != SILC_COMMAND_WHOWAS) {
1468 client_entry = silc_idlist_find_client_by_id(server->local_list,
1469 id->id, TRUE, NULL);
1471 client_entry = silc_idlist_find_client_by_id(server->global_list,
1472 id->id, TRUE, NULL);
1474 client_entry->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
1481 /* If there are queries left then wait for them */
1482 if (query->queries_left)
1485 SILC_LOG_DEBUG(("Reprocess the query"));
1487 /* If the original command caller has gone away, just stop. */
1488 if (!silc_packet_stream_is_valid(query->cmd->sock)) {
1489 SILC_LOG_DEBUG(("Original command caller vanished"));
1490 silc_server_query_free(query);
1494 /* We have received all queries. Now re-search all information required
1495 to complete this query. Reason we cannot save the values found in
1496 the first search is that SilcClientEntry, SilcServerEntry and
1497 SilcChannelEntry pointers may become invalid while we were waiting
1498 for these resolvings. */
1499 silc_server_query_process(server, query, FALSE);
1502 /* Send the reply to the original query. If arguments are NULL then this
1503 sends only the errors that has occurred during the processing of the
1504 query. This sends the errors always after sending all the found
1505 information. The query is over after this function returns and the
1506 `query' will become invalid. This is called only after all informations
1507 has been resolved. This means that if something is not found or is
1508 incomplete in this function we were unable to resolve the information
1509 or it does not exist at all. */
1511 void silc_server_query_send_reply(SilcServer server,
1512 SilcServerQuery query,
1513 SilcClientEntry *clients,
1514 SilcUInt32 clients_count,
1515 SilcServerEntry *servers,
1516 SilcUInt32 servers_count,
1517 SilcChannelEntry *channels,
1518 SilcUInt32 channels_count)
1520 SilcServerCommandContext cmd = query->cmd;
1521 SilcIDListData idata = silc_packet_get_context(cmd->sock);
1522 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1528 int i, k, valid_count;
1529 char nh[384], uh[384];
1530 SilcBool sent_reply = FALSE;
1532 SILC_LOG_DEBUG(("Sending reply to query"));
1533 SILC_LOG_DEBUG(("Sending %d clients", clients_count));
1534 SILC_LOG_DEBUG(("Sending %d servers", servers_count));
1535 SILC_LOG_DEBUG(("Sending %d channels", channels_count));
1536 SILC_LOG_DEBUG(("Sending %d errors", query->errors_count));
1538 status = SILC_STATUS_OK;
1541 if (clients_count) {
1542 SilcClientEntry entry;
1543 SilcPacketStream hsock;
1545 /* Mark all invalid entries */
1546 for (i = 0, valid_count = 0; i < clients_count; i++) {
1551 switch (query->querycmd) {
1552 case SILC_COMMAND_WHOIS:
1553 if (!entry->nickname || !entry->username || !entry->userinfo ||
1554 !(entry->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
1555 /* When querying by ID, every "unfound" entry must cause error */
1557 silc_server_query_add_error_id(server, query,
1558 SILC_STATUS_ERR_TIMEDOUT,
1559 entry->id, SILC_ID_CLIENT);
1565 case SILC_COMMAND_IDENTIFY:
1566 if (!entry->nickname ||
1567 !(entry->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
1568 /* When querying by ID, every "unfound" entry must cause error */
1570 silc_server_query_add_error_id(server, query,
1571 SILC_STATUS_ERR_TIMEDOUT,
1572 entry->id, SILC_ID_CLIENT);
1578 case SILC_COMMAND_WHOWAS:
1579 if (!entry->nickname || !entry->username ||
1580 entry->data.status & SILC_IDLIST_STATUS_REGISTERED) {
1589 /* Start processing found clients */
1590 status = SILC_STATUS_OK;
1591 if (valid_count > 1)
1592 status = SILC_STATUS_LIST_START;
1594 /* Now do the sending of valid entries */
1596 for (i = 0; i < clients_count && valid_count; i++) {
1602 status = SILC_STATUS_LIST_ITEM;
1603 if (valid_count > 1 && k == valid_count - 1
1604 && !servers_count && !channels_count && !query->errors_count)
1605 status = SILC_STATUS_LIST_END;
1606 if (query->reply_count && k - 1 == query->reply_count)
1607 status = SILC_STATUS_LIST_END;
1609 SILC_LOG_DEBUG(("%s: client %s",
1610 (status == SILC_STATUS_OK ? " OK" :
1611 status == SILC_STATUS_LIST_START ? "START" :
1612 status == SILC_STATUS_LIST_ITEM ? " ITEM" :
1613 status == SILC_STATUS_LIST_END ? " END" :
1614 " : "), entry->nickname));
1616 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1617 memset(nh, 0, sizeof(nh));
1619 silc_strncat(nh, sizeof(nh), entry->nickname, strlen(entry->nickname));
1620 if (!strchr(entry->nickname, '@')) {
1621 silc_strncat(nh, sizeof(nh), "@", 1);
1622 if (entry->servername) {
1623 silc_strncat(nh, sizeof(nh), entry->servername,
1624 strlen(entry->servername));
1626 len = entry->router ? strlen(entry->router->server_name) :
1627 strlen(server->server_name);
1628 silc_strncat(nh, sizeof(nh), entry->router ?
1629 entry->router->server_name :
1630 server->server_name, len);
1634 switch (query->querycmd) {
1636 case SILC_COMMAND_WHOIS:
1638 unsigned char idle[4], mode[4];
1639 unsigned char *fingerprint, fempty[20], *attrs = NULL;
1640 SilcBuffer channels, umode_list = NULL, tmpattrs = NULL;
1642 memset(fempty, 0, sizeof(fempty));
1643 memset(idle, 0, sizeof(idle));
1644 memset(uh, 0, sizeof(uh));
1646 silc_strncat(uh, sizeof(uh), entry->username,
1647 strlen(entry->username));
1648 if (!strchr(entry->username, '@') && entry->connection) {
1649 hsock = entry->connection;
1650 silc_strncat(uh, sizeof(uh), "@", 1);
1651 silc_socket_stream_get_info(silc_packet_stream_get_stream(hsock),
1652 NULL, (const char **)&tmp2, NULL, NULL);
1653 silc_strncat(uh, sizeof(uh), tmp2, strlen(tmp2));
1656 if (idata->conn_type == SILC_CONN_CLIENT)
1658 silc_server_get_client_channel_list(server, entry, FALSE,
1659 FALSE, &umode_list);
1662 silc_server_get_client_channel_list(server, entry, TRUE,
1665 if (memcmp(entry->data.fingerprint, fempty, sizeof(fempty)))
1666 fingerprint = entry->data.fingerprint;
1670 SILC_PUT32_MSB(entry->mode, mode);
1671 if (entry->connection)
1672 SILC_PUT32_MSB((time(NULL) - entry->data.last_receive), idle);
1674 /* If Requested Attribute were present, and we do not have the
1675 attributes we will reply to them on behalf of the client. */
1678 if (!entry->attrs && SILC_IS_LOCAL(entry)) {
1679 tmpattrs = silc_server_query_reply_attrs(server, query, entry);
1680 entry->attrs = silc_buffer_steal(tmpattrs, &len);
1681 entry->attrs_len = len;
1682 silc_buffer_free(tmpattrs);
1684 attrs = entry->attrs;
1685 len = entry->attrs_len;
1688 /* Send command reply */
1689 silc_server_send_command_reply(server, cmd->sock, query->querycmd,
1690 status, 0, ident, 10,
1691 2, idp->data, silc_buffer_len(idp),
1695 strlen(entry->userinfo),
1696 6, channels ? channels->data : NULL,
1697 channels ? silc_buffer_len(channels)
1702 fingerprint ? 20 : 0,
1703 10, umode_list ? umode_list->data :
1705 silc_buffer_len(umode_list) :
1710 /* For now we always delete Requested Attributes, unless the client
1711 is detached, in which case we don't want to reconstruct the
1712 same data everytime */
1713 if (!(entry->mode & SILC_UMODE_DETACHED) &&
1714 !(entry->data.status & SILC_IDLIST_STATUS_NOATTR)) {
1715 silc_free(entry->attrs);
1716 entry->attrs = NULL;
1720 silc_buffer_free(channels);
1722 silc_buffer_free(umode_list);
1728 case SILC_COMMAND_IDENTIFY:
1729 if (!entry->username) {
1730 silc_server_send_command_reply(server, cmd->sock, query->querycmd,
1731 status, 0, ident, 2,
1732 2, idp->data, silc_buffer_len(idp),
1736 memset(uh, 0, sizeof(uh));
1737 silc_strncat(uh, sizeof(uh), entry->username,
1738 strlen(entry->username));
1739 if (!strchr(entry->username, '@') && entry->connection) {
1740 hsock = entry->connection;
1741 silc_strncat(uh, sizeof(uh), "@", 1);
1742 silc_socket_stream_get_info(silc_packet_stream_get_stream(hsock),
1743 NULL, (const char **)&tmp2,
1745 silc_strncat(uh, sizeof(uh), tmp2, strlen(tmp2));
1748 silc_server_send_command_reply(server, cmd->sock, query->querycmd,
1749 status, 0, ident, 3,
1750 2, idp->data, silc_buffer_len(idp),
1757 case SILC_COMMAND_WHOWAS:
1758 memset(uh, 0, sizeof(uh));
1759 silc_strncat(uh, sizeof(uh), entry->username, strlen(entry->username));
1760 if (!strchr(entry->username, '@'))
1761 silc_strncat(uh, sizeof(uh), "@-private-", 10);
1763 /* Send command reply */
1764 silc_server_send_command_reply(server, cmd->sock, query->querycmd,
1765 status, 0, ident, 4,
1766 2, idp->data, silc_buffer_len(idp),
1771 strlen(entry->userinfo) : 0);
1776 silc_buffer_free(idp);
1778 if (status == SILC_STATUS_LIST_END)
1784 /* Not one valid entry was found, send error. If nickname was used
1785 in query send error based on that, otherwise the query->errors
1786 already includes proper errors. */
1787 if (query->nickname[0] || (!query->ids && query->attrs))
1788 silc_server_query_add_error(server, query, 1, 1,
1789 SILC_STATUS_ERR_NO_SUCH_NICK);
1791 /* Make sure some error is sent */
1792 if (!query->errors_count && !servers_count && !channels_count)
1793 silc_server_query_add_error(server, query, 2, 0,
1794 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1799 if (query->querycmd == SILC_COMMAND_IDENTIFY && servers_count) {
1800 SilcServerEntry entry;
1802 if (status == SILC_STATUS_OK && servers_count > 1)
1803 status = SILC_STATUS_LIST_START;
1806 for (i = 0; i < servers_count; i++) {
1810 status = SILC_STATUS_LIST_ITEM;
1811 if (servers_count == 1 && status != SILC_STATUS_OK && !channels_count &&
1812 !query->errors_count)
1813 status = SILC_STATUS_LIST_END;
1814 if (servers_count > 1 && k == servers_count - 1 && !channels_count &&
1815 !query->errors_count)
1816 status = SILC_STATUS_LIST_END;
1817 if (query->reply_count && k - 1 == query->reply_count)
1818 status = SILC_STATUS_LIST_END;
1820 SILC_LOG_DEBUG(("%s: server %s",
1821 (status == SILC_STATUS_OK ? " OK" :
1822 status == SILC_STATUS_LIST_START ? "START" :
1823 status == SILC_STATUS_LIST_ITEM ? " ITEM" :
1824 status == SILC_STATUS_LIST_END ? " END" :
1826 entry->server_name ? entry->server_name : ""));
1828 /* Send command reply */
1829 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
1830 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_IDENTIFY,
1831 status, 0, ident, 2,
1832 2, idp->data, silc_buffer_len(idp),
1833 3, entry->server_name,
1834 entry->server_name ?
1835 strlen(entry->server_name) : 0);
1836 silc_buffer_free(idp);
1839 if (status == SILC_STATUS_LIST_END)
1846 if (query->querycmd == SILC_COMMAND_IDENTIFY && channels_count) {
1847 SilcChannelEntry entry;
1849 if (status == SILC_STATUS_OK && channels_count > 1)
1850 status = SILC_STATUS_LIST_START;
1853 for (i = 0; i < channels_count; i++) {
1854 entry = channels[i];
1857 status = SILC_STATUS_LIST_ITEM;
1858 if (channels_count == 1 && status != SILC_STATUS_OK &&
1859 !query->errors_count)
1860 status = SILC_STATUS_LIST_END;
1861 if (channels_count > 1 && k == channels_count - 1 &&
1862 !query->errors_count)
1863 status = SILC_STATUS_LIST_END;
1864 if (query->reply_count && k - 1 == query->reply_count)
1865 status = SILC_STATUS_LIST_END;
1867 SILC_LOG_DEBUG(("%s: channel %s",
1868 (status == SILC_STATUS_OK ? " OK" :
1869 status == SILC_STATUS_LIST_START ? "START" :
1870 status == SILC_STATUS_LIST_ITEM ? " ITEM" :
1871 status == SILC_STATUS_LIST_END ? " END" :
1873 entry->channel_name ? entry->channel_name : ""));
1875 /* Send command reply */
1876 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1877 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_IDENTIFY,
1878 status, 0, ident, 2,
1879 2, idp->data, silc_buffer_len(idp),
1880 3, entry->channel_name,
1881 entry->channel_name ?
1882 strlen(entry->channel_name) : 0);
1883 silc_buffer_free(idp);
1886 if (status == SILC_STATUS_LIST_END)
1893 if (query->errors_count) {
1896 if (status == SILC_STATUS_OK && query->errors_count > 1)
1897 status = SILC_STATUS_LIST_START;
1900 for (i = 0; i < query->errors_count; i++) {
1903 /* Take error argument */
1904 if (query->errors[i].type == 1) {
1905 /* Take from sent arguments */
1907 tmp = silc_argument_get_arg_type(cmd->args,
1908 query->errors[i].index, &len);
1910 } else if (query->errors[i].type == 2) {
1915 } else if (!query->errors[i].id) {
1916 /* Take from query->ids */
1918 silc_id_payload_encode(query->ids[query->errors[i].index].id,
1919 query->ids[query->errors[k].index].id_type);
1921 len = silc_buffer_len(idp);
1924 /* Take added ID. */
1925 idp = silc_id_payload_encode(query->errors[i].id,
1926 query->errors[k].id_type);
1928 len = silc_buffer_len(idp);
1933 status = SILC_STATUS_LIST_ITEM;
1934 if (query->errors_count == 1 && status != SILC_STATUS_OK)
1935 status = SILC_STATUS_LIST_END;
1936 if (query->errors_count > 1 && k == query->errors_count - 1)
1937 status = SILC_STATUS_LIST_END;
1938 if (query->reply_count && k - 1 == query->reply_count)
1939 status = SILC_STATUS_LIST_END;
1941 SILC_LOG_DEBUG(("%s: ERROR: %s (%d)",
1942 (status == SILC_STATUS_OK ? " OK" :
1943 status == SILC_STATUS_LIST_START ? "START" :
1944 status == SILC_STATUS_LIST_ITEM ? " ITEM" :
1945 status == SILC_STATUS_LIST_END ? " END" :
1947 silc_get_status_message(query->errors[i].error),
1948 query->errors[i].error));
1950 #if 1 /* XXX Backwards compatibility. Remove in 1.0. */
1951 if (query->errors[i].error == SILC_STATUS_ERR_NO_SUCH_NICK)
1953 silc_server_send_command_reply(server, cmd->sock, query->querycmd,
1954 (status == SILC_STATUS_OK ?
1955 query->errors[i].error : status),
1956 (status == SILC_STATUS_OK ?
1957 0 : query->errors[i].error), ident, 2,
1963 silc_server_send_command_reply(server, cmd->sock, query->querycmd,
1964 (status == SILC_STATUS_OK ?
1965 query->errors[i].error : status),
1966 (status == SILC_STATUS_OK ?
1967 0 : query->errors[i].error), ident, 1,
1970 silc_buffer_free(idp);
1973 if (status == SILC_STATUS_LIST_END)
1980 SILC_LOG_ERROR(("BUG: Query did not send anything"));
1983 silc_server_query_free(query);
1986 /* This routine is used to reply to Requested Attributes in WHOIS on behalf
1987 of the client since we were unable to resolve them from the client.
1988 Either client does not support Requested Attributes or isn't replying
1989 to them like it should. */
1991 SilcBuffer silc_server_query_reply_attrs(SilcServer server,
1992 SilcServerQuery query,
1993 SilcClientEntry client_entry)
1995 SilcBuffer buffer = NULL;
1996 SilcAttribute attribute;
1997 SilcAttributePayload attr;
1998 SilcAttributeObjPk pk;
1999 SilcAttributeObjService service;
2001 unsigned char sign[2048 + 1];
2002 SilcUInt32 sign_len;
2004 SILC_LOG_DEBUG(("Constructing Requested Attributes"));
2006 /* Go through all requested attributes */
2007 silc_dlist_start(query->attrs);
2008 while ((attr = silc_dlist_get(query->attrs)) != SILC_LIST_END) {
2009 attribute = silc_attribute_get_attribute(attr);
2010 switch (attribute) {
2012 case SILC_ATTRIBUTE_SERVICE:
2013 /* Put SERVICE. Put only SILC service. */
2014 memset(&service, 0, sizeof(service));
2015 service.port = (server->config->server_info->primary ?
2016 server->config->server_info->primary->port : SILC_PORT);
2017 silc_strncat(service.address, sizeof(service.address),
2018 server->server_name, strlen(server->server_name));
2019 service.status = !(client_entry->mode & SILC_UMODE_DETACHED);
2020 if (client_entry->connection)
2021 service.idle = time(NULL) - client_entry->data.last_receive;
2022 buffer = silc_attribute_payload_encode(buffer, attribute,
2023 SILC_ATTRIBUTE_FLAG_VALID,
2024 &service, sizeof(service));
2029 case SILC_ATTRIBUTE_STATUS_MOOD:
2030 /* Put STATUS_MOOD */
2031 buffer = silc_attribute_payload_encode(buffer, attribute,
2032 SILC_ATTRIBUTE_FLAG_VALID,
2034 SILC_ATTRIBUTE_MOOD_NORMAL,
2035 sizeof(SilcUInt32));
2040 case SILC_ATTRIBUTE_STATUS_FREETEXT:
2041 /* Put STATUS_FREETEXT. We just tell in the message that we are
2042 replying on behalf of the client. */
2044 "This information was provided by the server on behalf of the user";
2045 buffer = silc_attribute_payload_encode(buffer, attribute,
2046 SILC_ATTRIBUTE_FLAG_VALID,
2052 case SILC_ATTRIBUTE_PREFERRED_CONTACT:
2053 /* Put PREFERRED_CONTACT */
2054 buffer = silc_attribute_payload_encode(buffer, attribute,
2055 SILC_ATTRIBUTE_FLAG_VALID,
2057 SILC_ATTRIBUTE_CONTACT_CHAT,
2058 sizeof(SilcUInt32));
2063 case SILC_ATTRIBUTE_USER_PUBLIC_KEY:
2064 /* Put USER_PUBLIC_KEY */
2065 if (client_entry->data.public_key) {
2066 pk.type = "silc-rsa";
2067 pk.data = silc_pkcs_public_key_encode(client_entry->data.public_key,
2069 buffer = silc_attribute_payload_encode(buffer, attribute, pk.data ?
2070 SILC_ATTRIBUTE_FLAG_VALID :
2071 SILC_ATTRIBUTE_FLAG_INVALID,
2079 /* No public key available */
2080 buffer = silc_attribute_payload_encode(buffer, attribute,
2081 SILC_ATTRIBUTE_FLAG_INVALID,
2088 /* Ignore SERVER_PUBLIC_KEY since we are going to put it anyway later */
2089 if (attribute == SILC_ATTRIBUTE_SERVER_PUBLIC_KEY ||
2090 attribute == SILC_ATTRIBUTE_SERVER_DIGITAL_SIGNATURE)
2093 /* For other attributes we cannot reply so mark it invalid */
2094 buffer = silc_attribute_payload_encode(buffer, attribute,
2095 SILC_ATTRIBUTE_FLAG_INVALID,
2103 /* Always put our public key. This assures that we send at least
2104 something valid back always. */
2105 pk.type = "silc-rsa";
2106 pk.data = silc_pkcs_public_key_encode(server->public_key, &pk.data_len);
2107 buffer = silc_attribute_payload_encode(buffer,
2108 SILC_ATTRIBUTE_SERVER_PUBLIC_KEY,
2109 pk.data ? SILC_ATTRIBUTE_FLAG_VALID :
2110 SILC_ATTRIBUTE_FLAG_INVALID,
2116 /* Finally compute the digital signature of all the data we provided
2117 as an indication that we provided rightfull information, and this
2118 also authenticates our public key. */
2119 if (silc_pkcs_private_key_get_len(server->private_key) / 8 <=
2121 silc_pkcs_sign(server->private_key, buffer->data,
2122 silc_buffer_len(buffer), sign, sizeof(sign), &sign_len,
2123 TRUE, server->sha1hash)) {
2126 pk.data_len = sign_len;
2128 silc_attribute_payload_encode(buffer,
2129 SILC_ATTRIBUTE_SERVER_DIGITAL_SIGNATURE,
2130 SILC_ATTRIBUTE_FLAG_VALID,
2139 /* Find client by the Client ID indicated by the `client_id', and if not
2140 found then query it by using WHOIS command. The client information
2141 is also resolved if the cached information is incomplete or if the
2142 `always_resolve' is set to TRUE. The indication whether requested
2143 client was being resolved is saved into `resolved'. If the client
2144 is not being resolved its entry is returned by this function. NULL
2145 is returned if client is resolved. */
2147 SilcClientEntry silc_server_query_client(SilcServer server,
2148 const SilcClientID *client_id,
2149 SilcBool always_resolve,
2152 SilcClientEntry client;
2154 SILC_LOG_DEBUG(("Resolving client by client ID"));
2159 client = silc_idlist_find_client_by_id(server->local_list,
2160 (SilcClientID *)client_id,
2163 client = silc_idlist_find_client_by_id(server->global_list,
2164 (SilcClientID *)client_id,
2166 if (!client && server->server_type == SILC_ROUTER)
2170 if (!client && server->standalone)
2173 if (!client || !client->nickname || !client->username ||
2175 SilcBuffer buffer, idp;
2178 server->stat.commands_sent++;
2181 client->data.status |= SILC_IDLIST_STATUS_RESOLVING;
2182 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVED;
2183 client->resolve_cmd_ident = ++server->cmd_ident;
2186 idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
2187 buffer = silc_command_payload_encode_va(SILC_COMMAND_WHOIS,
2188 server->cmd_ident, 1,
2190 silc_buffer_len(idp));
2191 silc_server_packet_send(server,
2192 client && client->router ? client->router->connection :
2193 SILC_PRIMARY_ROUTE(server),
2194 SILC_PACKET_COMMAND, 0,
2195 buffer->data, silc_buffer_len(buffer));
2196 silc_buffer_free(idp);
2197 silc_buffer_free(buffer);