5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 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"
24 static int silc_server_is_registered(SilcServer server,
25 SilcSocketConnection sock,
26 SilcServerCommandContext cmd,
29 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
34 silc_server_command_send_status_data(SilcServerCommandContext cmd,
39 const unsigned char *arg,
42 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
43 SilcServerCommandReplyContext cmdr,
45 SILC_TASK_CALLBACK(silc_server_command_process_timeout);
47 /* Server command list. */
48 SilcServerCommand silc_command_list[] =
50 SILC_SERVER_CMD(whois, WHOIS, SILC_CF_LAG | SILC_CF_REG),
51 SILC_SERVER_CMD(whowas, WHOWAS, SILC_CF_LAG | SILC_CF_REG),
52 SILC_SERVER_CMD(identify, IDENTIFY, SILC_CF_LAG | SILC_CF_REG),
53 SILC_SERVER_CMD(nick, NICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
54 SILC_SERVER_CMD(list, LIST, SILC_CF_LAG_STRICT | SILC_CF_REG),
55 SILC_SERVER_CMD(topic, TOPIC, SILC_CF_LAG | SILC_CF_REG),
56 SILC_SERVER_CMD(invite, INVITE, SILC_CF_LAG | SILC_CF_REG),
57 SILC_SERVER_CMD(quit, QUIT, SILC_CF_LAG | SILC_CF_REG),
58 SILC_SERVER_CMD(kill, KILL, SILC_CF_LAG_STRICT | SILC_CF_REG | SILC_CF_OPER),
59 SILC_SERVER_CMD(info, INFO, SILC_CF_LAG | SILC_CF_REG),
60 SILC_SERVER_CMD(stats, STATS, SILC_CF_LAG | SILC_CF_REG),
61 SILC_SERVER_CMD(ping, PING, SILC_CF_LAG | SILC_CF_REG),
62 SILC_SERVER_CMD(oper, OPER, SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
63 SILC_SERVER_CMD(join, JOIN, SILC_CF_LAG_STRICT | SILC_CF_REG),
64 SILC_SERVER_CMD(motd, MOTD, SILC_CF_LAG | SILC_CF_REG),
65 SILC_SERVER_CMD(umode, UMODE, SILC_CF_LAG | SILC_CF_REG),
66 SILC_SERVER_CMD(cmode, CMODE, SILC_CF_LAG_STRICT | SILC_CF_REG),
67 SILC_SERVER_CMD(cumode, CUMODE, SILC_CF_LAG | SILC_CF_REG),
68 SILC_SERVER_CMD(kick, KICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
69 SILC_SERVER_CMD(ban, BAN, SILC_CF_LAG_STRICT | SILC_CF_REG),
70 SILC_SERVER_CMD(detach, DETACH, SILC_CF_LAG_STRICT | SILC_CF_REG),
71 SILC_SERVER_CMD(watch, WATCH, SILC_CF_LAG | SILC_CF_REG),
72 SILC_SERVER_CMD(silcoper, SILCOPER,
73 SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER),
74 SILC_SERVER_CMD(leave, LEAVE, SILC_CF_LAG_STRICT | SILC_CF_REG),
75 SILC_SERVER_CMD(users, USERS, SILC_CF_LAG | SILC_CF_REG),
76 SILC_SERVER_CMD(getkey, GETKEY, SILC_CF_LAG | SILC_CF_REG),
78 SILC_SERVER_CMD(connect, PRIV_CONNECT,
79 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
80 SILC_SERVER_CMD(close, PRIV_CLOSE,
81 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
82 SILC_SERVER_CMD(shutdown, PRIV_SHUTDOWN, SILC_CF_LAG | SILC_CF_REG |
88 /* Performs several checks to the command. It first checks whether this
89 command was called as pending command callback. If it was then it checks
90 whether error occurred in the command reply where the pending command
93 It also checks that the requested command includes correct amount
95 #define SILC_SERVER_COMMAND_CHECK(command, context, min, max) \
99 if (silc_server_command_pending_error_check(cmd, context2, command)) { \
100 SILC_LOG_DEBUG(("Error occurred in command reply, command not called")); \
101 silc_server_command_free(cmd); \
105 _argc = silc_argument_get_arg_num(cmd->args); \
107 SILC_LOG_DEBUG(("Not enough parameters in command")); \
108 silc_server_command_send_status_reply(cmd, command, \
109 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, \
111 silc_server_command_free(cmd); \
115 SILC_LOG_DEBUG(("Too many parameters in command")); \
116 silc_server_command_send_status_reply(cmd, command, \
117 SILC_STATUS_ERR_TOO_MANY_PARAMS, \
119 silc_server_command_free(cmd); \
124 /* Returns TRUE if the connection is registered. Unregistered connections
125 usually cannot send commands hence the check. */
127 static int silc_server_is_registered(SilcServer server,
128 SilcSocketConnection sock,
129 SilcServerCommandContext cmd,
132 SilcIDListData idata = (SilcIDListData)sock->user_data;
137 if (idata->status & SILC_IDLIST_STATUS_REGISTERED)
140 silc_server_command_send_status_reply(cmd, command,
141 SILC_STATUS_ERR_NOT_REGISTERED, 0);
145 /* Internal context to hold data when executed command with timeout. */
147 SilcServerCommandContext ctx;
148 SilcServerCommand *cmd;
149 } *SilcServerCommandTimeout;
151 /* Timeout callback to process commands with timeout for client. Client's
152 commands are always executed with timeout. */
154 SILC_TASK_CALLBACK(silc_server_command_process_timeout)
156 SilcServerCommandTimeout timeout = (SilcServerCommandTimeout)context;
157 SilcClientEntry client = (SilcClientEntry)timeout->ctx->sock->user_data;
160 SILC_LOG_DEBUG(("Client entry is invalid"));
161 silc_server_command_free(timeout->ctx);
165 /* Update access time */
166 client->last_command = time(NULL);
168 if (!(timeout->cmd->flags & SILC_CF_REG)) {
169 SILC_LOG_DEBUG(("Calling %s command",
170 silc_get_command_name(timeout->cmd->cmd)));
171 timeout->cmd->cb(timeout->ctx, NULL);
172 } else if (silc_server_is_registered(timeout->ctx->server,
175 timeout->cmd->cmd)) {
176 SILC_LOG_DEBUG(("Calling %s command",
177 silc_get_command_name(timeout->cmd->cmd)));
178 timeout->cmd->cb(timeout->ctx, NULL);
180 SILC_LOG_DEBUG(("Client is not registered"));
181 silc_server_command_free(timeout->ctx);
187 /* Processes received command packet. */
189 void silc_server_command_process(SilcServer server,
190 SilcSocketConnection sock,
191 SilcPacketContext *packet)
193 SilcServerCommandContext ctx;
194 SilcServerCommand *cmd;
197 /* Allocate command context. This must be free'd by the
198 command routine receiving it. */
199 ctx = silc_server_command_alloc();
200 ctx->server = server;
201 ctx->sock = silc_socket_dup(sock);
202 ctx->packet = silc_packet_context_dup(packet); /* Save original packet */
204 /* Parse the command payload in the packet */
205 ctx->payload = silc_command_payload_parse(packet->buffer->data,
206 packet->buffer->len);
208 SILC_LOG_ERROR(("Bad command payload, packet dropped"));
209 silc_packet_context_free(packet);
210 silc_socket_free(ctx->sock);
214 ctx->args = silc_command_get_args(ctx->payload);
216 /* Get the command */
217 command = silc_command_get(ctx->payload);
218 for (cmd = silc_command_list; cmd->cb; cmd++)
219 if (cmd->cmd == command)
222 if (!cmd || !cmd->cb) {
223 SILC_LOG_DEBUG(("Unknown command %d", command));
224 silc_server_command_send_status_reply(ctx, command,
225 SILC_STATUS_ERR_UNKNOWN_COMMAND, 0);
226 silc_server_command_free(ctx);
230 /* Execute client's commands always with timeout. Normally they are
231 executed with zero (0) timeout but if client is sending command more
232 frequently than once in 2 seconds, then the timeout may be 0 to 2
234 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
235 SilcClientEntry client = (SilcClientEntry)sock->user_data;
236 SilcServerCommandTimeout timeout;
240 SILC_LOG_DEBUG(("Client entry is invalid"));
241 silc_server_command_free(ctx);
244 timeout = silc_calloc(1, sizeof(*timeout));
248 if (client->last_command && (time(NULL) - client->last_command) < 2) {
249 client->fast_command++;
252 if (client->fast_command - 2 <= 0)
253 client->fast_command = 0;
255 client->fast_command -= 2;
259 if (!fast && ((cmd->flags & SILC_CF_LAG_STRICT) ||
260 (client->fast_command > 5 && cmd->flags & SILC_CF_LAG)))
261 silc_schedule_task_add(server->schedule, sock->sock,
262 silc_server_command_process_timeout, timeout,
263 (client->fast_command < 3 ? 0 :
264 2 - (time(NULL) - client->last_command)),
265 (client->fast_command < 3 ? 200000 : 0),
266 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
268 silc_schedule_task_add(server->schedule, sock->sock,
269 silc_server_command_process_timeout, timeout,
270 0, 1, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
274 /* Execute for server */
276 if (!(cmd->flags & SILC_CF_REG)) {
277 SILC_LOG_DEBUG(("Calling %s command", silc_get_command_name(cmd->cmd)));
279 } else if (silc_server_is_registered(server, sock, ctx, cmd->cmd)) {
280 SILC_LOG_DEBUG(("Calling %s command", silc_get_command_name(cmd->cmd)));
283 SILC_LOG_DEBUG(("Server is not registered"));
284 silc_server_command_free(ctx);
288 /* Allocate Command Context */
290 SilcServerCommandContext silc_server_command_alloc()
292 SilcServerCommandContext ctx = silc_calloc(1, sizeof(*ctx));
297 /* Free's the command context allocated before executing the command */
299 void silc_server_command_free(SilcServerCommandContext ctx)
302 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users + 1,
304 if (ctx->users < 1) {
306 silc_command_payload_free(ctx->payload);
308 silc_packet_context_free(ctx->packet);
310 silc_socket_free(ctx->sock); /* Decrease reference counter */
315 /* Duplicate Command Context by adding reference counter. The context won't
316 be free'd untill it hits zero. */
318 SilcServerCommandContext
319 silc_server_command_dup(SilcServerCommandContext ctx)
322 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users - 1,
327 /* Timeout for pending command. If reply to pending command never arrives
328 this is called to free resources. */
330 SILC_TASK_CALLBACK(silc_server_command_pending_timeout)
332 SilcServer server = app_context;
333 SilcServerCommandPending *reply = context;
334 SilcServerCommandReplyContext cmdr;
338 SILC_LOG_DEBUG(("Timeout pending command"));
340 /* Allocate temporary and bogus command reply context */
341 cmdr = silc_calloc(1, sizeof(*cmdr));
342 cmdr->server = server;
343 cmdr->ident = reply->ident;
345 /* Check for pending commands and mark to be exeucted */
347 silc_server_command_pending_check(server, reply->reply_cmd,
348 reply->ident, &cmdr->callbacks_count);
350 /* Create bogus command reply with an error inside */
352 silc_command_reply_payload_encode_va(reply->reply_cmd ? reply->reply_cmd :
353 SILC_COMMAND_RESERVED,
354 SILC_STATUS_ERR_TIMEDOUT, 0,
356 cmdr->payload = silc_command_payload_parse(tmpreply->data, tmpreply->len);
357 silc_buffer_free(tmpreply);
359 /* Call all callbacks. Same as SILC_SERVER_PENDING_EXEC macro. */
360 for (i = 0; i < cmdr->callbacks_count; i++)
361 if (cmdr->callbacks[i].callback)
362 (*cmdr->callbacks[i].callback)(cmdr->callbacks[i].context, cmdr);
364 silc_server_command_pending_del(server, reply->reply_cmd, reply->ident);
365 silc_server_command_reply_free(cmdr);
368 /* Add new pending command to be executed when reply to a command has been
369 received. The `reply_cmd' is the command that will call the `callback'
370 with `context' when reply has been received. It can be SILC_COMMAND_NONE
371 to match any command with the `ident'. If `ident' is non-zero
372 the `callback' will be executed when received reply with command
373 identifier `ident'. If there already exists pending command for the
374 specified command, ident, callback and context this function has no
377 bool silc_server_command_pending(SilcServer server,
378 SilcCommand reply_cmd,
380 SilcCommandCb callback,
383 return silc_server_command_pending_timed(server, reply_cmd, ident, callback,
387 /* Same as silc_server_command_pending with specific timeout for pending
388 commands. If the `timeout' is zero default timeout is used. */
390 bool silc_server_command_pending_timed(SilcServer server,
391 SilcCommand reply_cmd,
393 SilcCommandCb callback,
397 SilcServerCommandPending *reply;
399 /* Check whether identical pending already exists for same command,
400 ident, callback and callback context. If it does then it would be
401 error to register it again. */
402 silc_dlist_start(server->pending_commands);
403 while ((reply = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
404 if (reply->reply_cmd == reply_cmd && reply->ident == ident &&
405 reply->callback == callback && reply->context == context)
409 reply = silc_calloc(1, sizeof(*reply));
410 reply->reply_cmd = reply_cmd;
411 reply->ident = ident;
412 reply->context = context;
413 reply->callback = callback;
415 silc_schedule_task_add(server->schedule, 0,
416 silc_server_command_pending_timeout, reply,
417 timeout ? timeout : 10, 0,
418 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
419 silc_dlist_add(server->pending_commands, reply);
424 /* Deletes pending command by reply command type. */
426 void silc_server_command_pending_del(SilcServer server,
427 SilcCommand reply_cmd,
430 SilcServerCommandPending *r;
432 silc_dlist_start(server->pending_commands);
433 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
434 if ((r->reply_cmd == reply_cmd || (r->reply_cmd == SILC_COMMAND_NONE &&
436 && r->ident == ident) {
437 silc_dlist_del(server->pending_commands, r);
439 silc_schedule_task_del(server->schedule, r->timeout);
445 /* Checks for pending commands and marks callbacks to be called from
446 the command reply function. Returns TRUE if there were pending command. */
448 SilcServerCommandPendingCallbacks
449 silc_server_command_pending_check(SilcServer server,
452 SilcUInt32 *callbacks_count)
454 SilcServerCommandPending *r;
455 SilcServerCommandPendingCallbacks callbacks = NULL;
458 silc_dlist_start(server->pending_commands);
459 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
460 if ((r->reply_cmd == command || r->reply_cmd == SILC_COMMAND_NONE)
461 && r->ident == ident) {
462 callbacks = silc_realloc(callbacks, sizeof(*callbacks) * (i + 1));
463 callbacks[i].context = r->context;
464 callbacks[i].callback = r->callback;
465 r->reply_check = TRUE;
470 *callbacks_count = i;
474 /* Sends simple status message as command reply packet */
477 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
484 SILC_LOG_DEBUG(("Sending command status %d", status));
487 silc_command_reply_payload_encode_va(command, status, error,
488 silc_command_get_ident(cmd->payload),
490 silc_server_packet_send(cmd->server, cmd->sock,
491 SILC_PACKET_COMMAND_REPLY, 0,
492 buffer->data, buffer->len, FALSE);
493 silc_buffer_free(buffer);
496 /* Sends command status reply with one extra argument. The argument
497 type must be sent as argument. */
500 silc_server_command_send_status_data(SilcServerCommandContext cmd,
505 const unsigned char *arg,
510 SILC_LOG_DEBUG(("Sending command status %d", status));
513 silc_command_reply_payload_encode_va(command, status, 0,
514 silc_command_get_ident(cmd->payload),
515 1, arg_type, arg, arg_len);
516 silc_server_packet_send(cmd->server, cmd->sock,
517 SILC_PACKET_COMMAND_REPLY, 0,
518 buffer->data, buffer->len, FALSE);
519 silc_buffer_free(buffer);
522 /* This function can be called to check whether in the command reply
523 an error occurred. This function has no effect if this is called
524 when the command function was not called as pending command callback.
525 This returns TRUE if error had occurred. */
528 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
529 SilcServerCommandReplyContext cmdr,
532 if (!cmd->pending || !cmdr)
535 if (!silc_command_get_status(cmdr->payload, NULL, NULL)) {
538 /* Send the same command reply payload */
539 silc_command_set_command(cmdr->payload, silc_command_get(cmd->payload));
540 silc_command_set_ident(cmdr->payload,
541 silc_command_get_ident(cmd->payload));
542 buffer = silc_command_payload_encode_payload(cmdr->payload);
543 silc_server_packet_send(cmd->server, cmd->sock,
544 SILC_PACKET_COMMAND_REPLY, 0,
545 buffer->data, buffer->len, FALSE);
546 silc_buffer_free(buffer);
553 /* Server side of command WHOIS. */
555 SILC_SERVER_CMD_FUNC(whois)
557 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
558 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOIS, cmd, 1, 256);
559 silc_server_query_command(cmd->server, SILC_COMMAND_WHOIS, cmd);
560 silc_server_command_free(cmd);
563 /* Server side of command WHOWAS. */
565 SILC_SERVER_CMD_FUNC(whowas)
567 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
568 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOWAS, cmd, 1, 2);
569 silc_server_query_command(cmd->server, SILC_COMMAND_WHOWAS, cmd);
570 silc_server_command_free(cmd);
573 /* Server side of command IDENTIFY. */
575 SILC_SERVER_CMD_FUNC(identify)
577 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
578 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_IDENTIFY, cmd, 1, 256);
579 silc_server_query_command(cmd->server, SILC_COMMAND_IDENTIFY, cmd);
580 silc_server_command_free(cmd);
583 /* Server side of command NICK. Sets nickname for user. Setting
584 nickname causes generation of a new client ID for the client. The
585 new client ID is sent to the client after changing the nickname. */
587 SILC_SERVER_CMD_FUNC(nick)
589 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
590 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
591 SilcServer server = cmd->server;
592 SilcBuffer packet, nidp, oidp = NULL;
593 SilcClientID *new_id;
596 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
599 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
602 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_NICK, cmd, 1, 1);
605 nick = silc_argument_get_arg_type(cmd->args, 1, &nick_len);
607 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
608 SILC_STATUS_ERR_BAD_NICKNAME, 0);
613 if (silc_server_name_bad_chars(nick, nick_len) == TRUE) {
614 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
615 SILC_STATUS_ERR_BAD_NICKNAME, 0);
619 /* Check for same nickname */
620 if (!strcmp(client->nickname, nick)) {
621 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
625 /* Create new Client ID */
626 while (!silc_id_create_client_id(cmd->server, cmd->server->id,
628 cmd->server->md5hash, nick,
632 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
633 SILC_STATUS_ERR_BAD_NICKNAME, 0);
636 snprintf(&nick[strlen(nick) - 1], 1, "%d", nickfail);
639 /* Send notify about nickname change to our router. We send the new
640 ID and ask to replace it with the old one. If we are router the
641 packet is broadcasted. Send NICK_CHANGE notify. */
642 silc_server_send_notify_nick_change(server, SILC_PRIMARY_ROUTE(server),
643 SILC_BROADCAST(server), client->id,
646 /* Check if anyone is watching the old nickname */
647 if (server->server_type == SILC_ROUTER)
648 silc_server_check_watcher_list(server, client, nick,
649 SILC_NOTIFY_TYPE_NICK_CHANGE);
651 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
653 /* Remove old cache entry */
654 silc_idcache_del_by_context(server->local_list->clients, client);
656 silc_free(client->id);
659 silc_free(client->nickname);
660 client->nickname = strdup(nick);
662 /* Update client cache */
663 silc_idcache_add(server->local_list->clients, client->nickname,
664 client->id, (void *)client, 0, NULL);
666 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
668 /* Send NICK_CHANGE notify to the client's channels */
669 silc_server_send_notify_on_channels(server, NULL, client,
670 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
671 oidp->data, oidp->len,
672 nidp->data, nidp->len,
674 strlen(client->nickname));
676 /* Check if anyone is watching the new nickname */
677 if (server->server_type == SILC_ROUTER)
678 silc_server_check_watcher_list(server, client, NULL,
679 SILC_NOTIFY_TYPE_NICK_CHANGE);
682 /* Send the new Client ID as reply command back to client */
683 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_NICK,
684 SILC_STATUS_OK, 0, ident, 2,
685 2, nidp->data, nidp->len,
686 3, nick, strlen(nick));
687 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
688 0, packet->data, packet->len, FALSE);
690 silc_buffer_free(packet);
691 silc_buffer_free(nidp);
693 silc_buffer_free(oidp);
696 silc_server_command_free(cmd);
699 /* Sends the LIST command reply */
702 silc_server_command_list_send_reply(SilcServerCommandContext cmd,
703 SilcChannelEntry *lch,
704 SilcUInt32 lch_count,
705 SilcChannelEntry *gch,
706 SilcUInt32 gch_count)
709 SilcBuffer packet, idp;
710 SilcChannelEntry entry;
712 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
714 unsigned char usercount[4];
716 int valid_lcount = 0, valid_rcount = 0;
718 for (i = 0; i < lch_count; i++) {
719 if (lch[i]->mode & SILC_CHANNEL_MODE_SECRET)
724 for (i = 0; i < gch_count; i++) {
725 if (gch[i]->mode & SILC_CHANNEL_MODE_SECRET)
731 status = SILC_STATUS_OK;
732 if ((lch_count + gch_count) > 1)
733 status = SILC_STATUS_LIST_START;
736 for (i = 0, k = 0; i < lch_count; i++) {
742 status = SILC_STATUS_LIST_ITEM;
743 if (valid_lcount > 1 && k == valid_lcount - 1 && !valid_rcount)
744 status = SILC_STATUS_LIST_END;
746 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
748 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
750 memset(usercount, 0, sizeof(usercount));
752 topic = entry->topic;
753 users = silc_hash_table_count(entry->user_list);
754 SILC_PUT32_MSB(users, usercount);
759 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
761 2, idp->data, idp->len,
762 3, entry->channel_name,
763 strlen(entry->channel_name),
764 4, topic, topic ? strlen(topic) : 0,
766 silc_server_packet_send(cmd->server, cmd->sock,
767 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
769 silc_buffer_free(packet);
770 silc_buffer_free(idp);
775 for (i = 0, k = 0; i < gch_count; i++) {
781 status = SILC_STATUS_LIST_ITEM;
782 if (valid_rcount > 1 && k == valid_rcount - 1)
783 status = SILC_STATUS_LIST_END;
785 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
787 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
789 memset(usercount, 0, sizeof(usercount));
791 topic = entry->topic;
792 users = entry->user_count;
793 SILC_PUT32_MSB(users, usercount);
798 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
800 2, idp->data, idp->len,
801 3, entry->channel_name,
802 strlen(entry->channel_name),
803 4, topic, topic ? strlen(topic) : 0,
805 silc_server_packet_send(cmd->server, cmd->sock,
806 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
808 silc_buffer_free(packet);
809 silc_buffer_free(idp);
814 /* Server side of LIST command. This lists the channel of the requested
815 server. Secret channels are not listed. */
817 SILC_SERVER_CMD_FUNC(list)
819 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
820 SilcServer server = cmd->server;
821 SilcChannelID *channel_id = NULL;
824 SilcChannelEntry *lchannels = NULL, *gchannels = NULL;
825 SilcUInt32 lch_count = 0, gch_count = 0;
827 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LIST, cmd, 0, 1);
829 /* If we are normal server, send the command to router, since we
830 want to know all channels in the network. */
831 if (!cmd->pending && server->server_type != SILC_ROUTER &&
832 !server->standalone) {
834 SilcUInt16 old_ident;
836 old_ident = silc_command_get_ident(cmd->payload);
837 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
838 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
839 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
840 SILC_PACKET_COMMAND, cmd->packet->flags,
841 tmpbuf->data, tmpbuf->len, TRUE);
843 /* Reprocess this packet after received reply from router */
844 silc_server_command_pending(server, SILC_COMMAND_LIST,
845 silc_command_get_ident(cmd->payload),
846 silc_server_command_list,
847 silc_server_command_dup(cmd));
849 silc_command_set_ident(cmd->payload, old_ident);
850 silc_buffer_free(tmpbuf);
855 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
857 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
859 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LIST,
860 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
865 /* Get the channels from local list */
866 lchannels = silc_idlist_get_channels(server->local_list, channel_id,
869 /* Get the channels from global list */
870 gchannels = silc_idlist_get_channels(server->global_list, channel_id,
874 silc_server_command_list_send_reply(cmd, lchannels, lch_count,
875 gchannels, gch_count);
877 silc_free(lchannels);
878 silc_free(gchannels);
881 silc_server_command_free(cmd);
884 /* Server side of TOPIC command. Sets topic for channel and/or returns
885 current topic to client. */
887 SILC_SERVER_CMD_FUNC(topic)
889 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
890 SilcServer server = cmd->server;
891 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
892 SilcChannelID *channel_id;
893 SilcChannelEntry channel;
894 SilcChannelClientEntry chl;
895 SilcBuffer packet, idp;
897 SilcUInt32 argc, tmp_len;
898 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
900 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
903 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_TOPIC, cmd, 1, 2);
905 argc = silc_argument_get_arg_num(cmd->args);
908 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
910 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
911 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
914 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
916 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
917 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
921 /* Check whether the channel exists */
922 channel = silc_idlist_find_channel_by_id(server->local_list,
925 channel = silc_idlist_find_channel_by_id(server->global_list,
928 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
929 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
937 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
939 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
940 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
945 if (strlen(tmp) > 256) {
946 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
947 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
952 /* See whether the client is on channel and has rights to change topic */
953 if (!silc_server_client_on_channel(client, channel, &chl)) {
954 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
955 SILC_STATUS_ERR_NOT_ON_CHANNEL,
960 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
961 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
962 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
963 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
964 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
969 if (!channel->topic || strcmp(channel->topic, tmp)) {
970 /* Set the topic for channel */
971 silc_free(channel->topic);
972 channel->topic = strdup(tmp);
974 /* Send TOPIC_SET notify type to the network */
975 silc_server_send_notify_topic_set(server, SILC_PRIMARY_ROUTE(server),
976 SILC_BROADCAST(server), channel,
977 client->id, SILC_ID_CLIENT,
980 /* Send notify about topic change to all clients on the channel */
981 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
982 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
983 SILC_NOTIFY_TYPE_TOPIC_SET, 2,
986 strlen(channel->topic));
987 silc_buffer_free(idp);
991 /* Send the topic to client as reply packet */
992 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
993 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
994 SILC_STATUS_OK, 0, ident, 2,
995 2, idp->data, idp->len,
998 strlen(channel->topic) : 0);
999 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1000 0, packet->data, packet->len, FALSE);
1002 silc_buffer_free(packet);
1003 silc_buffer_free(idp);
1004 silc_free(channel_id);
1007 silc_server_command_free(cmd);
1010 /* Server side of INVITE command. Invites some client to join some channel.
1011 This command is also used to manage the invite list of the channel. */
1013 SILC_SERVER_CMD_FUNC(invite)
1015 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1016 SilcServer server = cmd->server;
1017 SilcSocketConnection sock = cmd->sock, dest_sock;
1018 SilcChannelClientEntry chl;
1019 SilcClientEntry sender, dest;
1020 SilcClientID *dest_id = NULL;
1021 SilcChannelEntry channel;
1022 SilcChannelID *channel_id = NULL;
1023 SilcIDListData idata;
1024 SilcArgumentPayload args;
1025 SilcHashTableList htl;
1026 SilcBuffer packet, list, tmp2;
1028 SilcUInt32 len, type;
1029 SilcUInt16 argc = 0, ident = silc_command_get_ident(cmd->payload);
1031 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INVITE, cmd, 1, 4);
1033 /* Get Channel ID */
1034 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1036 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1037 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
1040 channel_id = silc_id_payload_parse_id(tmp, len, NULL);
1042 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1043 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
1047 /* Get the channel entry */
1048 channel = silc_idlist_find_channel_by_id(server->local_list,
1051 channel = silc_idlist_find_channel_by_id(server->global_list,
1054 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1055 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
1061 /* Check whether the sender of this command is on the channel. */
1062 sender = (SilcClientEntry)sock->user_data;
1063 if (!sender || !silc_server_client_on_channel(sender, channel, &chl)) {
1064 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1065 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
1069 /* Check whether the channel is invite-only channel. If yes then the
1070 sender of this command must be at least channel operator. */
1071 if (channel->mode & SILC_CHANNEL_MODE_INVITE &&
1072 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1073 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1074 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1075 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
1080 /* Get destination client ID */
1081 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
1085 dest_id = silc_id_payload_parse_id(tmp, len, NULL);
1087 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1088 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
1092 /* Get the client entry */
1093 dest = silc_server_query_client(server, dest_id, FALSE, &resolve);
1095 if (server->server_type != SILC_SERVER || !resolve || cmd->pending) {
1096 silc_server_command_send_status_reply(
1097 cmd, SILC_COMMAND_INVITE,
1098 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID, 0);
1102 /* The client info is being resolved. Reprocess this packet after
1103 receiving the reply to the query. */
1104 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1106 silc_server_command_invite,
1107 silc_server_command_dup(cmd));
1108 cmd->pending = TRUE;
1109 silc_free(channel_id);
1114 /* Check whether the requested client is already on the channel. */
1115 if (silc_server_client_on_channel(dest, channel, NULL)) {
1116 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1117 SILC_STATUS_ERR_USER_ON_CHANNEL,
1122 /* Get route to the client */
1123 dest_sock = silc_server_get_client_route(server, NULL, 0, dest_id,
1126 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1127 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1132 /* Add the client to the invite list */
1134 /* Allocate hash table for invite list if it doesn't exist yet */
1135 if (!channel->invite_list)
1136 channel->invite_list = silc_hash_table_alloc(0, silc_hash_ptr,
1140 /* Check if the ID is in the list already */
1141 silc_hash_table_list(channel->invite_list, &htl);
1142 while (silc_hash_table_get(&htl, (void **)&type, (void **)&tmp2)) {
1143 if (type == 3 && !memcmp(tmp2->data, tmp, len)) {
1148 silc_hash_table_list_reset(&htl);
1150 /* Add new Client ID to invite list */
1152 list = silc_buffer_alloc_size(len);
1153 silc_buffer_put(list, tmp, len);
1154 silc_hash_table_add(channel->invite_list, (void *)3, list);
1157 if (!(dest->mode & SILC_UMODE_BLOCK_INVITE)) {
1158 /* Send notify to the client that is invited to the channel */
1159 SilcBuffer idp, idp2;
1160 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
1161 idp2 = silc_id_payload_encode(sender->id, SILC_ID_CLIENT);
1162 silc_server_send_notify_dest(server, dest_sock, FALSE, dest_id,
1164 SILC_NOTIFY_TYPE_INVITE, 3,
1165 idp->data, idp->len,
1166 channel->channel_name,
1167 strlen(channel->channel_name),
1168 idp2->data, idp2->len);
1169 silc_buffer_free(idp);
1170 silc_buffer_free(idp2);
1174 /* Get the invite information */
1175 tmp = silc_argument_get_arg_type(cmd->args, 4, &len);
1177 /* Parse the arguments to see they are constructed correctly */
1178 SILC_GET16_MSB(argc, tmp);
1179 args = silc_argument_payload_parse(tmp + 2, len - 2, argc);
1181 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1182 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1187 /* Get the type of action */
1188 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
1189 if (tmp && len == 1) {
1190 if (tmp[0] == 0x00) {
1191 /* Allocate hash table for invite list if it doesn't exist yet */
1192 if (!channel->invite_list)
1193 channel->invite_list = silc_hash_table_alloc(0, silc_hash_ptr,
1197 /* Check for resource limit */
1198 if (silc_hash_table_count(channel->invite_list) > 64) {
1199 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1200 SILC_STATUS_ERR_RESOURCE_LIMIT,
1206 /* Now add or delete the information. */
1207 silc_server_inviteban_process(server, channel->invite_list,
1208 (SilcUInt8)tmp[0], args);
1210 silc_argument_payload_free(args);
1213 /* Encode invite list */
1215 if (channel->invite_list) {
1216 list = silc_buffer_alloc_size(2);
1217 silc_buffer_format(list,
1218 SILC_STR_UI_SHORT(silc_hash_table_count(
1219 channel->invite_list)),
1221 silc_hash_table_list(channel->invite_list, &htl);
1222 while (silc_hash_table_get(&htl, (void **)&type, (void **)&tmp2)) {
1224 list = silc_argument_payload_encode_one(list, (char *)tmp2,
1225 strlen((char *)tmp2), type);
1227 list = silc_argument_payload_encode_one(list, tmp2->data, tmp2->len,
1230 silc_hash_table_list_reset(&htl);
1233 /* Send notify to the primary router */
1234 silc_server_send_notify_invite(
1235 server, SILC_PRIMARY_ROUTE(server),
1236 SILC_BROADCAST(server), channel, sender->id,
1237 silc_argument_get_arg_type(cmd->args, 3, NULL),
1240 /* Send command reply */
1241 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1242 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
1243 SILC_STATUS_OK, 0, ident, 2,
1245 3, list ? list->data : NULL,
1246 list ? list->len : 0);
1247 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
1248 packet->data, packet->len, FALSE);
1249 silc_buffer_free(packet);
1250 silc_buffer_free(list);
1254 silc_free(channel_id);
1255 silc_server_command_free(cmd);
1259 SilcSocketConnection sock;
1263 /* Quits connection to client. This gets called if client won't
1264 close the connection even when it has issued QUIT command. */
1266 SILC_TASK_CALLBACK(silc_server_command_quit_cb)
1268 SilcServer server = app_context;
1269 QuitInternal q = (QuitInternal)context;
1271 /* Free all client specific data, such as client entry and entires
1272 on channels this client may be on. */
1273 silc_server_free_client_data(server, q->sock, q->sock->user_data,
1275 q->sock->user_data = NULL;
1277 /* Close the connection on our side */
1278 silc_server_close_connection(server, q->sock);
1280 silc_socket_free(q->sock);
1281 silc_free(q->signoff);
1285 /* Quits SILC session. This is the normal way to disconnect client. */
1287 SILC_SERVER_CMD_FUNC(quit)
1289 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1290 SilcServer server = cmd->server;
1291 SilcSocketConnection sock = cmd->sock;
1293 unsigned char *tmp = NULL;
1296 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_QUIT, cmd, 0, 1);
1298 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
1302 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1306 q = silc_calloc(1, sizeof(*q));
1307 q->sock = silc_socket_dup(sock);
1308 q->signoff = tmp ? strdup(tmp) : NULL;
1310 /* We quit the connection with little timeout */
1311 silc_schedule_task_add(server->schedule, sock->sock,
1312 silc_server_command_quit_cb, (void *)q,
1313 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
1316 silc_server_command_free(cmd);
1319 /* Server side of command KILL. This command is used by router operator
1320 to remove an client from the SILC Network temporarily. */
1322 SILC_SERVER_CMD_FUNC(kill)
1324 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1325 SilcServer server = cmd->server;
1326 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
1327 SilcClientEntry remote_client;
1328 SilcClientID *client_id = NULL;
1329 unsigned char *tmp, *comment, *auth;
1330 SilcUInt32 tmp_len, tmp_len2, auth_len;
1332 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_KILL, cmd, 1, 3);
1334 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
1337 /* Get authentication payload if present */
1338 auth = silc_argument_get_arg_type(cmd->args, 3, &auth_len);
1341 /* Router operator killing */
1343 /* KILL command works only on router */
1344 if (server->server_type != SILC_ROUTER) {
1345 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1346 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
1350 /* Check whether client has the permissions. */
1351 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1352 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1353 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
1358 /* Get the client ID */
1359 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1361 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1362 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1366 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1368 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1369 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1374 /* Get the client entry */
1375 remote_client = silc_idlist_find_client_by_id(server->local_list,
1376 client_id, TRUE, NULL);
1377 if (!remote_client) {
1378 remote_client = silc_idlist_find_client_by_id(server->global_list,
1379 client_id, TRUE, NULL);
1380 if (!remote_client) {
1381 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1382 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1389 comment = silc_argument_get_arg_type(cmd->args, 2, &tmp_len2);
1390 if (comment && tmp_len2 > 128) {
1392 comment[127] = '\0';
1395 /* If authentication data is provided then verify that killing is
1397 if (auth && auth_len) {
1398 SilcSocketConnection sock;
1400 if (!SILC_IS_LOCAL(remote_client) || !remote_client->data.public_key) {
1401 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1402 SILC_STATUS_ERR_OPERATION_ALLOWED,
1407 /* Verify the signature */
1408 if (!silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
1409 remote_client->data.public_key, 0,
1410 server->sha1hash, remote_client->id,
1412 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1413 SILC_STATUS_ERR_AUTH_FAILED,
1418 /* Send reply to the sender */
1419 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1422 /* Do normal signoff for the destination client */
1423 sock = remote_client->connection;
1424 silc_server_free_client_data(server, NULL, remote_client, TRUE,
1426 (unsigned char *)"Killed");
1428 silc_server_close_connection(server, sock);
1430 /* Router operator killing */
1432 /* Send reply to the sender */
1433 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1436 /* Check if anyone is watching this nickname */
1437 if (server->server_type == SILC_ROUTER)
1438 silc_server_check_watcher_list(server, client, NULL,
1439 SILC_NOTIFY_TYPE_KILLED);
1441 /* Now do the killing */
1442 silc_server_kill_client(server, remote_client, comment, client->id,
1447 silc_free(client_id);
1448 silc_server_command_free(cmd);
1451 /* Server side of command INFO. This sends information about us to
1452 the client. If client requested specific server we will send the
1453 command to that server. */
1455 SILC_SERVER_CMD_FUNC(info)
1457 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1458 SilcServer server = cmd->server;
1459 SilcBuffer packet, idp;
1462 char *dest_server, *server_info = NULL, *server_name;
1463 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1464 SilcServerEntry entry = NULL;
1465 SilcServerID *server_id = NULL;
1467 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INFO, cmd, 0, 2);
1469 /* Get server name */
1470 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
1473 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
1475 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1477 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
1478 SILC_STATUS_ERR_NO_SERVER_ID, 0);
1484 /* Check whether we have this server cached */
1485 entry = silc_idlist_find_server_by_id(server->local_list,
1486 server_id, TRUE, NULL);
1488 entry = silc_idlist_find_server_by_id(server->global_list,
1489 server_id, TRUE, NULL);
1490 if (!entry && server->server_type != SILC_SERVER) {
1491 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
1492 SILC_STATUS_ERR_NO_SUCH_SERVER,
1499 /* Some buggy servers has sent request to router about themselves. */
1500 if (server->server_type != SILC_SERVER && cmd->sock->user_data == entry)
1503 if ((!dest_server && !server_id && !entry) || (entry &&
1504 entry == server->id_entry) ||
1505 (dest_server && !cmd->pending &&
1506 !strncasecmp(dest_server, server->server_name, strlen(dest_server)))) {
1507 /* Send our reply */
1508 char info_string[256];
1510 memset(info_string, 0, sizeof(info_string));
1511 snprintf(info_string, sizeof(info_string),
1512 "location: %s server: %s admin: %s <%s>",
1513 server->config->server_info->location,
1514 server->config->server_info->server_type,
1515 server->config->server_info->admin,
1516 server->config->server_info->email);
1518 server_info = info_string;
1519 entry = server->id_entry;
1521 /* Check whether we have this server cached */
1522 if (!entry && dest_server) {
1523 entry = silc_idlist_find_server_by_name(server->global_list,
1524 dest_server, TRUE, NULL);
1526 entry = silc_idlist_find_server_by_name(server->local_list,
1527 dest_server, TRUE, NULL);
1531 if (!cmd->pending &&
1532 server->server_type != SILC_SERVER && entry && !entry->server_info) {
1533 /* Send to the server */
1535 SilcUInt16 old_ident;
1537 old_ident = silc_command_get_ident(cmd->payload);
1538 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
1539 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1541 silc_server_packet_send(server, entry->connection,
1542 SILC_PACKET_COMMAND, cmd->packet->flags,
1543 tmpbuf->data, tmpbuf->len, TRUE);
1545 /* Reprocess this packet after received reply from router */
1546 silc_server_command_pending(server, SILC_COMMAND_INFO,
1547 silc_command_get_ident(cmd->payload),
1548 silc_server_command_info,
1549 silc_server_command_dup(cmd));
1550 cmd->pending = TRUE;
1551 silc_command_set_ident(cmd->payload, old_ident);
1552 silc_buffer_free(tmpbuf);
1556 if (!entry && !cmd->pending && !server->standalone) {
1557 /* Send to the primary router */
1559 SilcUInt16 old_ident;
1561 old_ident = silc_command_get_ident(cmd->payload);
1562 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
1563 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1565 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
1566 SILC_PACKET_COMMAND, cmd->packet->flags,
1567 tmpbuf->data, tmpbuf->len, TRUE);
1569 /* Reprocess this packet after received reply from router */
1570 silc_server_command_pending(server, SILC_COMMAND_INFO,
1571 silc_command_get_ident(cmd->payload),
1572 silc_server_command_info,
1573 silc_server_command_dup(cmd));
1574 cmd->pending = TRUE;
1575 silc_command_set_ident(cmd->payload, old_ident);
1576 silc_buffer_free(tmpbuf);
1581 silc_free(server_id);
1584 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
1585 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
1589 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
1591 server_info = entry->server_info;
1592 server_name = entry->server_name;
1594 /* Send the reply */
1595 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
1596 SILC_STATUS_OK, 0, ident, 3,
1597 2, idp->data, idp->len,
1599 strlen(server_name),
1602 strlen(server_info) : 0);
1603 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
1604 packet->data, packet->len, FALSE);
1606 silc_buffer_free(packet);
1607 silc_buffer_free(idp);
1610 silc_server_command_free(cmd);
1613 /* Server side of command PING. This just replies to the ping. */
1615 SILC_SERVER_CMD_FUNC(ping)
1617 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1618 SilcServer server = cmd->server;
1621 SilcServerID *server_id = NULL;
1623 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PING, cmd, 1, 1);
1626 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1628 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1629 SILC_STATUS_ERR_NO_SERVER_ID, 0);
1632 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1636 if (SILC_ID_SERVER_COMPARE(server_id, server->id)) {
1637 /* Send our reply */
1638 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1641 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1642 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
1647 silc_free(server_id);
1648 silc_server_command_free(cmd);
1651 /* Server side of command STATS. */
1653 SILC_SERVER_CMD_FUNC(stats)
1655 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1656 SilcServer server = cmd->server;
1657 SilcServerID *server_id;
1660 SilcBuffer packet, stats;
1661 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1664 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_STATS, cmd, 1, 1);
1667 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1669 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1670 SILC_STATUS_ERR_NO_SERVER_ID, 0);
1673 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1677 /* The ID must be ours */
1678 if (!SILC_ID_SERVER_COMPARE(server->id, server_id)) {
1679 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1680 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
1681 silc_free(server_id);
1684 silc_free(server_id);
1686 /* If we are router then just send everything we got. If we are normal
1687 server then we'll send this to our router to get all the latest
1688 statistical information. */
1689 if (!cmd->pending && server->server_type != SILC_ROUTER &&
1690 !server->standalone) {
1691 /* Send request to our router */
1692 SilcBuffer idp = silc_id_payload_encode(server->router->id,
1694 packet = silc_command_payload_encode_va(SILC_COMMAND_STATS,
1695 ++server->cmd_ident, 1,
1696 1, idp->data, idp->len);
1697 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
1698 SILC_PACKET_COMMAND, 0, packet->data,
1699 packet->len, FALSE);
1701 /* Reprocess this packet after received reply from router */
1702 silc_server_command_pending(server, SILC_COMMAND_STATS,
1704 silc_server_command_stats,
1705 silc_server_command_dup(cmd));
1706 cmd->pending = TRUE;
1707 silc_buffer_free(packet);
1708 silc_buffer_free(idp);
1712 /* Send our reply to sender */
1713 uptime = time(NULL) - server->starttime;
1715 stats = silc_buffer_alloc_size(60);
1716 silc_buffer_format(stats,
1717 SILC_STR_UI_INT(server->starttime),
1718 SILC_STR_UI_INT(uptime),
1719 SILC_STR_UI_INT(server->stat.my_clients),
1720 SILC_STR_UI_INT(server->stat.my_channels),
1721 SILC_STR_UI_INT(server->stat.my_server_ops),
1722 SILC_STR_UI_INT(server->stat.my_router_ops),
1723 SILC_STR_UI_INT(server->stat.cell_clients),
1724 SILC_STR_UI_INT(server->stat.cell_channels),
1725 SILC_STR_UI_INT(server->stat.cell_servers),
1726 SILC_STR_UI_INT(server->stat.clients),
1727 SILC_STR_UI_INT(server->stat.channels),
1728 SILC_STR_UI_INT(server->stat.servers),
1729 SILC_STR_UI_INT(server->stat.routers),
1730 SILC_STR_UI_INT(server->stat.server_ops),
1731 SILC_STR_UI_INT(server->stat.router_ops),
1734 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_STATS,
1735 SILC_STATUS_OK, 0, ident, 2,
1737 3, stats->data, stats->len);
1738 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1739 0, packet->data, packet->len, FALSE);
1740 silc_buffer_free(packet);
1741 silc_buffer_free(stats);
1744 silc_server_command_free(cmd);
1747 /* Internal routine to join channel. The channel sent to this function
1748 has been either created or resolved from ID lists. This joins the sent
1749 client to the channel. */
1751 static void silc_server_command_join_channel(SilcServer server,
1752 SilcServerCommandContext cmd,
1753 SilcChannelEntry channel,
1754 SilcClientID *client_id,
1758 const unsigned char *auth,
1759 SilcUInt32 auth_len)
1761 SilcSocketConnection sock = cmd->sock;
1763 SilcUInt32 tmp_len, user_count;
1764 unsigned char *passphrase = NULL, mode[4], tmp2[4], tmp3[4];
1765 SilcClientEntry client;
1766 SilcChannelClientEntry chl;
1767 SilcBuffer reply, chidp, clidp, keyp = NULL;
1768 SilcBuffer user_list, mode_list, invite_list, ban_list;
1769 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1770 char check[512], check2[512];
1771 bool founder = FALSE;
1773 SilcBuffer fkey = NULL;
1776 SILC_LOG_DEBUG(("Joining client to channel"));
1781 /* Get the client entry */
1782 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
1783 client = (SilcClientEntry)sock->user_data;
1787 client = silc_server_query_client(server, client_id, FALSE,
1790 if (!resolve || cmd->pending) {
1791 silc_server_command_send_status_reply(
1792 cmd, SILC_COMMAND_JOIN,
1793 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
1797 /* The client info is being resolved. Reprocess this packet after
1798 receiving the reply to the query. */
1799 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1801 silc_server_command_join,
1802 silc_server_command_dup(cmd));
1803 cmd->pending = TRUE;
1807 if (auth && auth_len && !client->data.public_key) {
1808 if (cmd->pending == 2)
1811 /* We must retrieve the detached client's public key by sending
1812 GETKEY command. Reprocess this packet after receiving the key */
1813 clidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1814 silc_server_send_command(server, cmd->sock,
1815 SILC_COMMAND_GETKEY, ++server->cmd_ident,
1816 1, 1, clidp->data, clidp->len);
1817 silc_buffer_free(clidp);
1818 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
1820 silc_server_command_join,
1821 silc_server_command_dup(cmd));
1826 cmd->pending = FALSE;
1830 * Check founder auth payload if provided. If client can gain founder
1831 * privileges it can override various conditions on joining the channel,
1832 * and can have directly the founder mode set on the channel.
1834 if (auth && auth_len && channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
1835 SilcIDListData idata = (SilcIDListData)client;
1836 SilcChannelClientEntry chl2;
1837 SilcHashTableList htl;
1839 if (channel->founder_key && idata->public_key &&
1840 silc_pkcs_public_key_compare(channel->founder_key,
1841 idata->public_key)) {
1842 /* Check whether the client is to become founder */
1843 if (silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
1844 channel->founder_key, 0, server->sha1hash,
1845 client->id, SILC_ID_CLIENT)) {
1847 /* There cannot be anyone else as founder on the channel now. This
1848 client is definitely the founder due to this authentication */
1849 silc_hash_table_list(channel->user_list, &htl);
1850 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
1851 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
1852 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
1853 silc_server_force_cumode_change(server, NULL, channel, chl2,
1857 silc_hash_table_list_reset(&htl);
1859 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
1866 * Check channel modes
1870 memset(check, 0, sizeof(check));
1871 memset(check2, 0, sizeof(check2));
1872 silc_strncat(check, sizeof(check),
1873 client->nickname, strlen(client->nickname));
1874 silc_strncat(check, sizeof(check), "!", 1);
1875 silc_strncat(check, sizeof(check),
1876 client->username, strlen(client->username));
1877 if (!strchr(client->username, '@')) {
1878 silc_strncat(check, sizeof(check), "@", 1);
1879 silc_strncat(check, sizeof(check),
1880 cmd->sock->hostname, strlen(cmd->sock->hostname));
1883 silc_strncat(check2, sizeof(check2),
1884 client->nickname, strlen(client->nickname));
1885 if (!strchr(client->nickname, '@')) {
1886 silc_strncat(check2, sizeof(check2), "@", 1);
1887 silc_strncat(check2, sizeof(check2),
1888 server->server_name, strlen(server->server_name));
1890 silc_strncat(check2, sizeof(check2), "!", 1);
1891 silc_strncat(check2, sizeof(check2),
1892 client->username, strlen(client->username));
1893 if (!strchr(client->username, '@')) {
1894 silc_strncat(check2, sizeof(check2), "@", 1);
1895 silc_strncat(check2, sizeof(check2),
1896 cmd->sock->hostname, strlen(cmd->sock->hostname));
1899 /* Check invite list if channel is invite-only channel */
1900 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
1901 if (!channel->invite_list ||
1902 (!silc_server_inviteban_match(server, channel->invite_list,
1904 !silc_server_inviteban_match(server, channel->invite_list,
1905 2, client->data.public_key) &&
1906 !silc_server_inviteban_match(server, channel->invite_list,
1908 !silc_server_inviteban_match(server, channel->invite_list,
1910 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
1911 SILC_STATUS_ERR_NOT_INVITED, 0);
1916 /* Check ban list if it exists. If the client's nickname, server,
1917 username and/or hostname is in the ban list the access to the
1918 channel is denied. */
1919 if (channel->ban_list) {
1920 if (silc_server_inviteban_match(server, channel->invite_list,
1922 silc_server_inviteban_match(server, channel->invite_list,
1923 2, client->data.public_key) ||
1924 !silc_server_inviteban_match(server, channel->invite_list,
1926 !silc_server_inviteban_match(server, channel->invite_list,
1928 silc_server_command_send_status_reply(
1929 cmd, SILC_COMMAND_JOIN,
1930 SILC_STATUS_ERR_BANNED_FROM_CHANNEL, 0);
1935 /* Check user count limit if set. */
1936 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT) {
1937 if (silc_hash_table_count(channel->user_list) + 1 >
1938 channel->user_limit) {
1939 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
1940 SILC_STATUS_ERR_CHANNEL_IS_FULL,
1947 /* Check the channel passphrase if set. */
1948 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
1949 /* Get passphrase */
1950 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
1952 passphrase = silc_memdup(tmp, tmp_len);
1954 if (!passphrase || !channel->passphrase ||
1955 memcmp(passphrase, channel->passphrase, strlen(channel->passphrase))) {
1956 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
1957 SILC_STATUS_ERR_BAD_PASSWORD, 0);
1963 * Client is allowed to join to the channel. Make it happen.
1966 /* Check whether the client already is on the channel */
1967 if (silc_server_client_on_channel(client, channel, NULL)) {
1968 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
1969 SILC_STATUS_ERR_USER_ON_CHANNEL, 0);
1973 /* Generate new channel key as protocol dictates */
1975 if (!silc_server_create_channel_key(server, channel, 0))
1978 /* Send the channel key. This is broadcasted to the channel but is not
1979 sent to the client who is joining to the channel. */
1980 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY))
1981 silc_server_send_channel_key(server, NULL, channel,
1982 server->server_type == SILC_ROUTER ?
1983 FALSE : !server->standalone);
1986 /* Join the client to the channel by adding it to channel's user list.
1987 Add also the channel to client entry's channels list for fast cross-
1989 chl = silc_calloc(1, sizeof(*chl));
1991 chl->client = client;
1992 chl->channel = channel;
1993 silc_hash_table_add(channel->user_list, client, chl);
1994 silc_hash_table_add(client->channels, channel, chl);
1995 channel->user_count++;
1996 channel->disabled = FALSE;
1998 /* Get users on the channel */
1999 silc_server_get_users_on_channel(server, channel, &user_list, &mode_list,
2002 /* Encode Client ID Payload of the original client who wants to join */
2003 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2005 /* Encode command reply packet */
2006 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2007 SILC_PUT32_MSB(channel->mode, mode);
2008 SILC_PUT32_MSB(created, tmp2);
2009 SILC_PUT32_MSB(user_count, tmp3);
2011 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2012 tmp = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2013 cipher = silc_cipher_get_name(channel->channel_key);
2014 keyp = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
2017 strlen(cipher), cipher,
2018 channel->key_len / 8, channel->key);
2022 if (channel->founder_key)
2023 fkey = silc_pkcs_public_key_payload_encode(channel->founder_key);
2025 /* Encode invite list */
2027 if (channel->invite_list) {
2028 SilcHashTableList htl;
2030 invite_list = silc_buffer_alloc_size(2);
2031 silc_buffer_format(invite_list,
2032 SILC_STR_UI_SHORT(silc_hash_table_count(
2033 channel->invite_list)),
2036 silc_hash_table_list(channel->invite_list, &htl);
2037 while (silc_hash_table_get(&htl, (void **)&tmp_len, (void **)&reply)) {
2039 invite_list = silc_argument_payload_encode_one(invite_list,
2041 strlen((char *)reply),
2044 invite_list = silc_argument_payload_encode_one(invite_list,
2046 reply->len, tmp_len);
2048 silc_hash_table_list_reset(&htl);
2051 /* Encode ban list */
2053 if (channel->ban_list) {
2054 SilcHashTableList htl;
2056 ban_list = silc_buffer_alloc_size(2);
2057 silc_buffer_format(ban_list,
2058 SILC_STR_UI_SHORT(silc_hash_table_count(
2059 channel->ban_list)),
2062 silc_hash_table_list(channel->ban_list, &htl);
2063 while (silc_hash_table_get(&htl, (void **)&tmp_len, (void **)&reply)) {
2065 ban_list = silc_argument_payload_encode_one(ban_list,
2067 strlen((char *)reply),
2070 ban_list = silc_argument_payload_encode_one(ban_list,
2072 reply->len, tmp_len);
2074 silc_hash_table_list_reset(&htl);
2078 silc_command_reply_payload_encode_va(SILC_COMMAND_JOIN,
2079 SILC_STATUS_OK, 0, ident, 14,
2080 2, channel->channel_name,
2081 strlen(channel->channel_name),
2082 3, chidp->data, chidp->len,
2083 4, clidp->data, clidp->len,
2086 7, keyp ? keyp->data : NULL,
2087 keyp ? keyp->len : 0,
2088 8, ban_list ? ban_list->data : NULL,
2089 ban_list ? ban_list->len : 0,
2090 9, invite_list ? invite_list->data :
2092 invite_list ? invite_list->len : 0,
2095 strlen(channel->topic) : 0,
2096 11, silc_hmac_get_name(channel->hmac),
2097 strlen(silc_hmac_get_name(channel->
2100 13, user_list->data, user_list->len,
2101 14, mode_list->data,
2103 15, fkey ? fkey->data : NULL,
2104 fkey ? fkey->len : 0);
2106 /* Send command reply */
2107 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
2108 reply->data, reply->len, FALSE);
2110 /* Send JOIN notify to locally connected clients on the channel. If
2111 we are normal server then router will send or have sent JOIN notify
2112 already. However since we've added the client already to our channel
2113 we'll ignore it (in packet_receive.c) so we must send it here. If
2114 we are router then this will send it to local clients and local
2116 SILC_LOG_DEBUG(("Send JOIN notify to channel"));
2117 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2118 SILC_NOTIFY_TYPE_JOIN, 2,
2119 clidp->data, clidp->len,
2120 chidp->data, chidp->len);
2122 /* Update statistics */
2123 server->stat.my_chanclients++;
2124 if (server->server_type == SILC_ROUTER) {
2125 server->stat.cell_chanclients++;
2126 server->stat.chanclients++;
2129 if (!cmd->pending) {
2130 /* Send JOIN notify packet to our primary router */
2131 silc_server_send_notify_join(server, SILC_PRIMARY_ROUTE(server),
2132 SILC_BROADCAST(server), channel, client->id);
2135 /* Distribute the channel key to all backup routers. */
2136 silc_server_backup_send(server, NULL, SILC_PACKET_CHANNEL_KEY, 0,
2137 keyp->data, keyp->len, FALSE, TRUE);
2139 /* If client became founder by providing correct founder auth data
2140 notify the mode change to the channel. */
2142 SILC_PUT32_MSB(chl->mode, mode);
2143 SILC_LOG_DEBUG(("Send CUMODE_CHANGE notify to channel"));
2144 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2145 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
2146 clidp->data, clidp->len,
2147 mode, 4, clidp->data, clidp->len,
2148 fkey ? fkey->data : NULL,
2149 fkey ? fkey->len : 0);
2153 /* Set CUMODE notify type to network */
2155 silc_server_send_notify_cumode(server, SILC_PRIMARY_ROUTE(server),
2156 SILC_BROADCAST(server), channel,
2157 chl->mode, client->id, SILC_ID_CLIENT,
2158 client->id, channel->founder_key);
2160 silc_buffer_free(reply);
2161 silc_buffer_free(clidp);
2162 silc_buffer_free(chidp);
2163 silc_buffer_free(keyp);
2164 silc_buffer_free(user_list);
2165 silc_buffer_free(mode_list);
2166 silc_buffer_free(fkey);
2167 silc_buffer_free(invite_list);
2168 silc_buffer_free(ban_list);
2171 silc_free(passphrase);
2174 /* Server side of command JOIN. Joins client into requested channel. If
2175 the channel does not exist it will be created. */
2177 SILC_SERVER_CMD_FUNC(join)
2179 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2180 SilcServer server = cmd->server;
2181 unsigned char *auth;
2182 SilcUInt32 tmp_len, auth_len;
2183 char *tmp, *channel_name = NULL, *cipher, *hmac;
2184 SilcChannelEntry channel;
2185 SilcUInt32 umode = 0;
2186 bool created = FALSE, create_key = TRUE;
2187 SilcClientID *client_id;
2189 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_JOIN, cmd, 2, 6);
2191 /* Get channel name */
2192 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2194 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2195 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2202 channel_name[255] = '\0';
2204 if (silc_server_name_bad_chars(channel_name, tmp_len) == TRUE) {
2205 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2206 SILC_STATUS_ERR_BAD_CHANNEL, 0);
2210 /* Get Client ID of the client who is joining to the channel */
2211 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2213 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2214 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2218 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
2220 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2221 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2226 /* Get cipher, hmac name and auth payload */
2227 cipher = silc_argument_get_arg_type(cmd->args, 4, NULL);
2228 hmac = silc_argument_get_arg_type(cmd->args, 5, NULL);
2229 auth = silc_argument_get_arg_type(cmd->args, 6, &auth_len);
2231 /* See if the channel exists */
2232 channel = silc_idlist_find_channel_by_name(server->local_list,
2233 channel_name, NULL);
2235 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2236 SilcClientEntry entry = (SilcClientEntry)cmd->sock->user_data;
2238 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2239 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2244 silc_free(client_id);
2245 client_id = silc_id_dup(entry->id, SILC_ID_CLIENT);
2248 (channel->disabled && server->server_type != SILC_ROUTER)) {
2249 /* Channel not found or not valid */
2251 /* If we are standalone server we don't have a router, we just create
2252 the channel by ourselves (unless it existed). */
2253 if (server->standalone) {
2255 channel = silc_server_create_new_channel(server, server->id, cipher,
2256 hmac, channel_name, TRUE);
2258 silc_server_command_send_status_reply(
2259 cmd, SILC_COMMAND_JOIN,
2260 SILC_STATUS_ERR_UNKNOWN_ALGORITHM,
2262 silc_free(client_id);
2266 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2272 /* The channel does not exist on our server. If we are normal server
2273 we will send JOIN command to our router which will handle the
2274 joining procedure (either creates the channel if it doesn't exist
2275 or joins the client to it). */
2276 if (server->server_type != SILC_ROUTER) {
2278 SilcUInt16 old_ident;
2280 /* If this is pending command callback then we've resolved
2281 it and it didn't work, return since we've notified the
2282 client already in the command reply callback. */
2284 silc_free(client_id);
2288 old_ident = silc_command_get_ident(cmd->payload);
2289 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2290 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2292 /* Send JOIN command to our router */
2293 silc_server_packet_send(server, (SilcSocketConnection)
2294 SILC_PRIMARY_ROUTE(server),
2295 SILC_PACKET_COMMAND, cmd->packet->flags,
2296 tmpbuf->data, tmpbuf->len, TRUE);
2298 /* Reprocess this packet after received reply from router */
2299 silc_server_command_pending(server, SILC_COMMAND_JOIN,
2300 silc_command_get_ident(cmd->payload),
2301 silc_server_command_join,
2302 silc_server_command_dup(cmd));
2303 cmd->pending = TRUE;
2304 silc_command_set_ident(cmd->payload, old_ident);
2305 silc_buffer_free(tmpbuf);
2306 silc_free(client_id);
2310 /* We are router and the channel does not seem exist so we will check
2311 our global list as well for the channel. */
2312 channel = silc_idlist_find_channel_by_name(server->global_list,
2313 channel_name, NULL);
2315 /* Channel really does not exist, create it */
2316 channel = silc_server_create_new_channel(server, server->id, cipher,
2317 hmac, channel_name, TRUE);
2319 silc_server_command_send_status_reply(
2320 cmd, SILC_COMMAND_JOIN,
2321 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2322 silc_free(client_id);
2326 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2334 /* Channel not found */
2336 /* If the command came from router and we are normal server then
2337 something went wrong with the joining as the channel was not found.
2338 We can't do anything else but ignore this. */
2339 if (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER ||
2340 server->server_type != SILC_ROUTER) {
2341 silc_free(client_id);
2345 /* We are router and the channel does not seem exist so we will check
2346 our global list as well for the channel. */
2347 channel = silc_idlist_find_channel_by_name(server->global_list,
2348 channel_name, NULL);
2350 /* Channel really does not exist, create it */
2351 channel = silc_server_create_new_channel(server, server->id, cipher,
2352 hmac, channel_name, TRUE);
2354 silc_server_command_send_status_reply(
2355 cmd, SILC_COMMAND_JOIN,
2356 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2357 silc_free(client_id);
2361 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2368 /* Check whether the channel was created by our router */
2369 if (cmd->pending && context2) {
2370 SilcServerCommandReplyContext reply = context2;
2372 if (silc_command_get(reply->payload) == SILC_COMMAND_JOIN) {
2373 tmp = silc_argument_get_arg_type(reply->args, 6, NULL);
2374 SILC_GET32_MSB(created, tmp);
2375 if (silc_argument_get_arg_type(reply->args, 7, NULL))
2376 create_key = FALSE; /* Router returned the key already */
2378 if (silc_command_get_status(reply->payload, NULL, NULL) &&
2379 channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2380 /* Save channel passphrase, if user provided it successfully */
2383 pa = silc_argument_get_arg_type(cmd->args, 3, &pa_len);
2385 silc_free(channel->passphrase);
2386 channel->passphrase = silc_memdup(pa, pa_len);
2391 if (silc_command_get(reply->payload) == SILC_COMMAND_WHOIS &&
2392 !channel->disabled && !silc_hash_table_count(channel->user_list))
2396 /* If the channel does not have global users and is also empty the client
2397 will be the channel founder and operator. */
2398 if (!channel->disabled &&
2399 !channel->global_users && !silc_hash_table_count(channel->user_list))
2400 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2402 /* Join to the channel */
2403 silc_server_command_join_channel(server, cmd, channel, client_id,
2404 created, create_key, umode,
2407 silc_free(client_id);
2410 silc_server_command_free(cmd);
2413 /* Server side of command MOTD. Sends server's current "message of the
2414 day" to the client. */
2416 SILC_SERVER_CMD_FUNC(motd)
2418 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2419 SilcServer server = cmd->server;
2420 SilcBuffer packet, idp;
2421 char *motd, *dest_server;
2422 SilcUInt32 motd_len;
2423 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2425 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_MOTD, cmd, 1, 1);
2427 /* Get server name */
2428 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2430 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
2431 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
2435 if (!strncasecmp(dest_server, server->server_name, strlen(dest_server))) {
2438 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
2440 if (server->config && server->config->server_info &&
2441 server->config->server_info->motd_file) {
2443 motd = silc_file_readfile(server->config->server_info->motd_file,
2449 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
2456 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
2462 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2463 packet->data, packet->len, FALSE);
2464 silc_buffer_free(packet);
2465 silc_buffer_free(idp);
2467 SilcServerEntry entry;
2469 /* Check whether we have this server cached */
2470 entry = silc_idlist_find_server_by_name(server->global_list,
2471 dest_server, TRUE, NULL);
2473 entry = silc_idlist_find_server_by_name(server->local_list,
2474 dest_server, TRUE, NULL);
2477 if (server->server_type != SILC_SERVER && !cmd->pending &&
2478 entry && !entry->motd) {
2479 /* Send to the server */
2481 SilcUInt16 old_ident;
2483 old_ident = silc_command_get_ident(cmd->payload);
2484 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2485 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2487 silc_server_packet_send(server, entry->connection,
2488 SILC_PACKET_COMMAND, cmd->packet->flags,
2489 tmpbuf->data, tmpbuf->len, TRUE);
2491 /* Reprocess this packet after received reply from router */
2492 silc_server_command_pending(server, SILC_COMMAND_MOTD,
2493 silc_command_get_ident(cmd->payload),
2494 silc_server_command_motd,
2495 silc_server_command_dup(cmd));
2496 cmd->pending = TRUE;
2497 silc_command_set_ident(cmd->payload, old_ident);
2498 silc_buffer_free(tmpbuf);
2502 if (!entry && !cmd->pending && !server->standalone) {
2503 /* Send to the primary router */
2505 SilcUInt16 old_ident;
2507 old_ident = silc_command_get_ident(cmd->payload);
2508 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2509 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2511 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2512 SILC_PACKET_COMMAND, cmd->packet->flags,
2513 tmpbuf->data, tmpbuf->len, TRUE);
2515 /* Reprocess this packet after received reply from router */
2516 silc_server_command_pending(server, SILC_COMMAND_MOTD,
2517 silc_command_get_ident(cmd->payload),
2518 silc_server_command_motd,
2519 silc_server_command_dup(cmd));
2520 cmd->pending = TRUE;
2521 silc_command_set_ident(cmd->payload, old_ident);
2522 silc_buffer_free(tmpbuf);
2527 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
2528 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
2532 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
2533 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
2534 SILC_STATUS_OK, 0, ident, 2,
2538 strlen(entry->motd) : 0);
2539 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2540 packet->data, packet->len, FALSE);
2541 silc_buffer_free(packet);
2542 silc_buffer_free(idp);
2546 silc_server_command_free(cmd);
2549 /* Server side of command UMODE. Client can use this command to set/unset
2550 user mode. Client actually cannot set itself to be as server/router
2551 operator so this can be used only to unset the modes. */
2553 SILC_SERVER_CMD_FUNC(umode)
2555 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2556 SilcServer server = cmd->server;
2557 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2559 unsigned char *tmp_mask, m[4];
2560 SilcUInt32 mask = 0;
2561 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2562 bool set_mask = FALSE;
2564 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
2567 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_UMODE, cmd, 1, 2);
2569 /* Get the client's mode mask */
2570 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
2572 SILC_GET32_MSB(mask, tmp_mask);
2577 /* Check that mode changing is allowed. */
2578 if (!silc_server_check_umode_rights(server, client, mask)) {
2579 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
2580 SILC_STATUS_ERR_PERM_DENIED, 0);
2584 /* Anonymous mode cannot be set by client */
2585 if (mask & SILC_UMODE_ANONYMOUS) {
2586 if (!(client->mode & SILC_UMODE_ANONYMOUS)) {
2587 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
2588 SILC_STATUS_ERR_PERM_DENIED, 0);
2592 if (client->mode & SILC_UMODE_ANONYMOUS) {
2593 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
2594 SILC_STATUS_ERR_PERM_DENIED, 0);
2599 /* Update statistics */
2600 if (mask & SILC_UMODE_GONE) {
2601 if (!(client->mode & SILC_UMODE_GONE))
2602 server->stat.my_aways++;
2604 if (client->mode & SILC_UMODE_GONE)
2605 server->stat.my_aways--;
2608 /* Change the mode */
2609 client->mode = mask;
2611 /* Send UMODE change to primary router */
2612 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
2613 SILC_BROADCAST(server), client->id,
2616 /* Check if anyone is watching this nickname */
2617 if (server->server_type == SILC_ROUTER)
2618 silc_server_check_watcher_list(server, client, NULL,
2619 SILC_NOTIFY_TYPE_UMODE_CHANGE);
2622 /* Send command reply to sender */
2623 SILC_PUT32_MSB(client->mode, m);
2624 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_UMODE,
2625 SILC_STATUS_OK, 0, ident, 1,
2627 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2628 packet->data, packet->len, FALSE);
2629 silc_buffer_free(packet);
2632 silc_server_command_free(cmd);
2635 /* Server side command of CMODE. Changes channel mode */
2637 SILC_SERVER_CMD_FUNC(cmode)
2639 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2640 SilcServer server = cmd->server;
2641 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2642 SilcIDListData idata = (SilcIDListData)client;
2643 SilcChannelID *channel_id = NULL;
2644 SilcChannelEntry channel;
2645 SilcChannelClientEntry chl;
2646 SilcBuffer packet, cidp;
2647 unsigned char *tmp, *tmp_id, *tmp_mask;
2648 char *cipher = NULL, *hmac = NULL, *passphrase = NULL;
2649 SilcUInt32 mode_mask = 0, old_mask = 0, tmp_len, tmp_len2;
2650 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2651 bool set_mask = FALSE;
2652 SilcPublicKey founder_key = NULL;
2653 SilcBuffer fkey = NULL;
2656 silc_server_command_free(cmd);
2660 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CMODE, cmd, 1, 8);
2662 /* Get Channel ID */
2663 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
2665 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2666 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
2667 silc_server_command_free(cmd);
2670 channel_id = silc_id_payload_parse_id(tmp_id, tmp_len2, NULL);
2672 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2673 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
2674 silc_server_command_free(cmd);
2678 /* Get channel entry */
2679 channel = silc_idlist_find_channel_by_id(server->local_list,
2682 channel = silc_idlist_find_channel_by_id(server->global_list,
2685 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2686 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
2688 silc_free(channel_id);
2689 silc_server_command_free(cmd);
2693 old_mask = channel->mode;
2695 /* Get the channel mode mask */
2696 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2698 SILC_GET32_MSB(mode_mask, tmp_mask);
2702 /* Check whether this client is on the channel */
2703 if (!silc_server_client_on_channel(client, channel, &chl)) {
2704 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2705 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
2709 /* Check that client has rights to change any requested channel modes */
2710 if (set_mask && !silc_server_check_cmode_rights(server, channel, chl,
2712 SILC_LOG_DEBUG(("Client does not have rights to change mode"));
2713 silc_server_command_send_status_reply(
2714 cmd, SILC_COMMAND_CMODE,
2715 (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) ?
2716 SILC_STATUS_ERR_NO_CHANNEL_PRIV :
2717 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV), 0);
2721 /* If mode mask was not sent as argument then merely return the current
2722 mode mask to the sender. */
2725 SILC_PUT32_MSB(channel->mode, m);
2726 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
2727 SILC_STATUS_OK, 0, ident, 2,
2728 2, tmp_id, tmp_len2,
2730 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2731 packet->data, packet->len, FALSE);
2732 silc_buffer_free(packet);
2737 * Check the modes. Modes that requires nothing special operation are
2741 if (mode_mask & SILC_CHANNEL_MODE_PRIVKEY) {
2742 /* Channel uses private keys to protect traffic. Client(s) has set the
2743 key locally they want to use, server does not know that key. */
2744 /* Nothing interesting to do here */
2746 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
2747 /* The mode is removed and we need to generate and distribute
2748 new channel key. Clients are not using private channel keys
2749 anymore after this. */
2751 /* Re-generate channel key */
2752 if (!silc_server_create_channel_key(server, channel, 0))
2755 /* Send the channel key. This sends it to our local clients and if
2756 we are normal server to our router as well. */
2757 silc_server_send_channel_key(server, NULL, channel,
2758 server->server_type == SILC_ROUTER ?
2759 FALSE : !server->standalone);
2761 cipher = (char *)silc_cipher_get_name(channel->channel_key);
2762 hmac = (char *)silc_hmac_get_name(channel->hmac);
2766 if (mode_mask & SILC_CHANNEL_MODE_ULIMIT) {
2767 /* User limit is set on channel */
2768 SilcUInt32 user_limit;
2770 /* Get user limit */
2771 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
2773 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT)) {
2774 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2775 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2779 SILC_GET32_MSB(user_limit, tmp);
2780 channel->user_limit = user_limit;
2783 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
2784 /* User limit mode is unset. Remove user limit */
2785 channel->user_limit = 0;
2788 if (mode_mask & SILC_CHANNEL_MODE_PASSPHRASE) {
2789 if (!(channel->mode & SILC_CHANNEL_MODE_PASSPHRASE)) {
2790 /* Passphrase has been set to channel */
2792 /* Get the passphrase */
2793 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
2795 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2796 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2800 /* Save the passphrase */
2801 passphrase = channel->passphrase = silc_memdup(tmp, strlen(tmp));
2804 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2805 /* Passphrase mode is unset. remove the passphrase */
2806 silc_free(channel->passphrase);
2807 channel->passphrase = NULL;
2811 if (mode_mask & SILC_CHANNEL_MODE_CIPHER) {
2812 if (!(channel->mode & SILC_CHANNEL_MODE_CIPHER)) {
2813 /* Cipher to use protect the traffic */
2814 SilcCipher newkey, oldkey;
2817 cipher = silc_argument_get_arg_type(cmd->args, 5, NULL);
2819 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2820 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2824 /* Delete old cipher and allocate the new one */
2825 if (!silc_cipher_alloc(cipher, &newkey)) {
2826 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2827 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2831 oldkey = channel->channel_key;
2832 channel->channel_key = newkey;
2834 /* Re-generate channel key */
2835 if (!silc_server_create_channel_key(server, channel, 0)) {
2836 /* We don't have new key, revert to old one */
2837 channel->channel_key = oldkey;
2841 /* Remove old channel key for good */
2842 silc_cipher_free(oldkey);
2844 /* Send the channel key. This sends it to our local clients and if
2845 we are normal server to our router as well. */
2846 silc_server_send_channel_key(server, NULL, channel,
2847 server->server_type == SILC_ROUTER ?
2848 FALSE : !server->standalone);
2851 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
2852 /* Cipher mode is unset. Remove the cipher and revert back to
2854 SilcCipher newkey, oldkey;
2855 cipher = channel->cipher;
2857 /* Delete old cipher and allocate default one */
2858 if (!silc_cipher_alloc(cipher ? cipher : SILC_DEFAULT_CIPHER, &newkey)) {
2859 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2860 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2864 oldkey = channel->channel_key;
2865 channel->channel_key = newkey;
2867 /* Re-generate channel key */
2868 if (!silc_server_create_channel_key(server, channel, 0)) {
2869 /* We don't have new key, revert to old one */
2870 channel->channel_key = oldkey;
2874 /* Remove old channel key for good */
2875 silc_cipher_free(oldkey);
2877 /* Send the channel key. This sends it to our local clients and if
2878 we are normal server to our router as well. */
2879 silc_server_send_channel_key(server, NULL, channel,
2880 server->server_type == SILC_ROUTER ?
2881 FALSE : !server->standalone);
2885 if (mode_mask & SILC_CHANNEL_MODE_HMAC) {
2886 if (!(channel->mode & SILC_CHANNEL_MODE_HMAC)) {
2887 /* HMAC to use protect the traffic */
2888 unsigned char hash[32];
2892 hmac = silc_argument_get_arg_type(cmd->args, 6, NULL);
2894 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2895 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2899 /* Delete old hmac and allocate the new one */
2900 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
2901 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2902 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2906 silc_hmac_free(channel->hmac);
2907 channel->hmac = newhmac;
2909 /* Set the HMAC key out of current channel key. The client must do
2911 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
2912 channel->key_len / 8, hash);
2913 silc_hmac_set_key(channel->hmac, hash,
2914 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
2915 memset(hash, 0, sizeof(hash));
2918 if (channel->mode & SILC_CHANNEL_MODE_HMAC) {
2919 /* Hmac mode is unset. Remove the hmac and revert back to
2922 unsigned char hash[32];
2923 hmac = channel->hmac_name;
2925 /* Delete old hmac and allocate default one */
2926 silc_hmac_free(channel->hmac);
2927 if (!silc_hmac_alloc(hmac ? hmac : SILC_DEFAULT_HMAC, NULL, &newhmac)) {
2928 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2929 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2933 silc_hmac_free(channel->hmac);
2934 channel->hmac = newhmac;
2936 /* Set the HMAC key out of current channel key. The client must do
2938 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
2939 channel->key_len / 8,
2941 silc_hmac_set_key(channel->hmac, hash,
2942 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
2943 memset(hash, 0, sizeof(hash));
2947 if (mode_mask & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
2948 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
2949 /* Check if the founder public key was received */
2950 founder_key = idata->public_key;
2951 tmp = silc_argument_get_arg_type(cmd->args, 8, &tmp_len);
2953 if (!silc_pkcs_public_key_payload_decode(tmp, tmp_len, &founder_key)) {
2954 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2955 SILC_STATUS_ERR_AUTH_FAILED,
2961 /* Set the founder authentication */
2962 tmp = silc_argument_get_arg_type(cmd->args, 7, &tmp_len);
2964 silc_server_command_send_status_reply(
2965 cmd, SILC_COMMAND_CMODE,
2966 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2970 /* Verify the payload before setting the mode */
2971 if (!silc_auth_verify_data(tmp, tmp_len, SILC_AUTH_PUBLIC_KEY,
2972 founder_key, 0, server->sha1hash,
2973 client->id, SILC_ID_CLIENT)) {
2974 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2975 SILC_STATUS_ERR_AUTH_FAILED,
2980 /* Save the public key */
2981 if (channel->founder_key)
2982 silc_pkcs_public_key_free(channel->founder_key);
2983 if (silc_argument_get_arg_type(cmd->args, 8, NULL))
2984 channel->founder_key = founder_key;
2986 channel->founder_key = silc_pkcs_public_key_copy(founder_key);
2987 if (!channel->founder_key) {
2988 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2989 SILC_STATUS_ERR_AUTH_FAILED,
2994 fkey = silc_pkcs_public_key_payload_encode(channel->founder_key);
2996 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2997 SILC_STATUS_ERR_AUTH_FAILED,
2999 silc_pkcs_public_key_free(channel->founder_key);
3000 channel->founder_key = NULL;
3005 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3006 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3007 if (channel->founder_key)
3008 silc_pkcs_public_key_free(channel->founder_key);
3009 channel->founder_key = NULL;
3014 /* Finally, set the mode */
3015 old_mask = channel->mode = mode_mask;
3017 /* Send CMODE_CHANGE notify. */
3018 cidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3019 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3020 SILC_NOTIFY_TYPE_CMODE_CHANGE, 6,
3021 cidp->data, cidp->len,
3023 cipher, cipher ? strlen(cipher) : 0,
3024 hmac, hmac ? strlen(hmac) : 0,
3025 passphrase, passphrase ?
3026 strlen(passphrase) : 0,
3027 fkey ? fkey->data : NULL,
3028 fkey ? fkey->len : 0);
3030 /* Set CMODE notify type to network */
3031 silc_server_send_notify_cmode(server, SILC_PRIMARY_ROUTE(server),
3032 SILC_BROADCAST(server), channel,
3033 mode_mask, client->id, SILC_ID_CLIENT,
3034 cipher, hmac, passphrase, founder_key);
3036 /* Send command reply to sender */
3037 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
3038 SILC_STATUS_OK, 0, ident, 3,
3039 2, tmp_id, tmp_len2,
3041 4, fkey ? fkey->data : NULL,
3042 fkey ? fkey->len : 0);
3043 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3044 packet->data, packet->len, FALSE);
3046 silc_buffer_free(packet);
3047 silc_buffer_free(cidp);
3050 channel->mode = old_mask;
3051 silc_buffer_free(fkey);
3052 silc_free(channel_id);
3053 silc_server_command_free(cmd);
3056 /* Server side of CUMODE command. Changes client's mode on a channel. */
3058 SILC_SERVER_CMD_FUNC(cumode)
3060 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3061 SilcServer server = cmd->server;
3062 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3063 SilcIDListData idata = (SilcIDListData)client;
3064 SilcChannelID *channel_id = NULL;
3065 SilcClientID *client_id = NULL;
3066 SilcChannelEntry channel;
3067 SilcClientEntry target_client;
3068 SilcChannelClientEntry chl;
3069 SilcBuffer packet, idp;
3070 unsigned char *tmp_id, *tmp_ch_id, *tmp_mask;
3071 SilcUInt32 target_mask, sender_mask = 0, tmp_len, tmp_ch_len;
3073 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
3074 SilcPublicKey founder_key = NULL;
3075 SilcBuffer fkey = NULL;
3080 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CUMODE, cmd, 3, 4);
3082 /* Get Channel ID */
3083 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3085 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3086 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3089 channel_id = silc_id_payload_parse_id(tmp_ch_id, tmp_ch_len, NULL);
3091 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3092 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3096 /* Get channel entry */
3097 channel = silc_idlist_find_channel_by_id(server->local_list,
3100 channel = silc_idlist_find_channel_by_id(server->global_list,
3103 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3104 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
3110 /* Check whether sender is on the channel */
3111 if (!silc_server_client_on_channel(client, channel, &chl)) {
3112 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3113 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
3116 sender_mask = chl->mode;
3118 /* Get the target client's channel mode mask */
3119 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3121 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3122 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3126 SILC_GET32_MSB(target_mask, tmp_mask);
3128 /* Get target Client ID */
3129 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3131 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3132 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3135 client_id = silc_id_payload_parse_id(tmp_id, tmp_len, NULL);
3137 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3138 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3142 /* Get target client's entry */
3143 target_client = silc_idlist_find_client_by_id(server->local_list,
3144 client_id, TRUE, NULL);
3145 if (!target_client) {
3146 target_client = silc_idlist_find_client_by_id(server->global_list,
3147 client_id, TRUE, NULL);
3150 if (target_client != client &&
3151 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO) &&
3152 !(sender_mask & SILC_CHANNEL_UMODE_CHANOP)) {
3153 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3154 SILC_STATUS_ERR_NO_CHANNEL_PRIV, 0);
3158 /* Check whether target client is on the channel */
3159 if (target_client != client) {
3160 if (!silc_server_client_on_channel(target_client, channel, &chl)) {
3161 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3162 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL, 0);
3171 /* If the target client is founder, no one else can change their mode
3173 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && client != target_client) {
3174 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3175 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV,
3180 if (target_mask & SILC_CHANNEL_UMODE_CHANFO) {
3181 if (target_client != client) {
3182 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3183 SILC_STATUS_ERR_NOT_YOU, 0);
3187 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
3188 /* The client tries to claim the founder rights. */
3189 unsigned char *tmp_auth;
3190 SilcUInt32 tmp_auth_len;
3191 SilcChannelClientEntry chl2;
3192 SilcHashTableList htl;
3194 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) ||
3195 !channel->founder_key || !idata->public_key ||
3196 !silc_pkcs_public_key_compare(channel->founder_key,
3197 idata->public_key)) {
3198 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3199 SILC_STATUS_ERR_AUTH_FAILED, 0);
3203 tmp_auth = silc_argument_get_arg_type(cmd->args, 4, &tmp_auth_len);
3205 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3206 SILC_STATUS_ERR_AUTH_FAILED, 0);
3210 /* Verify the authentication payload */
3211 if (!silc_auth_verify_data(tmp_auth, tmp_auth_len, SILC_AUTH_PUBLIC_KEY,
3212 channel->founder_key, 0, server->sha1hash,
3213 client->id, SILC_ID_CLIENT)) {
3214 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3215 SILC_STATUS_ERR_AUTH_FAILED, 0);
3220 founder_key = channel->founder_key;
3221 fkey = silc_pkcs_public_key_payload_encode(founder_key);
3223 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3224 SILC_STATUS_ERR_AUTH_FAILED, 0);
3228 /* There cannot be anyone else as founder on the channel now. This
3229 client is definitely the founder due to this authentication */
3230 silc_hash_table_list(channel->user_list, &htl);
3231 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
3232 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
3233 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
3234 silc_server_force_cumode_change(server, NULL, channel, chl2,
3238 silc_hash_table_list_reset(&htl);
3240 sender_mask = chl->mode |= SILC_CHANNEL_UMODE_CHANFO;
3243 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3244 if (target_client == client) {
3245 /* Remove channel founder rights from itself */
3246 chl->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
3249 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3250 SILC_STATUS_ERR_NOT_YOU, 0);
3256 if (target_mask & SILC_CHANNEL_UMODE_CHANOP) {
3257 /* Promote to operator */
3258 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
3259 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
3260 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
3261 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3262 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
3267 chl->mode |= SILC_CHANNEL_UMODE_CHANOP;
3271 if (chl->mode & SILC_CHANNEL_UMODE_CHANOP) {
3272 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
3273 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
3274 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3275 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
3280 /* Demote to normal user */
3281 chl->mode &= ~SILC_CHANNEL_UMODE_CHANOP;
3286 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES) {
3287 if (target_client != client) {
3288 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3289 SILC_STATUS_ERR_NOT_YOU, 0);
3293 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES)) {
3294 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES;
3298 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES) {
3299 if (target_client != client) {
3300 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3301 SILC_STATUS_ERR_NOT_YOU, 0);
3305 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES;
3310 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS) {
3311 if (target_client != client) {
3312 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3313 SILC_STATUS_ERR_NOT_YOU, 0);
3317 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS)) {
3318 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS;
3322 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS) {
3323 if (target_client != client) {
3324 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3325 SILC_STATUS_ERR_NOT_YOU, 0);
3329 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS;
3334 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS) {
3335 if (target_client != client) {
3336 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3337 SILC_STATUS_ERR_NOT_YOU, 0);
3341 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS)) {
3342 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS;
3346 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS) {
3347 if (target_client != client) {
3348 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3349 SILC_STATUS_ERR_NOT_YOU, 0);
3353 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS;
3358 if (target_mask & SILC_CHANNEL_UMODE_QUIET) {
3359 if (!(chl->mode & SILC_CHANNEL_UMODE_QUIET)) {
3360 if (client == target_client) {
3361 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3362 SILC_STATUS_ERR_PERM_DENIED, 0);
3365 chl->mode |= SILC_CHANNEL_UMODE_QUIET;
3369 if (chl->mode & SILC_CHANNEL_UMODE_QUIET) {
3370 if (client == target_client) {
3371 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3372 SILC_STATUS_ERR_PERM_DENIED, 0);
3375 chl->mode &= ~SILC_CHANNEL_UMODE_QUIET;
3380 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3381 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3383 /* Send notify to channel, notify only if mode was actually changed. */
3385 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3386 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
3387 idp->data, idp->len,
3390 fkey ? fkey->data : NULL,
3391 fkey ? fkey->len : 0);
3393 /* Set CUMODE notify type to network */
3394 silc_server_send_notify_cumode(server, SILC_PRIMARY_ROUTE(server),
3395 SILC_BROADCAST(server), channel,
3396 target_mask, client->id, SILC_ID_CLIENT,
3397 target_client->id, founder_key);
3400 /* Send command reply to sender */
3401 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CUMODE,
3402 SILC_STATUS_OK, 0, ident, 3,
3404 3, tmp_ch_id, tmp_ch_len,
3405 4, tmp_id, tmp_len);
3406 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3407 packet->data, packet->len, FALSE);
3409 silc_buffer_free(packet);
3410 silc_buffer_free(idp);
3413 silc_free(channel_id);
3414 silc_free(client_id);
3415 silc_buffer_free(fkey);
3416 silc_server_command_free(cmd);
3419 /* Server side of KICK command. Kicks client out of channel. */
3421 SILC_SERVER_CMD_FUNC(kick)
3423 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3424 SilcServer server = cmd->server;
3425 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3426 SilcClientEntry target_client;
3427 SilcChannelID *channel_id;
3428 SilcClientID *client_id;
3429 SilcChannelEntry channel;
3430 SilcChannelClientEntry chl;
3432 SilcUInt32 tmp_len, target_idp_len;
3433 unsigned char *tmp, *comment, *target_idp;
3438 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 3);
3440 /* Get Channel ID */
3441 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3443 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3444 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3447 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
3449 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3450 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3454 /* Get channel entry */
3455 channel = silc_idlist_find_channel_by_id(server->local_list,
3458 channel = silc_idlist_find_channel_by_id(server->local_list,
3461 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3462 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
3468 /* Check whether sender is on the channel */
3469 if (!silc_server_client_on_channel(client, channel, &chl)) {
3470 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3471 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
3475 /* Check that the kicker is channel operator or channel founder */
3476 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
3477 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
3478 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3479 SILC_STATUS_ERR_NO_CHANNEL_PRIV, 0);
3483 /* Get target Client ID */
3484 target_idp = silc_argument_get_arg_type(cmd->args, 2, &target_idp_len);
3486 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3487 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3490 client_id = silc_id_payload_parse_id(target_idp, target_idp_len, NULL);
3492 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3493 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3497 /* Get target client's entry */
3498 target_client = silc_idlist_find_client_by_id(server->local_list,
3499 client_id, TRUE, NULL);
3500 if (!target_client) {
3501 target_client = silc_idlist_find_client_by_id(server->global_list,
3502 client_id, TRUE, NULL);
3505 /* Check whether target client is on the channel */
3506 if (!silc_server_client_on_channel(target_client, channel, &chl)) {
3507 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3508 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL,
3513 /* Check that the target client is not channel founder. Channel founder
3514 cannot be kicked from the channel. */
3515 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3516 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3517 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV,
3524 comment = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3528 /* Send command reply to sender */
3529 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3532 /* Send KICKED notify to local clients on the channel */
3533 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3534 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3535 SILC_NOTIFY_TYPE_KICKED, 3,
3536 target_idp, target_idp_len,
3537 comment, comment ? strlen(comment) : 0,
3538 idp->data, idp->len);
3539 silc_buffer_free(idp);
3541 /* Send KICKED notify to primary route */
3542 silc_server_send_notify_kicked(server, SILC_PRIMARY_ROUTE(server),
3543 SILC_BROADCAST(server), channel,
3544 target_client->id, client->id, comment);
3546 /* Remove the client from the channel. If the channel does not exist
3547 after removing the client then the client kicked itself off the channel
3548 and we don't have to send anything after that. */
3549 if (!silc_server_remove_from_one_channel(server, NULL, channel,
3550 target_client, FALSE))
3553 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
3554 /* Re-generate channel key */
3555 if (!silc_server_create_channel_key(server, channel, 0))
3558 /* Send the channel key to the channel. The key of course is not sent
3559 to the client who was kicked off the channel. */
3560 silc_server_send_channel_key(server, target_client->connection, channel,
3561 server->server_type == SILC_ROUTER ?
3562 FALSE : !server->standalone);
3566 silc_server_command_free(cmd);
3569 /* Server side of OPER command. Client uses this comand to obtain server
3570 operator privileges to this server/router. */
3572 SILC_SERVER_CMD_FUNC(oper)
3574 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3575 SilcServer server = cmd->server;
3576 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3577 unsigned char *username, *auth;
3579 SilcServerConfigAdmin *admin;
3580 SilcIDListData idata = (SilcIDListData)client;
3581 bool result = FALSE;
3582 SilcPublicKey cached_key;
3584 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
3587 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_OPER, cmd, 1, 2);
3589 /* Get the username */
3590 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3592 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3593 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3598 /* Get the admin configuration */
3599 admin = silc_server_config_find_admin(server, cmd->sock->ip,
3600 username, client->nickname);
3602 admin = silc_server_config_find_admin(server, cmd->sock->hostname,
3603 username, client->nickname);
3605 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3606 SILC_STATUS_ERR_AUTH_FAILED,
3608 SILC_LOG_INFO(("OPER authentication failed for username '%s' by "
3609 "nickname '%s' from %s", username,
3610 client->nickname, cmd->sock->hostname));
3615 /* Get the authentication payload */
3616 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3618 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3619 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3624 /* Verify the authentication data. If both passphrase and public key
3625 is set then try both of them. */
3626 if (admin->passphrase)
3627 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PASSWORD,
3628 admin->passphrase, admin->passphrase_len,
3629 idata->hash, client->id, SILC_ID_CLIENT);
3630 if (!result && admin->publickeys) {
3631 cached_key = silc_server_get_public_key(server, admin->publickeys);
3634 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PUBLIC_KEY,
3635 cached_key, 0, idata->hash,
3636 client->id, SILC_ID_CLIENT);
3639 /* Authentication failed */
3640 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3641 SILC_STATUS_ERR_AUTH_FAILED,
3646 /* Client is now server operator */
3647 client->mode |= SILC_UMODE_SERVER_OPERATOR;
3649 /* Update statistics */
3650 if (SILC_IS_LOCAL(client))
3651 server->stat.my_server_ops++;
3652 if (server->server_type == SILC_ROUTER)
3653 server->stat.server_ops++;
3655 /* Send UMODE change to primary router */
3656 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
3657 SILC_BROADCAST(server), client->id,
3660 /* Check if anyone is watching this nickname */
3661 if (server->server_type == SILC_ROUTER)
3662 silc_server_check_watcher_list(server, client, NULL,
3663 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3665 /* Send reply to the sender */
3666 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3670 silc_server_command_free(cmd);
3673 SILC_TASK_CALLBACK(silc_server_command_detach_cb)
3675 SilcServer server = app_context;
3676 QuitInternal q = (QuitInternal)context;
3677 SilcClientID *client_id = (SilcClientID *)q->sock;
3678 SilcClientEntry client;
3679 SilcSocketConnection sock;
3681 client = silc_idlist_find_client_by_id(server->local_list, client_id,
3683 if (client && client->connection) {
3684 sock = client->connection;
3686 /* If there is pending outgoing data for the client then purge it
3687 to the network before closing connection. */
3688 silc_server_packet_queue_purge(server, sock);
3690 /* Close the connection on our side */
3691 client->router = NULL;
3692 client->connection = NULL;
3693 sock->user_data = NULL;
3694 silc_server_close_connection(server, sock);
3697 silc_free(client_id);
3701 SILC_TASK_CALLBACK(silc_server_command_detach_timeout)
3703 SilcServer server = app_context;
3704 QuitInternal q = (QuitInternal)context;
3705 SilcClientID *client_id = (SilcClientID *)q->sock;
3706 SilcClientEntry client;
3708 client = silc_idlist_find_client_by_id(server->local_list, client_id,
3710 if (client && client->mode & SILC_UMODE_DETACHED) {
3711 SILC_LOG_DEBUG(("Detach timeout"));
3712 silc_server_free_client_data(server, NULL, client, TRUE,
3716 silc_free(client_id);
3720 /* Server side of DETACH command. Detached the client from the network
3721 by closing the connection but preserving the session. */
3723 SILC_SERVER_CMD_FUNC(detach)
3725 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3726 SilcServer server = cmd->server;
3727 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3730 if (server->config->detach_disabled) {
3731 silc_server_command_send_status_reply(cmd, SILC_COMMAND_DETACH,
3732 SILC_STATUS_ERR_OPERATION_ALLOWED,
3737 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
3740 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_DETACH, cmd, 0, 0);
3742 /* Remove operator privileges, since the client may resume in some
3743 other server which to it does not have operator privileges. */
3744 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
3745 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
3747 /* Send the user mode notify to notify that client is detached */
3748 client->mode |= SILC_UMODE_DETACHED;
3749 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
3750 client->data.status &= ~SILC_IDLIST_STATUS_NOATTR;
3751 client->last_command = 0;
3752 client->fast_command = 0;
3753 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
3754 SILC_BROADCAST(server), client->id,
3756 server->stat.my_detached++;
3758 /* Check if anyone is watching this nickname */
3759 if (server->server_type == SILC_ROUTER)
3760 silc_server_check_watcher_list(server, client, NULL,
3761 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3763 q = silc_calloc(1, sizeof(*q));
3764 q->sock = silc_id_dup(client->id, SILC_ID_CLIENT);
3765 silc_schedule_task_add(server->schedule, 0, silc_server_command_detach_cb,
3766 q, 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
3768 if (server->config->detach_timeout) {
3769 q = silc_calloc(1, sizeof(*q));
3770 q->sock = silc_id_dup(client->id, SILC_ID_CLIENT);
3771 silc_schedule_task_add(server->schedule, 0,
3772 silc_server_command_detach_timeout,
3773 q, server->config->detach_timeout * 60,
3774 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
3777 /* Send reply to the sender */
3778 silc_server_command_send_status_reply(cmd, SILC_COMMAND_DETACH,
3782 silc_server_command_free(cmd);
3785 /* Server side of WATCH command. */
3787 SILC_SERVER_CMD_FUNC(watch)
3789 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3790 SilcServer server = cmd->server;
3791 char *add_nick, *del_nick;
3792 SilcUInt32 add_nick_len, del_nick_len, tmp_len;
3794 unsigned char hash[16], *tmp;
3795 SilcClientEntry client;
3796 SilcClientID *client_id = NULL;
3798 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WATCH, cmd, 1, 3);
3800 if (server->server_type != SILC_ROUTER && !server->standalone) {
3801 if (!cmd->pending) {
3802 /* Send the command to router */
3804 SilcUInt16 old_ident;
3806 SILC_LOG_DEBUG(("Forwarding WATCH to router"));
3808 old_ident = silc_command_get_ident(cmd->payload);
3809 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
3810 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3812 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3813 SILC_PACKET_COMMAND, cmd->packet->flags,
3814 tmpbuf->data, tmpbuf->len, TRUE);
3816 /* Reprocess this packet after received reply from router */
3817 silc_server_command_pending(server, SILC_COMMAND_WATCH,
3818 silc_command_get_ident(cmd->payload),
3819 silc_server_command_watch,
3820 silc_server_command_dup(cmd));
3821 cmd->pending = TRUE;
3822 silc_command_set_ident(cmd->payload, old_ident);
3823 silc_buffer_free(tmpbuf);
3824 } else if (context2) {
3825 /* Received reply from router, just send same data to the client. */
3826 SilcServerCommandReplyContext reply = context2;
3829 SILC_LOG_DEBUG(("Received reply to WATCH from router"));
3830 silc_command_get_status(reply->payload, &status, NULL);
3831 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH, status,
3838 /* We are router and keep the watch list for local cell */
3840 /* Get the client ID */
3841 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3843 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3844 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3848 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
3850 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3851 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
3856 /* Get the client entry which must be in local list */
3857 client = silc_idlist_find_client_by_id(server->local_list,
3858 client_id, TRUE, NULL);
3860 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3861 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
3867 add_nick = silc_argument_get_arg_type(cmd->args, 2, &add_nick_len);
3868 del_nick = silc_argument_get_arg_type(cmd->args, 3, &del_nick_len);
3869 if (!add_nick && !del_nick) {
3870 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3871 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3876 if (add_nick && add_nick_len > 128)
3877 add_nick[128] = '\0';
3878 if (del_nick && del_nick_len > 128)
3879 del_nick[128] = '\0';
3881 memset(nick, 0, sizeof(nick));
3883 /* Add new nickname to be watched in our cell */
3885 if (silc_server_name_bad_chars(add_nick, strlen(add_nick)) == TRUE) {
3886 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3887 SILC_STATUS_ERR_BAD_NICKNAME, 0);
3891 /* Hash the nick, we have the hash saved, not nicks because we can
3892 do one to one mapping to the nick from Client ID hash this way. */
3893 silc_to_lower(add_nick, nick, sizeof(nick) - 1);
3894 silc_hash_make(server->md5hash, nick, strlen(nick), hash);
3896 /* Check whether this client is already watching this nickname */
3897 if (silc_hash_table_find_by_context(server->watcher_list, hash,
3899 /* Nickname is alredy being watched for this client */
3900 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3901 SILC_STATUS_ERR_NICKNAME_IN_USE,
3906 /* Get the nickname from the watcher list and use the same key in
3907 new entries as well. If key doesn't exist then create it. */
3908 if (!silc_hash_table_find(server->watcher_list, hash, (void **)&tmp, NULL))
3909 tmp = silc_memdup(hash, CLIENTID_HASH_LEN);
3911 /* Add the client to the watcher list with the specified nickname hash. */
3912 silc_hash_table_add(server->watcher_list, tmp, client);
3915 /* Delete nickname from watch list */
3917 if (silc_server_name_bad_chars(del_nick, strlen(del_nick)) == TRUE) {
3918 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3919 SILC_STATUS_ERR_BAD_NICKNAME, 0);
3923 /* Hash the nick, we have the hash saved, not nicks because we can
3924 do one to one mapping to the nick from Client ID hash this way. */
3925 silc_to_lower(del_nick, nick, sizeof(nick) - 1);
3926 silc_hash_make(server->md5hash, nick, strlen(nick), hash);
3928 /* Check that this client is watching for this nickname */
3929 if (!silc_hash_table_find_by_context(server->watcher_list, hash,
3930 client, (void **)&tmp)) {
3931 /* Nickname is alredy being watched for this client */
3932 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3933 SILC_STATUS_ERR_NO_SUCH_NICK, 0);
3937 /* Delete the nickname from the watcher list. */
3938 silc_hash_table_del_by_context(server->watcher_list, hash, client);
3940 /* Now check whether there still exists entries with this key, if not
3941 then free the key to not leak memory. */
3942 if (!silc_hash_table_find(server->watcher_list, hash, NULL, NULL))
3946 /* Distribute the watch list to backup routers too */
3947 if (server->backup) {
3949 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
3950 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3951 silc_server_backup_send(server, NULL, SILC_PACKET_COMMAND,
3952 cmd->packet->flags, tmpbuf->data, tmpbuf->len,
3954 silc_buffer_free(tmpbuf);
3957 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3961 silc_free(client_id);
3962 silc_server_command_free(cmd);
3965 /* Server side of SILCOPER command. Client uses this comand to obtain router
3966 operator privileges to this router. */
3968 SILC_SERVER_CMD_FUNC(silcoper)
3970 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3971 SilcServer server = cmd->server;
3972 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3973 unsigned char *username, *auth;
3975 SilcServerConfigAdmin *admin;
3976 SilcIDListData idata = (SilcIDListData)client;
3977 bool result = FALSE;
3978 SilcPublicKey cached_key;
3980 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
3983 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_SILCOPER, cmd, 1, 2);
3985 if (server->server_type != SILC_ROUTER) {
3986 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
3987 SILC_STATUS_ERR_AUTH_FAILED, 0);
3991 /* Get the username */
3992 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3994 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
3995 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4000 /* Get the admin configuration */
4001 admin = silc_server_config_find_admin(server, cmd->sock->ip,
4002 username, client->nickname);
4004 admin = silc_server_config_find_admin(server, cmd->sock->hostname,
4005 username, client->nickname);
4007 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4008 SILC_STATUS_ERR_AUTH_FAILED, 0);
4009 SILC_LOG_INFO(("SILCOPER authentication failed for username '%s' by "
4010 "nickname '%s' from %s", username,
4011 client->nickname, cmd->sock->hostname));
4016 /* Get the authentication payload */
4017 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4019 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4020 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4025 /* Verify the authentication data. If both passphrase and public key
4026 is set then try both of them. */
4027 if (admin->passphrase)
4028 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PASSWORD,
4029 admin->passphrase, admin->passphrase_len,
4030 idata->hash, client->id, SILC_ID_CLIENT);
4031 if (!result && admin->publickeys) {
4032 cached_key = silc_server_get_public_key(server, admin->publickeys);
4035 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PUBLIC_KEY,
4036 cached_key, 0, idata->hash,
4037 client->id, SILC_ID_CLIENT);
4040 /* Authentication failed */
4041 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4042 SILC_STATUS_ERR_AUTH_FAILED, 0);
4046 /* Client is now router operator */
4047 client->mode |= SILC_UMODE_ROUTER_OPERATOR;
4049 /* Update statistics */
4050 if (SILC_IS_LOCAL(client))
4051 server->stat.my_router_ops++;
4052 if (server->server_type == SILC_ROUTER)
4053 server->stat.router_ops++;
4055 /* Send UMODE change to primary router */
4056 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
4057 SILC_BROADCAST(server), client->id,
4060 /* Check if anyone is watching this nickname */
4061 if (server->server_type == SILC_ROUTER)
4062 silc_server_check_watcher_list(server, client, NULL,
4063 SILC_NOTIFY_TYPE_UMODE_CHANGE);
4065 /* Send reply to the sender */
4066 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4070 silc_server_command_free(cmd);
4073 /* Server side of command BAN. This is used to manage the ban list of the
4074 channel. To add clients and remove clients from the ban list. */
4076 SILC_SERVER_CMD_FUNC(ban)
4078 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4079 SilcServer server = cmd->server;
4080 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4081 SilcBuffer packet, list, tmp2;
4082 SilcChannelEntry channel;
4083 SilcChannelClientEntry chl;
4084 SilcChannelID *channel_id = NULL;
4085 unsigned char *id, *tmp;
4086 SilcUInt32 id_len, len;
4087 SilcArgumentPayload args;
4088 SilcHashTableList htl;
4090 SilcUInt16 argc = 0, ident = silc_command_get_ident(cmd->payload);
4092 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
4095 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_BAN, cmd, 0, 3);
4097 /* Get Channel ID */
4098 id = silc_argument_get_arg_type(cmd->args, 1, &id_len);
4100 channel_id = silc_id_payload_parse_id(id, id_len, NULL);
4102 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4103 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4108 /* Get channel entry. The server must know about the channel since the
4109 client is expected to be on the channel. */
4110 channel = silc_idlist_find_channel_by_id(server->local_list,
4113 channel = silc_idlist_find_channel_by_id(server->global_list,
4116 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4117 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
4123 /* Check whether this client is on the channel */
4124 if (!silc_server_client_on_channel(client, channel, &chl)) {
4125 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4126 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
4130 /* The client must be at least channel operator. */
4131 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4132 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4133 SILC_STATUS_ERR_NO_CHANNEL_PRIV, 0);
4137 /* Get the ban information */
4138 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
4140 /* Parse the arguments to see they are constructed correctly */
4141 SILC_GET16_MSB(argc, tmp);
4142 args = silc_argument_payload_parse(tmp + 2, len - 2, argc);
4144 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4145 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4150 /* Get the type of action */
4151 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
4152 if (tmp && len == 1) {
4153 if (tmp[0] == 0x00) {
4154 /* Allocate hash table for ban list if it doesn't exist yet */
4155 if (!channel->ban_list)
4156 channel->ban_list = silc_hash_table_alloc(0, silc_hash_ptr,
4160 /* Check for resource limit */
4161 if (silc_hash_table_count(channel->ban_list) > 64) {
4162 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4163 SILC_STATUS_ERR_RESOURCE_LIMIT,
4169 /* Now add or delete the information. */
4170 silc_server_inviteban_process(server, channel->ban_list,
4171 (SilcUInt8)tmp[0], args);
4173 silc_argument_payload_free(args);
4176 /* Encode ban list */
4178 if (channel->ban_list) {
4179 list = silc_buffer_alloc_size(2);
4180 silc_buffer_format(list,
4181 SILC_STR_UI_SHORT(silc_hash_table_count(
4182 channel->ban_list)),
4184 silc_hash_table_list(channel->ban_list, &htl);
4185 while (silc_hash_table_get(&htl, (void **)&type, (void **)&tmp2)) {
4187 list = silc_argument_payload_encode_one(list, (char *)tmp2,
4188 strlen((char *)tmp2), type);
4190 list = silc_argument_payload_encode_one(list, tmp2->data, tmp2->len,
4193 silc_hash_table_list_reset(&htl);
4196 /* Send the BAN notify type to our primary router. */
4198 silc_server_send_notify_ban(server, SILC_PRIMARY_ROUTE(server),
4199 SILC_BROADCAST(server), channel,
4200 silc_argument_get_arg_type(cmd->args, 2, NULL),
4203 /* Send the reply back to the client */
4205 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4206 SILC_STATUS_OK, 0, ident, 2,
4208 3, list ? list->data : NULL,
4209 list ? list->len : 0);
4210 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4211 packet->data, packet->len, FALSE);
4213 silc_buffer_free(packet);
4214 silc_buffer_free(list);
4217 silc_free(channel_id);
4218 silc_server_command_free(cmd);
4221 /* Server side command of LEAVE. Removes client from a channel. */
4223 SILC_SERVER_CMD_FUNC(leave)
4225 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4226 SilcServer server = cmd->server;
4227 SilcSocketConnection sock = cmd->sock;
4228 SilcClientEntry id_entry = (SilcClientEntry)cmd->sock->user_data;
4229 SilcChannelID *id = NULL;
4230 SilcChannelEntry channel;
4234 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !id_entry)
4237 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 2);
4239 /* Get Channel ID */
4240 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
4242 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4243 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4246 id = silc_id_payload_parse_id(tmp, len, NULL);
4248 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4249 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4253 /* Get channel entry */
4254 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4256 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4258 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4259 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
4265 /* Check whether this client is on the channel */
4266 if (!silc_server_client_on_channel(id_entry, channel, NULL)) {
4267 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4268 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
4272 /* Notify routers that they should remove this client from their list
4273 of clients on the channel. Send LEAVE notify type. */
4274 silc_server_send_notify_leave(server, SILC_PRIMARY_ROUTE(server),
4275 SILC_BROADCAST(server), channel, id_entry->id);
4277 silc_server_command_send_status_data(cmd, SILC_COMMAND_LEAVE,
4278 SILC_STATUS_OK, 0, 2, tmp, len);
4280 /* Remove client from channel */
4281 if (!silc_server_remove_from_one_channel(server, sock, channel, id_entry,
4283 /* If the channel does not exist anymore we won't send anything */
4286 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4287 /* Re-generate channel key */
4288 if (!silc_server_create_channel_key(server, channel, 0))
4291 /* Send the channel key */
4292 silc_server_send_channel_key(server, NULL, channel,
4293 server->server_type == SILC_ROUTER ?
4294 FALSE : !server->standalone);
4299 silc_server_command_free(cmd);
4302 /* Server side of command USERS. Resolves clients and their USERS currently
4303 joined on the requested channel. The list of Client ID's and their modes
4304 on the channel is sent back. */
4306 SILC_SERVER_CMD_FUNC(users)
4308 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4309 SilcServer server = cmd->server;
4310 SilcChannelEntry channel;
4311 SilcChannelID *id = NULL;
4312 SilcBuffer packet, idp;
4313 unsigned char *channel_id;
4314 SilcUInt32 channel_id_len;
4315 SilcBuffer client_id_list;
4316 SilcBuffer client_mode_list;
4317 unsigned char lc[4];
4318 SilcUInt32 list_count = 0;
4319 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
4322 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_USERS, cmd, 1, 2);
4324 /* Get Channel ID */
4325 channel_id = silc_argument_get_arg_type(cmd->args, 1, &channel_id_len);
4327 /* Get channel name */
4328 channel_name = silc_argument_get_arg_type(cmd->args, 2, NULL);
4330 if (!channel_id && !channel_name) {
4331 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4332 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4337 id = silc_id_payload_parse_id(channel_id, channel_id_len, NULL);
4339 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4340 SILC_STATUS_ERR_BAD_CHANNEL_ID, 0);
4345 /* If we are server and we don't know about this channel we will send
4346 the command to our router. If we know about the channel then we also
4347 have the list of users already. */
4349 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4351 channel = silc_idlist_find_channel_by_name(server->local_list,
4352 channel_name, NULL);
4354 if (!channel || (!server->standalone && (channel->disabled ||
4355 !channel->users_resolved))) {
4356 if (server->server_type != SILC_ROUTER && !server->standalone &&
4360 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4361 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4363 /* Send USERS command */
4364 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
4365 SILC_PACKET_COMMAND, cmd->packet->flags,
4366 tmpbuf->data, tmpbuf->len, TRUE);
4368 /* Reprocess this packet after received reply */
4369 silc_server_command_pending(server, SILC_COMMAND_USERS,
4370 silc_command_get_ident(cmd->payload),
4371 silc_server_command_users,
4372 silc_server_command_dup(cmd));
4373 cmd->pending = TRUE;
4374 silc_command_set_ident(cmd->payload, ident);
4375 silc_buffer_free(tmpbuf);
4380 /* Check the global list as well. */
4382 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4384 channel = silc_idlist_find_channel_by_name(server->global_list,
4385 channel_name, NULL);
4387 /* Channel really does not exist */
4388 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4389 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
4395 /* If the channel is private or secret do not send anything, unless the
4396 user requesting this command is on the channel or is server */
4397 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
4398 if (channel->mode & (SILC_CHANNEL_MODE_PRIVATE | SILC_CHANNEL_MODE_SECRET)
4399 && !silc_server_client_on_channel(cmd->sock->user_data, channel,
4401 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4402 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
4408 /* Get the users list */
4409 if (!silc_server_get_users_on_channel(server, channel, &client_id_list,
4410 &client_mode_list, &list_count)) {
4412 client_id_list = NULL;
4413 client_mode_list = NULL;
4417 SILC_PUT32_MSB(list_count, lc);
4420 idp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
4421 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_USERS,
4422 SILC_STATUS_OK, 0, ident, 4,
4423 2, idp->data, idp->len,
4426 client_id_list->data : NULL,
4428 client_id_list->len : 0,
4429 5, client_mode_list ?
4430 client_mode_list->data : NULL,
4432 client_mode_list->len : 0);
4433 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4434 packet->data, packet->len, FALSE);
4436 silc_buffer_free(idp);
4437 silc_buffer_free(packet);
4439 silc_buffer_free(client_id_list);
4440 if (client_mode_list)
4441 silc_buffer_free(client_mode_list);
4445 silc_server_command_free(cmd);
4448 /* Server side of command GETKEY. This fetches the client's public key
4449 from the server where to the client is connected. */
4451 SILC_SERVER_CMD_FUNC(getkey)
4453 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4454 SilcServer server = cmd->server;
4456 SilcClientEntry client;
4457 SilcServerEntry server_entry;
4458 SilcClientID *client_id = NULL;
4459 SilcServerID *server_id = NULL;
4460 SilcIDPayload idp = NULL;
4461 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
4464 SilcBuffer pk = NULL;
4466 SilcPublicKey public_key;
4468 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4470 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4471 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4475 idp = silc_id_payload_parse(tmp, tmp_len);
4477 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4478 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4483 id_type = silc_id_payload_get_type(idp);
4484 if (id_type == SILC_ID_CLIENT) {
4485 client_id = silc_id_payload_get_id(idp);
4487 /* If the client is not found from local list there is no chance it
4488 would be locally connected client so send the command further. */
4489 client = silc_idlist_find_client_by_id(server->local_list,
4490 client_id, TRUE, NULL);
4492 client = silc_idlist_find_client_by_id(server->global_list,
4493 client_id, TRUE, NULL);
4495 if ((!client && !cmd->pending && !server->standalone) ||
4496 (client && !client->connection && !cmd->pending &&
4497 !(client->mode & SILC_UMODE_DETACHED)) ||
4498 (client && !client->data.public_key && !cmd->pending)) {
4500 SilcUInt16 old_ident;
4501 SilcSocketConnection dest_sock;
4503 dest_sock = silc_server_get_client_route(server, NULL, 0,
4504 client_id, NULL, NULL);
4508 old_ident = silc_command_get_ident(cmd->payload);
4509 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4510 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4512 silc_server_packet_send(server, dest_sock,
4513 SILC_PACKET_COMMAND, cmd->packet->flags,
4514 tmpbuf->data, tmpbuf->len, TRUE);
4516 /* Reprocess this packet after received reply from router */
4517 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
4518 silc_command_get_ident(cmd->payload),
4519 silc_server_command_getkey,
4520 silc_server_command_dup(cmd));
4521 cmd->pending = TRUE;
4522 silc_command_set_ident(cmd->payload, old_ident);
4523 silc_buffer_free(tmpbuf);
4528 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4529 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
4534 /* The client is locally connected, just get the public key and
4535 send it back. If they key does not exist then do not send it,
4536 send just OK reply */
4537 public_key = client->data.public_key;
4539 pk = silc_pkcs_public_key_payload_encode(public_key);
4540 } else if (id_type == SILC_ID_SERVER) {
4541 server_id = silc_id_payload_get_id(idp);
4543 /* If the server is not found from local list there is no chance it
4544 would be locally connected server so send the command further. */
4545 server_entry = silc_idlist_find_server_by_id(server->local_list,
4546 server_id, TRUE, NULL);
4548 server_entry = silc_idlist_find_server_by_id(server->global_list,
4549 server_id, TRUE, NULL);
4551 if (server_entry != server->id_entry &&
4552 ((!server_entry && !cmd->pending && !server->standalone) ||
4553 (server_entry && !server_entry->connection && !cmd->pending &&
4554 !server->standalone) ||
4555 (server_entry && !server_entry->data.public_key && !cmd->pending &&
4556 !server->standalone))) {
4558 SilcUInt16 old_ident;
4560 old_ident = silc_command_get_ident(cmd->payload);
4561 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4562 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4564 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
4565 SILC_PACKET_COMMAND, cmd->packet->flags,
4566 tmpbuf->data, tmpbuf->len, TRUE);
4568 /* Reprocess this packet after received reply from router */
4569 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
4570 silc_command_get_ident(cmd->payload),
4571 silc_server_command_getkey,
4572 silc_server_command_dup(cmd));
4573 cmd->pending = TRUE;
4574 silc_command_set_ident(cmd->payload, old_ident);
4575 silc_buffer_free(tmpbuf);
4579 if (!server_entry) {
4580 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4581 SILC_STATUS_ERR_NO_SUCH_SERVER_ID,
4586 /* If they key does not exist then do not send it, send just OK reply */
4587 public_key = (!server_entry->data.public_key ?
4588 (server_entry == server->id_entry ? server->public_key :
4589 NULL) : server_entry->data.public_key);
4591 pk = silc_pkcs_public_key_payload_encode(public_key);
4596 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4597 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_GETKEY,
4598 SILC_STATUS_OK, 0, ident, 2,
4600 3, pk ? pk->data : NULL,
4602 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4603 packet->data, packet->len, FALSE);
4604 silc_buffer_free(packet);
4608 silc_id_payload_free(idp);
4609 silc_buffer_free(pk);
4610 silc_free(client_id);
4611 silc_free(server_id);
4612 silc_server_command_free(cmd);
4616 /* Private range commands, specific to this implementation */
4618 /* Server side command of CONNECT. Connects us to the specified remote
4619 server or router. */
4621 SILC_SERVER_CMD_FUNC(connect)
4623 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4624 SilcServer server = cmd->server;
4625 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4626 unsigned char *tmp, *host;
4628 SilcUInt32 port = SILC_PORT;
4630 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
4633 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_CONNECT, cmd, 1, 2);
4635 /* Check whether client has the permissions. */
4636 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
4637 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
4638 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
4639 SILC_STATUS_ERR_NO_SERVER_PRIV, 0);
4643 if (server->server_type == SILC_ROUTER && !server->backup_router &&
4644 client->mode & SILC_UMODE_SERVER_OPERATOR) {
4645 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
4646 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
4650 /* Get the remote server */
4651 host = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4653 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
4654 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4660 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4662 SILC_GET32_MSB(port, tmp);
4664 /* Create the connection. It is done with timeout and is async. */
4665 silc_server_create_connection(server, host, port);
4667 /* Send reply to the sender */
4668 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
4672 silc_server_command_free(cmd);
4675 /* Server side command of CLOSE. Closes connection to a specified server. */
4677 SILC_SERVER_CMD_FUNC(close)
4679 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4680 SilcServer server = cmd->server;
4681 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4682 SilcServerEntry server_entry;
4683 SilcSocketConnection sock;
4686 unsigned char *name;
4687 SilcUInt32 port = SILC_PORT;
4689 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
4692 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_CLOSE, cmd, 1, 2);
4694 /* Check whether client has the permissions. */
4695 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
4696 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
4697 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
4698 SILC_STATUS_ERR_NO_SERVER_PRIV,
4703 /* Get the remote server */
4704 name = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4706 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
4707 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4713 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4715 SILC_GET32_MSB(port, tmp);
4717 server_entry = silc_idlist_find_server_by_conn(server->local_list,
4718 name, port, FALSE, NULL);
4720 server_entry = silc_idlist_find_server_by_conn(server->global_list,
4721 name, port, FALSE, NULL);
4722 if (!server_entry) {
4723 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
4724 SILC_STATUS_ERR_NO_SERVER_ID, 0);
4728 if (server_entry == server->id_entry) {
4729 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
4730 SILC_STATUS_ERR_NO_SERVER_ID, 0);
4734 /* Send reply to the sender */
4735 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
4738 /* Close the connection to the server */
4739 sock = (SilcSocketConnection)server_entry->connection;
4741 /* If we shutdown primary router connection manually then don't trigger
4742 any reconnect or backup router connections, by setting the router
4744 if (server->router == server_entry) {
4745 server->id_entry->router = NULL;
4746 server->router = NULL;
4747 server->standalone = TRUE;
4749 silc_server_free_sock_user_data(server, sock, NULL);
4750 silc_server_close_connection(server, sock);
4753 silc_server_command_free(cmd);
4756 /* Server side command of SHUTDOWN. Shutdowns the server and closes all
4757 active connections. */
4759 SILC_SERVER_CMD_FUNC(shutdown)
4761 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4762 SilcServer server = cmd->server;
4763 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4765 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
4768 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_SHUTDOWN, cmd, 0, 0);
4770 /* Check whether client has the permission. */
4771 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
4772 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
4773 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_SHUTDOWN,
4774 SILC_STATUS_ERR_NO_SERVER_PRIV,
4779 /* Send reply to the sender */
4780 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_SHUTDOWN,
4783 /* Then, gracefully, or not, bring the server down. */
4784 silc_server_stop(server);
4788 silc_server_command_free(cmd);