5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2002 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 SilcSocketConnection 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 SilcUInt16 index; /* Index to IDs */
46 unsigned int from_cmd : 1; /* TRUE if `index' is from command args,
47 otherwise from query->ids */
48 unsigned int error : 7; /* The actual error (SilcStatus) */
49 } *SilcServerQueryError;
51 /* Query session context */
54 char *nickname; /* Queried nickname */
55 char *nick_server; /* Queried nickname's server */
56 char *server_name; /* Queried server name */
57 char *channel_name; /* Queried channel name */
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 SilcServerCommandContext cmd; /* Command context for query */
65 SilcServerQueryList querylist; /* Temporary query list context */
66 SilcServerQueryID queries; /* Ongoing queries */
67 SilcServerQueryError errors; /* Query errors */
68 SilcUInt16 querylist_count; /* Number of query lists */
69 SilcUInt16 queries_count; /* Number of ongoing queries */
70 SilcUInt16 queries_left; /* Number of ongoing queries left */
71 SilcUInt16 errors_count; /* number of errors */
72 unsigned int querycmd : 7; /* Query command (SilcCommand) */
73 unsigned int resolved : 1; /* TRUE if normal server has resolved
74 information from router */
77 void silc_server_query_free(SilcServerQuery query);
78 void silc_server_query_send_error(SilcServer server,
79 SilcServerQuery query,
80 SilcStatus error, ...);
81 void silc_server_query_add_error(SilcServer server,
82 SilcServerQuery query,
86 void silc_server_query_add_error_id(SilcServer server,
87 SilcServerQuery query,
89 void *id, SilcIdType id_type);
90 void silc_server_query_send_router(SilcServer server, SilcServerQuery query);
91 void silc_server_query_send_router_reply(void *context, void *reply);
92 void silc_server_query_parse(SilcServer server, SilcServerQuery query);
93 void silc_server_query_process(SilcServer server, SilcServerQuery query,
95 void silc_server_query_resolve(SilcServer server, SilcServerQuery query,
96 SilcSocketConnection sock,
97 SilcClientEntry client_entry);
98 void silc_server_query_resolve_reply(void *context, void *reply);
99 void silc_server_query_send_reply(SilcServer server,
100 SilcServerQuery query,
101 SilcClientEntry *clients,
102 SilcUInt32 clients_count,
103 SilcServerEntry *servers,
104 SilcUInt32 servers_count,
105 SilcChannelEntry *channels,
106 SilcUInt32 channels_count);
107 SilcBuffer silc_server_query_reply_attrs(SilcServer server,
108 SilcServerQuery query,
109 SilcClientEntry client_entry);
111 /* Free the query context structure and all allocated resources. */
113 void silc_server_query_free(SilcServerQuery query)
117 silc_server_command_free(query->cmd);
119 for (i = 0; i < query->queries_count; i++)
120 silc_free(query->queries[i].id);
121 silc_free(query->queries);
123 silc_free(query->nickname);
124 silc_free(query->nick_server);
125 silc_free(query->server_name);
126 silc_free(query->channel_name);
128 for (i = 0; i < query->ids_count; i++)
129 silc_free(query->ids[i].id);
130 silc_free(query->ids);
133 silc_attribute_payload_list_free(query->attrs);
135 for (i = 0; i < query->errors_count; i++)
136 silc_free(query->errors[i].id);
137 silc_free(query->errors);
139 memset(query, 'F', sizeof(*query));
143 /* Send error reply indicated by the `error' to the original sender of
146 void silc_server_query_send_error(SilcServer server,
147 SilcServerQuery query,
148 SilcStatus error, ...)
151 unsigned char *data = NULL;
152 SilcUInt32 data_len = 0, data_type = 0, argc = 0;
155 data_type = va_arg(va, SilcUInt32);
158 data = va_arg(va, unsigned char *);
159 data_len = va_arg(va, SilcUInt32);
162 SILC_LOG_DEBUG(("ERROR: %s (%d)", silc_get_status_message(error), error));
164 /* Send the command reply with error */
165 silc_server_send_command_reply(server, query->cmd->sock,
166 query->querycmd, error, 0,
167 silc_command_get_ident(query->cmd->payload),
168 argc, data_type, data, data_len);
172 /* Add error to error list. Multiple errors may occur during the query
173 processing and this function can be used to add one error. The
174 `index' is the index to the command context which includes the argument
175 which caused the error, or it is the index to query->ids, depending
176 on value of `from_cmd'. */
178 void silc_server_query_add_error(SilcServer server,
179 SilcServerQuery query,
184 query->errors = silc_realloc(query->errors, sizeof(*query->errors) *
185 (query->errors_count + 1));
188 query->errors[query->errors_count].index = index;
189 query->errors[query->errors_count].from_cmd = from_cmd;
190 query->errors[query->errors_count].error = error;
191 query->errors[query->errors_count].id = NULL;
192 query->errors[query->errors_count].id_type = 0;
193 query->errors_count++;
196 /* Same as silc_server_query_add_error but adds the ID data to be used
197 with error sending with this error type. */
199 void silc_server_query_add_error_id(SilcServer server,
200 SilcServerQuery query,
202 void *id, SilcIdType id_type)
204 query->errors = silc_realloc(query->errors, sizeof(*query->errors) *
205 (query->errors_count + 1));
208 query->errors[query->errors_count].index = 0;
209 query->errors[query->errors_count].from_cmd = FALSE;
210 query->errors[query->errors_count].error = error;
211 query->errors[query->errors_count].id = silc_id_dup(id, id_type);
212 query->errors[query->errors_count].id_type = id_type;
213 query->errors_count++;
216 /* Processes query as command. The `query' is the command that is
217 being processed indicated by the `cmd'. The `query' can be one of
218 the following: SILC_COMMAND_WHOIS, SILC_COMMAND_WHOWAS or
219 SILC_COMMAND_IDENTIFY. This function handles the reply sending
220 to the entity who sent this query to us automatically. Returns
221 TRUE if the query is being processed or FALSE on error. */
223 bool silc_server_query_command(SilcServer server, SilcCommand querycmd,
224 SilcServerCommandContext cmd)
226 SilcServerQuery query;
228 SILC_LOG_DEBUG(("Query %s command", silc_get_command_name(querycmd)));
230 query = silc_calloc(1, sizeof(*query));
231 query->querycmd = querycmd;
232 query->cmd = silc_server_command_dup(cmd);
236 case SILC_COMMAND_WHOIS:
237 /* If we are normal server and query contains nickname, send it
238 directly to router. */
239 if (server->server_type == SILC_SERVER && !server->standalone &&
240 cmd->sock != SILC_PRIMARY_ROUTE(server) &&
241 silc_argument_get_arg_type(cmd->args, 1, NULL)) {
242 silc_server_query_send_router(server, query);
247 case SILC_COMMAND_WHOWAS:
248 /* WHOWAS query is always sent to router if we are normal server */
249 if (server->server_type == SILC_SERVER && !server->standalone &&
250 cmd->sock != SILC_PRIMARY_ROUTE(server)) {
251 silc_server_query_send_router(server, query);
256 case SILC_COMMAND_IDENTIFY:
257 /* If we are normal server and query does not contain IDs, send it
258 directly to router (it contains nickname, server name or channel
260 if (server->server_type == SILC_SERVER && !server->standalone &&
261 cmd->sock != SILC_PRIMARY_ROUTE(server) &&
262 !silc_argument_get_arg_type(cmd->args, 5, NULL)) {
263 silc_server_query_send_router(server, query);
269 SILC_LOG_ERROR(("Bad query using %d command", querycmd));
270 silc_server_query_free(query);
274 /* Now parse the request */
275 silc_server_query_parse(server, query);
280 /* Send the received query to our primary router since we could not
281 handle the query directly. We will reprocess the query after our
282 router replies back. */
284 void silc_server_query_send_router(SilcServer server, SilcServerQuery query)
287 SilcUInt16 old_ident;
289 SILC_LOG_DEBUG(("Forwarding the query to router for processing"));
291 /* Send WHOIS command to our router */
292 old_ident = silc_command_get_ident(query->cmd->payload);
293 silc_command_set_ident(query->cmd->payload, ++server->cmd_ident);
294 tmpbuf = silc_command_payload_encode_payload(query->cmd->payload);
295 silc_server_packet_send(server,
296 SILC_PRIMARY_ROUTE(server),
297 SILC_PACKET_COMMAND, 0,
298 tmpbuf->data, tmpbuf->len, TRUE);
299 silc_command_set_ident(query->cmd->payload, old_ident);
300 silc_buffer_free(tmpbuf);
302 query->resolved = TRUE;
304 /* Continue parsing the query after received reply from router */
305 silc_server_command_pending(server, query->querycmd, server->cmd_ident,
306 silc_server_query_send_router_reply, query);
309 /* Reply callback called after primary router has replied to our initial
310 sending of the query to it. We will proceed the query in this function. */
312 void silc_server_query_send_router_reply(void *context, void *reply)
314 SilcServerQuery query = context;
315 SilcServer server = query->cmd->server;
316 SilcServerCommandReplyContext cmdr = reply;
318 SILC_LOG_DEBUG(("Received reply from router to query"));
320 /* Check if router sent error reply */
321 if (cmdr && !silc_command_get_status(cmdr->payload, NULL, NULL)) {
324 SILC_LOG_DEBUG(("Sending error to original query"));
326 /* Send the same command reply payload which contains the error */
327 silc_command_set_command(cmdr->payload, query->querycmd);
328 silc_command_set_ident(cmdr->payload,
329 silc_command_get_ident(query->cmd->payload));
330 buffer = silc_command_payload_encode_payload(cmdr->payload);
331 silc_server_packet_send(server, query->cmd->sock,
332 SILC_PACKET_COMMAND_REPLY, 0,
333 buffer->data, buffer->len, FALSE);
334 silc_buffer_free(buffer);
335 silc_server_query_free(query);
339 /* Continue with parsing */
340 silc_server_query_parse(server, query);
343 /* Parse the command query and start processing the queries in detail. */
345 void silc_server_query_parse(SilcServer server, SilcServerQuery query)
347 SilcServerCommandContext cmd = query->cmd;
349 SilcUInt32 tmp_len, argc = silc_argument_get_arg_num(cmd->args);
354 SILC_LOG_DEBUG(("Parsing %s query",
355 silc_get_command_name(query->querycmd)));
357 switch (query->querycmd) {
359 case SILC_COMMAND_WHOIS:
360 /* Get Client IDs if present. Take IDs always instead of nickname. */
361 tmp = silc_argument_get_arg_type(cmd->args, 4, &tmp_len);
365 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
367 silc_server_query_send_error(server, query,
368 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
369 silc_server_query_free(query);
373 /* Get the nickname@server string and parse it */
375 !silc_parse_userfqdn(tmp, &query->nickname, &query->nick_server)) {
376 silc_server_query_send_error(server, query,
377 SILC_STATUS_ERR_BAD_NICKNAME, 0);
378 silc_server_query_free(query);
383 /* Parse the IDs included in the query */
384 query->ids = silc_calloc(argc, sizeof(*query->ids));
386 for (i = 0; i < argc; i++) {
387 tmp = silc_argument_get_arg_type(cmd->args, i + 4, &tmp_len);
391 id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
393 silc_server_query_add_error(server, query, TRUE, i + 4,
394 SILC_STATUS_ERR_BAD_CLIENT_ID);
398 /* Normal server must check whether this ID exist, and if not then
399 send the query to router, unless done so already */
400 if (server->server_type == SILC_SERVER && !query->resolved) {
401 if (!silc_idlist_find_client_by_id(server->local_list,
403 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT ||
404 !silc_idlist_find_client_by_id(server->global_list,
406 silc_server_query_send_router(server, query);
407 for (i = 0; i < query->ids_count; i++)
408 silc_free(query->ids[i].id);
409 silc_free(query->ids);
416 query->ids[query->ids_count].id = id;
417 query->ids[query->ids_count].id_type = SILC_ID_CLIENT;
422 /* Get the max count of reply messages allowed */
423 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
424 if (tmp && tmp_len == sizeof(SilcUInt32))
425 SILC_GET32_MSB(query->reply_count, tmp);
427 /* Get requested attributes if set */
428 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
430 query->attrs = silc_attribute_payload_parse_list(tmp, tmp_len);
433 case SILC_COMMAND_WHOWAS:
435 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
437 silc_server_query_send_error(server, query,
438 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
439 silc_server_query_free(query);
443 /* Get the nickname@server string and parse it */
445 !silc_parse_userfqdn(tmp, &query->nickname, &query->nick_server)) {
446 silc_server_query_send_error(server, query,
447 SILC_STATUS_ERR_BAD_NICKNAME, 0);
448 silc_server_query_free(query);
452 /* Get the max count of reply messages allowed */
453 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
454 if (tmp && tmp_len == sizeof(SilcUInt32))
455 SILC_GET32_MSB(query->reply_count, tmp);
458 case SILC_COMMAND_IDENTIFY:
459 /* Get IDs if present. Take IDs always instead of names. */
460 tmp = silc_argument_get_arg_type(cmd->args, 5, &tmp_len);
463 /* Try get nickname */
464 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
466 /* Get the nickname@server string and parse it */
468 !silc_parse_userfqdn(tmp, &query->nickname, &query->nick_server))
469 silc_server_query_add_error(server, query, TRUE, 1,
470 SILC_STATUS_ERR_BAD_NICKNAME);
473 /* Try get server name */
474 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
476 query->server_name = silc_memdup(tmp, tmp_len);
478 /* Get channel name */
479 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
480 if (tmp && tmp_len <= 256)
481 query->channel_name = silc_memdup(tmp, tmp_len);
483 if (!query->nickname && !query->server_name && !query->channel_name) {
484 silc_server_query_send_error(server, query,
485 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
486 silc_server_query_free(query);
491 /* Parse the IDs included in the query */
492 query->ids = silc_calloc(argc, sizeof(*query->ids));
494 for (i = 0; i < argc; i++) {
495 tmp = silc_argument_get_arg_type(cmd->args, i + 5, &tmp_len);
499 id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
501 silc_server_query_add_error(server, query, TRUE, i + 5,
502 SILC_STATUS_ERR_BAD_CLIENT_ID);
506 /* Normal server must check whether this ID exist, and if not then
507 send the query to router, unless done so already */
508 if (server->server_type == SILC_SERVER && !query->resolved) {
509 if (!silc_idlist_find_client_by_id(server->local_list,
511 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT ||
512 !silc_idlist_find_client_by_id(server->global_list,
514 silc_server_query_send_router(server, query);
515 for (i = 0; i < query->ids_count; i++)
516 silc_free(query->ids[i].id);
517 silc_free(query->ids);
524 query->ids[query->ids_count].id = id;
525 query->ids[query->ids_count].id_type = id_type;
530 /* Get the max count of reply messages allowed */
531 tmp = silc_argument_get_arg_type(cmd->args, 4, &tmp_len);
532 if (tmp && tmp_len == sizeof(SilcUInt32))
533 SILC_GET32_MSB(query->reply_count, tmp);
537 /* Start processing the query information */
538 silc_server_query_process(server, query, TRUE);
541 /* Processes the parsed query. This does the actual finding of the
542 queried information and prepares for sending reply to the original
543 sender of the query command. */
545 void silc_server_query_process(SilcServer server, SilcServerQuery query,
548 SilcServerCommandContext cmd = query->cmd;
549 bool check_global = FALSE;
551 SilcClientEntry *clients = NULL, client_entry;
552 SilcChannelEntry *channels = NULL;
553 SilcServerEntry *servers = NULL;
554 SilcUInt32 clients_count = 0, channels_count = 0, servers_count = 0;
557 SILC_LOG_DEBUG(("Processing %s query",
558 silc_get_command_name(query->querycmd)));
560 /* Check global lists if query is coming from client or we are not
561 normal server (we know global information). */
562 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
564 else if (server->server_type != SILC_SERVER)
567 if (query->nickname) {
568 /* Get all clients matching nickname from local list */
569 if (!silc_idlist_get_clients_by_hash(server->local_list,
570 query->nickname, server->md5hash,
571 &clients, &clients_count))
572 silc_idlist_get_clients_by_nickname(server->local_list,
575 &clients, &clients_count);
577 /* Check global list as well */
579 if (!silc_idlist_get_clients_by_hash(server->global_list,
580 query->nickname, server->md5hash,
581 &clients, &clients_count))
582 silc_idlist_get_clients_by_nickname(server->global_list,
585 &clients, &clients_count);
589 silc_server_query_add_error(server, query, TRUE, 1,
590 SILC_STATUS_ERR_NO_SUCH_NICK);
593 if (query->server_name) {
594 /* Find server by name */
595 entry = silc_idlist_find_server_by_name(server->local_list,
596 query->server_name, TRUE, NULL);
597 if (!entry && check_global)
598 entry = silc_idlist_find_server_by_name(server->global_list,
599 query->server_name, TRUE, NULL);
601 servers = silc_realloc(servers, sizeof(*servers) * (servers_count + 1));
602 servers[servers_count++] = (SilcServerEntry)entry;
606 silc_server_query_add_error(server, query, TRUE, 2,
607 SILC_STATUS_ERR_NO_SUCH_SERVER);
610 if (query->channel_name) {
611 /* Find channel by name */
612 entry = silc_idlist_find_channel_by_name(server->local_list,
613 query->channel_name, NULL);
614 if (!entry && check_global)
615 entry = silc_idlist_find_channel_by_name(server->global_list,
616 query->channel_name, NULL);
618 channels = silc_realloc(channels, sizeof(*channels) *
619 (channels_count + 1));
620 channels[channels_count++] = (SilcChannelEntry)entry;
624 silc_server_query_add_error(server, query, TRUE, 3,
625 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
628 if (query->ids_count) {
629 /* Find entries by the queried IDs */
630 for (i = 0; i < query->ids_count; i++) {
631 void *id = query->ids[i].id;
635 switch (query->ids[i].id_type) {
638 /* Get client entry */
639 entry = silc_idlist_find_client_by_id(server->local_list,
641 if (!entry && check_global)
642 entry = silc_idlist_find_client_by_id(server->global_list,
645 silc_server_query_add_error(server, query, FALSE, i,
646 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
650 clients = silc_realloc(clients, sizeof(*clients) *
651 (clients_count + 1));
652 clients[clients_count++] = (SilcClientEntry)entry;
656 /* Get server entry */
657 entry = silc_idlist_find_server_by_id(server->local_list,
659 if (!entry && check_global)
660 entry = silc_idlist_find_server_by_id(server->global_list,
663 silc_server_query_add_error(server, query, FALSE, i,
664 SILC_STATUS_ERR_NO_SUCH_SERVER_ID);
668 servers = silc_realloc(servers, sizeof(*servers) *
669 (servers_count + 1));
670 servers[servers_count++] = (SilcServerEntry)entry;
673 case SILC_ID_CHANNEL:
674 /* Get channel entry */
675 entry = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
676 if (!entry && check_global)
677 entry = silc_idlist_find_channel_by_id(server->global_list, id,
680 silc_server_query_add_error(server, query, FALSE, i,
681 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID);
685 channels = silc_realloc(channels, sizeof(*channels) *
686 (channels_count + 1));
687 channels[channels_count++] = (SilcChannelEntry)entry;
696 /* If nothing was found, then just send the errors */
697 if (!clients && !channels && !servers) {
698 silc_server_query_send_reply(server, query, NULL, 0, NULL, 0, NULL, 0);
702 /* If caller does not want us to resolve anything (has resolved already)
703 then just continue with sending the reply */
705 silc_server_query_send_reply(server, query, clients, clients_count,
706 servers, servers_count, channels,
714 /* Now process all found information and if necessary do some more
716 switch (query->querycmd) {
718 case SILC_COMMAND_WHOIS:
719 for (i = 0; i < clients_count; i++) {
720 client_entry = clients[i];
722 /* Check if cannot query this anyway, so take next one */
724 !(client_entry->data.status & SILC_IDLIST_STATUS_REGISTERED))
727 /* If Requested Attributes is set then we always resolve the client
728 information, if not then check whether the entry is complete or not
729 and decide whether we need to resolve or not. */
732 /* Even if nickname and stuff are present, we may need to resolve
734 if (client_entry->nickname && client_entry->username &&
735 client_entry->userinfo) {
736 /* Check if cannot query this anyway, so take next one */
737 if (!client_entry->router)
740 /* If we are router, client is local to us, or client is on channel
741 we do not need to resolve the client information. */
742 if (server->server_type != SILC_SERVER || SILC_IS_LOCAL(client_entry)
743 || silc_hash_table_count(client_entry->channels) ||
749 /* When requested attributes is present and local client is detached
750 we cannot send the command to the client, we'll reply on behalf of
751 the client instead. */
752 if (query->attrs && SILC_IS_LOCAL(client_entry) &&
753 (client_entry->mode & SILC_UMODE_DETACHED ||
754 client_entry->data.status & SILC_IDLIST_STATUS_NOATTR))
757 /* Resolve the detailed client information. If client is local we
758 know that attributes were present and we will resolve directly
759 from the client. Otherwise resolve from client's owner. */
760 silc_server_query_resolve(server, query,
761 (SILC_IS_LOCAL(client_entry) ?
762 client_entry->connection :
763 client_entry->router->connection),
768 case SILC_COMMAND_WHOWAS:
769 for (i = 0; i < clients_count; i++) {
770 client_entry = clients[i];
772 /* Check if cannot query this anyway, so take next one */
773 if (!client_entry || !client_entry->router ||
774 client_entry->data.status & SILC_IDLIST_STATUS_REGISTERED)
777 /* If both nickname and username are present no resolving is needed */
778 if (client_entry->nickname && client_entry->username)
781 /* Resolve the detailed client information */
782 silc_server_query_resolve(server, query,
783 client_entry->router->connection,
788 case SILC_COMMAND_IDENTIFY:
789 for (i = 0; i < clients_count; i++) {
790 client_entry = clients[i];
792 /* Check if cannot query this anyway, so take next one */
793 if (!client_entry || !client_entry->router ||
794 !(client_entry->data.status & SILC_IDLIST_STATUS_REGISTERED))
797 /* Even if nickname is present, we may need to resolve the entry */
798 if (client_entry->nickname) {
800 /* If we are router, client is local to us, or client is on channel
801 we do not need to resolve the client information. */
802 if (server->server_type != SILC_SERVER || SILC_IS_LOCAL(client_entry)
803 || silc_hash_table_count(client_entry->channels) ||
808 /* Resolve the detailed client information */
809 silc_server_query_resolve(server, query,
810 client_entry->router->connection,
816 if (!query->queries_count)
817 /* If we didn't have to do any resolving, continue with sending the
818 command reply to the original sender. */
819 silc_server_query_send_reply(server, query, clients, clients_count,
820 servers, servers_count, channels,
823 /* Now actually send the resolvings we gathered earlier */
824 silc_server_query_resolve(server, query, NULL, NULL);
831 /* Resolve the detailed information for the `client_entry'. Only client
832 information needs to be resolved for being incomplete. Each incomplete
833 client entry calls this function to do the resolving. */
835 void silc_server_query_resolve(SilcServer server, SilcServerQuery query,
836 SilcSocketConnection sock,
837 SilcClientEntry client_entry)
839 SilcServerCommandContext cmd = query->cmd;
840 SilcServerQueryList r = NULL;
847 if (!sock && client_entry)
850 /* If arguments are NULL we will now actually send the resolvings
851 that earlier has been gathered by calling this function. */
852 if (!sock && !client_entry) {
855 SILC_LOG_DEBUG(("Sending the resolvings"));
857 /* WHOWAS resolving has been done at the same time this function
858 was called to add the resolving for WHOWAS, so just return. */
859 if (query->querycmd == SILC_COMMAND_WHOWAS)
862 for (i = 0; i < query->querylist_count; i++) {
863 r = &query->querylist[i];
865 /* Send WHOIS command */
866 res_cmd = silc_command_payload_encode(SILC_COMMAND_WHOIS,
867 r->argc, r->arg, r->arg_lens,
868 r->arg_types, r->ident);
869 silc_server_packet_send(server, r->sock, SILC_PACKET_COMMAND, 0,
870 res_cmd->data, res_cmd->len, FALSE);
871 silc_buffer_free(res_cmd);
873 /* Reprocess this packet after received reply */
874 if (silc_server_command_pending_timed(server, SILC_COMMAND_WHOIS,
876 silc_server_query_resolve_reply,
878 query->queries_left++;
881 /* Cleanup this temporary context */
882 for (i = 0; i < query->querylist_count; i++) {
884 for (k = 0; k < query->querylist[i].argc; k++)
885 silc_free(query->querylist[i].arg[k]);
886 silc_free(query->querylist[i].arg);
887 silc_free(query->querylist[i].arg_lens);
888 silc_free(query->querylist[i].arg_types);
890 silc_free(query->querylist);
891 query->querylist = NULL;
892 query->querylist_count = 0;
896 SILC_LOG_DEBUG(("Resolving client information"));
898 if (client_entry->data.status & SILC_IDLIST_STATUS_RESOLVING) {
899 /* The entry is being resolved by some other external query already.
900 Attach to that query instead of resolving again. */
901 ident = client_entry->resolve_cmd_ident;
902 if (silc_server_command_pending(server, SILC_COMMAND_NONE, ident,
903 silc_server_query_resolve_reply, query))
904 query->queries_left++;
906 /* This entry will be resolved */
907 ident = ++server->cmd_ident;
909 switch (query->querycmd) {
911 case SILC_COMMAND_WHOIS:
912 case SILC_COMMAND_IDENTIFY:
913 /* Take existing query context if exist for this connection */
914 for (i = 0; i < query->queries_count; i++)
915 if (query->querylist[i].sock == sock) {
916 r = &query->querylist[i];
921 /* Allocate new temp query list context */
922 query->querylist = silc_realloc(query->querylist,
923 sizeof(*query->querylist) *
924 (query->querylist_count + 1));
925 r = &query->querylist[query->querylist_count];
926 query->querylist_count++;
927 memset(r, 0, sizeof(*r));
930 if (SILC_IS_LOCAL(client_entry))
934 /* If Requested Attributes were present put them to this resolving */
935 if (query->attrs && query->querycmd == SILC_COMMAND_WHOIS) {
937 r->arg = silc_realloc(r->arg, sizeof(*r->arg) * len);
938 r->arg_lens = silc_realloc(r->arg_lens, sizeof(*r->arg_lens) * len);
939 r->arg_types = silc_realloc(r->arg_types, sizeof(*r->arg_types) * len);
941 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
943 r->arg[r->argc] = silc_memdup(tmp, len);
944 r->arg_lens[r->argc] = len;
945 r->arg_types[r->argc] = 3;
950 r->arg = silc_realloc(r->arg, sizeof(*r->arg) * len);
951 r->arg_lens = silc_realloc(r->arg_lens, sizeof(*r->arg_lens) * len);
952 r->arg_types = silc_realloc(r->arg_types, sizeof(*r->arg_types) * len);
954 /* Add the client entry to be resolved */
955 idp = silc_id_payload_encode(client_entry->id, SILC_ID_CLIENT);
956 r->arg[r->argc] = silc_memdup(idp->data, idp->len);
957 r->arg_lens[r->argc] = idp->len;
958 r->arg_types[r->argc] = r->argc + 4;
960 silc_buffer_free(idp);
964 case SILC_COMMAND_WHOWAS:
965 /* We must send WHOWAS command since it's the only the way of
966 resolving clients that are not present in the network anymore. */
967 silc_server_send_command(server, sock, query->querycmd, ident, 1,
968 1, query->nickname, strlen(query->nickname));
969 if (silc_server_command_pending(server, query->querycmd, ident,
970 silc_server_query_resolve_reply, query))
971 query->queries_left++;
976 /* Mark the entry as being resolved */
977 client_entry->data.status |= SILC_IDLIST_STATUS_RESOLVING;
978 client_entry->data.status &= ~SILC_IDLIST_STATUS_RESOLVED;
979 client_entry->resolve_cmd_ident = ident;
981 /* Save the queried ID, which we will reprocess after we get this and
982 all other queries back. */
983 query->queries = silc_realloc(query->queries, sizeof(*query->queries) *
984 (query->queries_count + 1));
985 if (query->queries) {
986 i = query->queries_count;
987 query->queries[i].id = silc_id_dup(client_entry->id, SILC_ID_CLIENT);
988 query->queries[i].id_type = SILC_ID_CLIENT;
989 query->queries[i].ident = ident;
990 query->queries_count++;
994 /* Reply callback called after one resolving has been completed. If
995 all resolvings has been received then we will continue with sending
996 the command reply to the original sender of the query. */
998 void silc_server_query_resolve_reply(void *context, void *reply)
1000 SilcServerQuery query = context;
1001 SilcServer server = query->cmd->server;
1002 SilcServerCommandReplyContext cmdr = reply;
1003 SilcUInt16 ident = cmdr->ident;
1004 SilcStatus error = SILC_STATUS_OK;
1005 SilcServerQueryID id = NULL;
1006 SilcClientEntry client_entry;
1009 /* One less query left */
1010 query->queries_left--;
1012 silc_command_get_status(cmdr->payload, NULL, &error);
1013 SILC_LOG_DEBUG(("Received reply to resolving (%d left) (status=%d)",
1014 query->queries_left, error));
1016 /* If no error then skip to other stuff */
1017 if (error == SILC_STATUS_OK)
1020 /* Error occurred during resolving */
1022 /* Find the resolved client ID */
1023 for (i = 0; i < query->queries_count; i++) {
1024 if (query->queries[i].ident != ident)
1027 id = &query->queries[i];
1029 if (error == SILC_STATUS_ERR_TIMEDOUT) {
1031 /* If timeout occurred for local entry when resolving attributes
1032 mark that this client doesn't support attributes in WHOIS. This
1033 assures we won't send the request again to the client. */
1034 if (query->querycmd == SILC_COMMAND_WHOIS && query->attrs) {
1035 client_entry = silc_idlist_find_client_by_id(server->local_list,
1036 id->id, TRUE, NULL);
1037 SILC_LOG_DEBUG(("Client %s does not support Requested Attributes",
1038 silc_id_render(id->id, SILC_ID_CLIENT)));
1039 if (client_entry && SILC_IS_LOCAL(client_entry)) {
1040 client_entry->data.status |= SILC_IDLIST_STATUS_NOATTR;
1041 client_entry->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
1046 /* Remove the RESOLVING status from the client entry */
1047 if (query->querycmd != SILC_COMMAND_WHOWAS) {
1048 client_entry = silc_idlist_find_client_by_id(server->local_list,
1049 id->id, TRUE, NULL);
1051 client_entry = silc_idlist_find_client_by_id(server->global_list,
1052 id->id, TRUE, NULL);
1054 client_entry->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
1061 /* If there are queries left then wait for them */
1062 if (query->queries_left)
1065 SILC_LOG_DEBUG(("Reprocess the query"));
1067 /* We have received all queries. Now re-search all information required
1068 to complete this query. Reason we cannot save the values found in
1069 the first search is that SilcClientEntry, SilcServerEntry and
1070 SilcChannelEntry pointers may become invalid while we were waiting
1071 for these resolvings. */
1072 silc_server_query_process(server, query, FALSE);
1075 /* Send the reply to the original query. If arguments are NULL then this
1076 sends only the errors that has occurred during the processing of the
1077 query. This sends the errors always after sending all the found
1078 information. The query is over after this function returns and the
1079 `query' will become invalid. This is called only after all informations
1080 has been resolved. This means that if something is not found or is
1081 incomplete in this function we were unable to resolve the information
1082 or it does not exist at all. */
1084 void silc_server_query_send_reply(SilcServer server,
1085 SilcServerQuery query,
1086 SilcClientEntry *clients,
1087 SilcUInt32 clients_count,
1088 SilcServerEntry *servers,
1089 SilcUInt32 servers_count,
1090 SilcChannelEntry *channels,
1091 SilcUInt32 channels_count)
1093 SilcServerCommandContext cmd = query->cmd;
1094 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1099 int i, k, valid_count;
1100 char nh[256], uh[256];
1101 bool sent_reply = FALSE;
1103 SILC_LOG_DEBUG(("Sending reply to query"));
1105 status = SILC_STATUS_OK;
1108 if (clients_count) {
1109 SilcClientEntry entry;
1110 SilcSocketConnection hsock;
1112 /* Mark all invalid entries */
1113 for (i = 0, valid_count = 0; i < clients_count; i++) {
1115 switch (query->querycmd) {
1116 case SILC_COMMAND_WHOIS:
1117 if (!entry->nickname || !entry->username || !entry->userinfo ||
1118 !(entry->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
1119 /* When querying by ID, every "unfound" entry must cause error */
1121 silc_server_query_add_error_id(server, query,
1122 SILC_STATUS_ERR_TIMEDOUT,
1123 entry->id, SILC_ID_CLIENT);
1129 case SILC_COMMAND_IDENTIFY:
1130 if (!entry->nickname ||
1131 !(entry->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
1132 /* When querying by ID, every "unfound" entry must cause error */
1134 silc_server_query_add_error_id(server, query,
1135 SILC_STATUS_ERR_TIMEDOUT,
1136 entry->id, SILC_ID_CLIENT);
1142 case SILC_COMMAND_WHOWAS:
1143 if (!entry->nickname || !entry->username ||
1144 entry->data.status & SILC_IDLIST_STATUS_REGISTERED) {
1153 /* Start processing found clients */
1154 status = SILC_STATUS_OK;
1155 if (valid_count > 1)
1156 status = SILC_STATUS_LIST_START;
1158 /* Now do the sending of valid entries */
1160 for (i = 0; i < clients_count && valid_count; i++) {
1166 status = SILC_STATUS_LIST_ITEM;
1167 if (valid_count > 1 && k == valid_count - 1
1168 && !servers_count && !channels_count && !query->errors_count)
1169 status = SILC_STATUS_LIST_END;
1170 if (query->reply_count && k - 1 == query->reply_count)
1171 status = SILC_STATUS_LIST_END;
1173 SILC_LOG_DEBUG(("%s: client %s",
1174 (status == SILC_STATUS_OK ? " OK" :
1175 status == SILC_STATUS_LIST_START ? "START" :
1176 status == SILC_STATUS_LIST_ITEM ? " ITEM" :
1177 status == SILC_STATUS_LIST_END ? " END" :
1178 " : "), entry->nickname));
1180 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1181 memset(uh, 0, sizeof(uh));
1182 memset(nh, 0, sizeof(nh));
1184 silc_strncat(nh, sizeof(nh), entry->nickname, strlen(entry->nickname));
1185 if (!strchr(entry->nickname, '@')) {
1186 silc_strncat(nh, sizeof(nh), "@", 1);
1187 if (entry->servername) {
1188 silc_strncat(nh, sizeof(nh), entry->servername,
1189 strlen(entry->servername));
1191 len = entry->router ? strlen(entry->router->server_name) :
1192 strlen(server->server_name);
1193 silc_strncat(nh, sizeof(nh), entry->router ?
1194 entry->router->server_name :
1195 server->server_name, len);
1199 switch (query->querycmd) {
1201 case SILC_COMMAND_WHOIS:
1203 unsigned char idle[4], mode[4];
1204 unsigned char *fingerprint, fempty[20], *attrs = NULL;
1205 SilcBuffer channels, umode_list = NULL, tmpattrs = NULL;
1207 memset(fempty, 0, sizeof(fempty));
1208 memset(idle, 0, sizeof(idle));
1209 silc_strncat(uh, sizeof(uh), entry->username,
1210 strlen(entry->username));
1211 if (!strchr(entry->username, '@') && entry->connection) {
1212 hsock = entry->connection;
1213 silc_strncat(uh, sizeof(uh), "@", 1);
1214 len = strlen(hsock->hostname);
1215 silc_strncat(uh, sizeof(uh), hsock->hostname, len);
1218 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1220 silc_server_get_client_channel_list(server, entry, FALSE,
1221 FALSE, &umode_list);
1224 silc_server_get_client_channel_list(server, entry, TRUE,
1227 if (memcmp(entry->data.fingerprint, fempty, sizeof(fempty)))
1228 fingerprint = entry->data.fingerprint;
1232 SILC_PUT32_MSB(entry->mode, mode);
1233 if (entry->connection)
1234 SILC_PUT32_MSB((time(NULL) - entry->data.last_receive), idle);
1236 /* If Requested Attribute were present, and we do not have the
1237 attributes we will reply to them on behalf of the client. */
1240 if (!entry->attrs) {
1241 tmpattrs = silc_server_query_reply_attrs(server, query, entry);
1242 entry->attrs = silc_memdup(tmpattrs->data, tmpattrs->len);
1243 entry->attrs_len = tmpattrs->len;
1244 silc_buffer_free(tmpattrs);
1246 attrs = entry->attrs;
1247 len = entry->attrs_len;
1250 /* Send command reply */
1251 silc_server_send_command_reply(server, cmd->sock, query->querycmd,
1252 status, 0, ident, 10,
1253 2, idp->data, idp->len,
1257 strlen(entry->userinfo),
1258 6, channels ? channels->data : NULL,
1259 channels ? channels->len : 0,
1263 fingerprint ? 20 : 0,
1264 10, umode_list ? umode_list->data :
1265 NULL, umode_list ? umode_list->len :
1270 /* For now we always delete Requested Attributes, unless the client
1271 is detached, in which case we don't want to reconstruct the
1272 same data everytime */
1273 if (!(entry->mode & SILC_UMODE_DETACHED) &&
1274 !(entry->data.status & SILC_IDLIST_STATUS_NOATTR)) {
1275 silc_free(entry->attrs);
1276 entry->attrs = NULL;
1280 silc_buffer_free(channels);
1282 silc_buffer_free(umode_list);
1288 case SILC_COMMAND_IDENTIFY:
1289 if (!entry->username) {
1290 silc_server_send_command_reply(server, cmd->sock, query->querycmd,
1291 status, 0, ident, 2,
1292 2, idp->data, idp->len,
1296 silc_strncat(uh, sizeof(uh), entry->username,
1297 strlen(entry->username));
1298 if (!strchr(entry->username, '@') && entry->connection) {
1299 hsock = entry->connection;
1300 silc_strncat(uh, sizeof(uh), "@", 1);
1301 len = strlen(hsock->hostname);
1302 silc_strncat(uh, sizeof(uh), hsock->hostname, len);
1305 silc_server_send_command_reply(server, cmd->sock, query->querycmd,
1306 status, 0, ident, 3,
1307 2, idp->data, idp->len,
1314 case SILC_COMMAND_WHOWAS:
1315 silc_strncat(uh, sizeof(uh), entry->username, strlen(entry->username));
1316 if (!strchr(entry->username, '@'))
1317 silc_strncat(uh, sizeof(uh), "@*private*", 10);
1319 /* Send command reply */
1320 silc_server_send_command_reply(server, cmd->sock, query->querycmd,
1321 status, 0, ident, 4,
1322 2, idp->data, idp->len,
1327 strlen(entry->userinfo) : 0);
1332 silc_buffer_free(idp);
1334 if (status == SILC_STATUS_LIST_END)
1340 /* Not one valid entry was found, send error. If nickname was used
1341 in query send error based on that, otherwise the query->errors
1342 already includes proper errors. */
1343 if (query->nickname)
1344 silc_server_query_add_error(server, query, TRUE, 1,
1345 SILC_STATUS_ERR_NO_SUCH_NICK);
1350 if (query->querycmd == SILC_COMMAND_IDENTIFY && servers_count) {
1351 SilcServerEntry entry;
1353 if (status == SILC_STATUS_OK && servers_count > 1)
1354 status = SILC_STATUS_LIST_START;
1357 for (i = 0; i < servers_count; i++) {
1361 status = SILC_STATUS_LIST_ITEM;
1362 if (servers_count > 1 && k == servers_count - 1 && !channels_count &&
1363 !query->errors_count)
1364 status = SILC_STATUS_LIST_END;
1365 if (query->reply_count && k - 1 == query->reply_count)
1366 status = SILC_STATUS_LIST_END;
1368 SILC_LOG_DEBUG(("%s: server %s",
1369 (status == SILC_STATUS_OK ? " OK" :
1370 status == SILC_STATUS_LIST_START ? "START" :
1371 status == SILC_STATUS_LIST_ITEM ? " ITEM" :
1372 status == SILC_STATUS_LIST_END ? " END" :
1374 entry->server_name ? entry->server_name : ""));
1376 /* Send command reply */
1377 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
1378 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_IDENTIFY,
1379 status, 0, ident, 2,
1380 2, idp->data, idp->len,
1381 3, entry->server_name,
1382 entry->server_name ?
1383 strlen(entry->server_name) : 0);
1384 silc_buffer_free(idp);
1387 if (status == SILC_STATUS_LIST_END)
1394 if (query->querycmd == SILC_COMMAND_IDENTIFY && channels_count) {
1395 SilcChannelEntry entry;
1397 if (status == SILC_STATUS_OK && channels_count > 1)
1398 status = SILC_STATUS_LIST_START;
1401 for (i = 0; i < channels_count; i++) {
1402 entry = channels[i];
1405 status = SILC_STATUS_LIST_ITEM;
1406 if (channels_count > 1 && k == channels_count - 1 &&
1407 !query->errors_count)
1408 status = SILC_STATUS_LIST_END;
1409 if (query->reply_count && k - 1 == query->reply_count)
1410 status = SILC_STATUS_LIST_END;
1412 SILC_LOG_DEBUG(("%s: channel %s",
1413 (status == SILC_STATUS_OK ? " OK" :
1414 status == SILC_STATUS_LIST_START ? "START" :
1415 status == SILC_STATUS_LIST_ITEM ? " ITEM" :
1416 status == SILC_STATUS_LIST_END ? " END" :
1418 entry->channel_name ? entry->channel_name : ""));
1420 /* Send command reply */
1421 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1422 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_IDENTIFY,
1423 status, 0, ident, 2,
1424 2, idp->data, idp->len,
1425 3, entry->channel_name,
1426 entry->channel_name ?
1427 strlen(entry->channel_name) : 0);
1428 silc_buffer_free(idp);
1431 if (status == SILC_STATUS_LIST_END)
1438 if (query->errors_count) {
1441 if (status == SILC_STATUS_OK && query->errors_count > 1)
1442 status = SILC_STATUS_LIST_START;
1445 for (i = 0; i < query->errors_count; i++) {
1448 /* Take error argument */
1449 if (query->errors[i].from_cmd) {
1451 tmp = silc_argument_get_arg_type(cmd->args,
1452 query->errors[i].index, &len);
1453 if (query->errors[i].index == 1)
1454 type = 3; /* Nickname */
1457 } else if (!query->errors[i].id) {
1459 silc_id_payload_encode(query->ids[query->errors[i].index].id,
1460 query->ids[query->errors[k].index].id_type);
1465 idp = silc_id_payload_encode(query->errors[i].id,
1466 query->errors[k].id_type);
1473 status = SILC_STATUS_LIST_ITEM;
1474 if (query->errors_count > 1 && k == query->errors_count - 1)
1475 status = SILC_STATUS_LIST_END;
1476 if (query->reply_count && k - 1 == query->reply_count)
1477 status = SILC_STATUS_LIST_END;
1479 SILC_LOG_DEBUG(("%s: ERROR: %s (%d)",
1480 (status == SILC_STATUS_OK ? " OK" :
1481 status == SILC_STATUS_LIST_START ? "START" :
1482 status == SILC_STATUS_LIST_ITEM ? " ITEM" :
1483 status == SILC_STATUS_LIST_END ? " END" :
1485 silc_get_status_message(query->errors[i].error),
1486 query->errors[i].error));
1489 silc_server_send_command_reply(server, cmd->sock, query->querycmd,
1490 (status == SILC_STATUS_OK ?
1491 query->errors[i].error : status),
1492 (status == SILC_STATUS_OK ?
1493 0 : query->errors[i].error), ident, 1,
1495 silc_buffer_free(idp);
1498 if (status == SILC_STATUS_LIST_END)
1505 SILC_LOG_ERROR(("BUG: Query did not send anything"));
1508 silc_server_query_free(query);
1511 /* This routine is used to reply to Requested Attributes in WHOIS on behalf
1512 of the client since we were unable to resolve them from the client.
1513 Either client does not support Requested Attributes or isn't replying
1514 to them like it should. */
1516 SilcBuffer silc_server_query_reply_attrs(SilcServer server,
1517 SilcServerQuery query,
1518 SilcClientEntry client_entry)
1520 SilcBuffer buffer = NULL;
1521 SilcAttribute attribute;
1522 SilcAttributePayload attr;
1523 SilcAttributeObjPk pk;
1524 SilcAttributeObjService service;
1526 unsigned char sign[2048];
1527 SilcUInt32 sign_len;
1529 SILC_LOG_DEBUG(("Constructing Requested Attributes"));
1531 /* Go through all requested attributes */
1532 silc_dlist_start(query->attrs);
1533 while ((attr = silc_dlist_get(query->attrs)) != SILC_LIST_END) {
1534 attribute = silc_attribute_get_attribute(attr);
1535 switch (attribute) {
1537 case SILC_ATTRIBUTE_USER_INFO:
1539 SILC_NOT_IMPLEMENTED("SILC_ATTRIBUTE_USER_INFO");
1542 case SILC_ATTRIBUTE_SERVICE:
1543 /* Put SERVICE. Put only SILC service. */
1544 memset(&service, 0, sizeof(service));
1545 service.port = (server->config->server_info->primary ?
1546 server->config->server_info->primary->port : SILC_PORT);
1547 silc_strncat(service.address, sizeof(service.address),
1548 server->server_name, strlen(server->server_name));
1549 service.status = !(client_entry->mode & SILC_UMODE_DETACHED);
1550 buffer = silc_attribute_payload_encode(buffer, attribute,
1551 SILC_ATTRIBUTE_FLAG_VALID,
1552 &service, sizeof(service));
1555 case SILC_ATTRIBUTE_STATUS_MOOD:
1556 /* Put STATUS_MOOD */
1557 buffer = silc_attribute_payload_encode(buffer, attribute,
1558 SILC_ATTRIBUTE_FLAG_VALID,
1560 SILC_ATTRIBUTE_MOOD_NORMAL,
1561 sizeof(SilcUInt32));
1564 case SILC_ATTRIBUTE_STATUS_FREETEXT:
1565 /* Put STATUS_FREETEXT. We just tell in the message that we are
1566 replying on behalf of the client. */
1568 "This information was provided by the server on behalf of the user";
1569 buffer = silc_attribute_payload_encode(buffer, attribute,
1570 SILC_ATTRIBUTE_FLAG_VALID,
1574 case SILC_ATTRIBUTE_PREFERRED_CONTACT:
1575 /* Put PREFERRED_CONTACT */
1576 buffer = silc_attribute_payload_encode(buffer, attribute,
1577 SILC_ATTRIBUTE_FLAG_VALID,
1579 SILC_ATTRIBUTE_CONTACT_CHAT,
1580 sizeof(SilcUInt32));
1583 case SILC_ATTRIBUTE_USER_PUBLIC_KEY:
1584 /* Put USER_PUBLIC_KEY */
1585 if (client_entry->data.public_key) {
1586 pk.type = "silc-rsa";
1587 pk.data = silc_pkcs_public_key_encode(client_entry->data.public_key,
1589 buffer = silc_attribute_payload_encode(buffer, attribute, pk.data ?
1590 SILC_ATTRIBUTE_FLAG_VALID :
1591 SILC_ATTRIBUTE_FLAG_INVALID,
1597 /* No public key available */
1598 buffer = silc_attribute_payload_encode(buffer, attribute,
1599 SILC_ATTRIBUTE_FLAG_INVALID,
1604 /* Ignore SERVER_PUBLIC_KEY since we are going to put it anyway later */
1605 if (attribute == SILC_ATTRIBUTE_SERVER_PUBLIC_KEY ||
1606 attribute == SILC_ATTRIBUTE_SERVER_DIGITAL_SIGNATURE)
1609 /* For other attributes we cannot reply so mark it invalid */
1610 buffer = silc_attribute_payload_encode(buffer, attribute,
1611 SILC_ATTRIBUTE_FLAG_INVALID,
1617 /* Always put our public key. This assures that we send at least
1618 something valid back always. */
1619 pk.type = "silc-rsa";
1620 pk.data = silc_pkcs_public_key_encode(server->public_key, &pk.data_len);
1621 buffer = silc_attribute_payload_encode(buffer,
1622 SILC_ATTRIBUTE_SERVER_PUBLIC_KEY,
1623 pk.data ? SILC_ATTRIBUTE_FLAG_VALID :
1624 SILC_ATTRIBUTE_FLAG_INVALID,
1628 /* Finally compute the digital signature of all the data we provided
1629 as an indication that we provided rightfull information, and this
1630 also authenticates our public key. */
1631 if (silc_pkcs_sign_with_hash(server->pkcs, server->sha1hash,
1632 buffer->data, buffer->len,
1636 pk.data_len = sign_len;
1638 silc_attribute_payload_encode(buffer,
1639 SILC_ATTRIBUTE_SERVER_DIGITAL_SIGNATURE,
1640 SILC_ATTRIBUTE_FLAG_VALID,
1647 /* Find client by the Client ID indicated by the `client_id', and if not
1648 found then query it by using WHOIS command. The client information
1649 is also resolved if the cached information is incomplete or if the
1650 `always_resolve' is set to TRUE. The indication whether requested
1651 client was being resolved is saved into `resolved'. If the client
1652 is not being resolved its entry is returned by this function. NULL
1653 is returned if client is resolved. */
1655 SilcClientEntry silc_server_query_client(SilcServer server,
1656 const SilcClientID *client_id,
1657 bool always_resolve,
1660 SilcClientEntry client;
1662 SILC_LOG_DEBUG(("Resolving client by client ID"));
1667 client = silc_idlist_find_client_by_id(server->local_list,
1668 (SilcClientID *)client_id,
1671 client = silc_idlist_find_client_by_id(server->global_list,
1672 (SilcClientID *)client_id,
1674 if (!client && server->server_type == SILC_ROUTER)
1678 if (!client && server->standalone)
1681 if (!client || !client->nickname || !client->username ||
1683 SilcBuffer buffer, idp;
1686 client->data.status |= SILC_IDLIST_STATUS_RESOLVING;
1687 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVED;
1688 client->resolve_cmd_ident = ++server->cmd_ident;
1691 idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1692 buffer = silc_command_payload_encode_va(SILC_COMMAND_WHOIS,
1693 server->cmd_ident, 1,
1694 4, idp->data, idp->len);
1695 silc_server_packet_send(server, client ? client->router->connection :
1696 SILC_PRIMARY_ROUTE(server),
1697 SILC_PACKET_COMMAND, 0,
1698 buffer->data, buffer->len, FALSE);
1699 silc_buffer_free(idp);
1700 silc_buffer_free(buffer);