From: Pekka Riikonen Date: Wed, 14 Nov 2001 14:44:27 +0000 (+0000) Subject: updates. X-Git-Tag: silcertest~4 X-Git-Url: http://git.silcnet.org/gitweb/?p=silc.git;a=commitdiff_plain;h=d2707de39e6f40cbf6a72790fe95a694eba7aee9 updates. --- diff --git a/CHANGES b/CHANGES index 3ad55368..88ca9bec 100644 --- a/CHANGES +++ b/CHANGES @@ -7,6 +7,12 @@ Wed Nov 14 16:22:25 EET 2001 Pekka Riikonen to run server on foreground. A patch by debolaz. Affected files silcd/server.c, silcd/silcd.c. + * Fixed MOTD to return the MOTD file server name. Affected + file silcd/command.c. + + * Added INFO command reply handling to the Irssi SILC Client. + Affected file irssi/src/silc/core/client_ops.c. + Wed Nov 14 00:18:08 EET 2001 Pekka Riikonen * Fixed the silc_idcache_list_* routines to really support diff --git a/TODO b/TODO index 168e2652..c631c660 100644 --- a/TODO +++ b/TODO @@ -41,8 +41,6 @@ TODO/bugs In SILC Client Library TODO/bugs In SILC Server ======================== - o MOTD and INFO does not work with server name. - o After backup resume protocol the TOPIC_SET was not handled correctly by all (unknown Channel ID). diff --git a/apps/irssi/src/silc/core/client_ops.c b/apps/irssi/src/silc/core/client_ops.c index 4104b4bd..772fb243 100644 --- a/apps/irssi/src/silc/core/client_ops.c +++ b/apps/irssi/src/silc/core/client_ops.c @@ -791,6 +791,27 @@ silc_command_reply(SilcClient client, SilcClientConnection conn, } } break; + + case SILC_COMMAND_INFO: + { + SilcServerEntry server_entry; + char *server_name; + char *server_info; + + if (!success) + return; + + server_entry = va_arg(vp, SilcServerEntry); + server_name = va_arg(vp, char *); + server_info = va_arg(vp, char *); + + if (server_name && server_info ) + { + printtext(server, NULL, MSGLEVEL_CRAP, "Server: %s", server_name); + printtext(server, NULL, MSGLEVEL_CRAP, "%s", server_info); + } + } + break; case SILC_COMMAND_TOPIC: { diff --git a/apps/silcd/command.c b/apps/silcd/command.c index f3eab682..64fa02a2 100644 --- a/apps/silcd/command.c +++ b/apps/silcd/command.c @@ -3474,7 +3474,6 @@ SILC_SERVER_CMD_FUNC(motd) SILC_STATUS_OK, ident, 2, 2, idp, idp->len, 3, motd, motd_len); - goto out; } else { /* No motd */ packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD, diff --git a/apps/silcd/server.c b/apps/silcd/server.c index 2d76a24a..6a76301f 100644 --- a/apps/silcd/server.c +++ b/apps/silcd/server.c @@ -356,19 +356,42 @@ int silc_server_init(SilcServer server) return FALSE; } -/* Fork server to background and set gid+uid to non-root. - Silcd will not run as root, so trying to set either user or group to - root will cause silcd to exit. */ +/* Fork server to background and set gid+uid to non-root */ void silc_server_daemonise(SilcServer server) +{ + int i; + + i = fork (); + + if (i) { + if (i > 0) { + if (geteuid()) + SILC_LOG_DEBUG(("Server started as user")); + else + SILC_LOG_DEBUG(("Server started as root. Dropping privileges.")); + + SILC_LOG_DEBUG(("Forking SILC server to background")); + exit(0); + } else { + SILC_LOG_DEBUG(("fork() failed, cannot proceed")); + exit(1); + } + } + setsid(); +} + +/* Drop root privligies. If this cannot be done, die. */ + +void silc_server_drop(SilcServer server) { /* Are we executing silcd as root or a regular user? */ - if (geteuid()==0) { - + if (!geteuid()) { + struct passwd *pw; struct group *gr; char *user, *group; - + if (!server->config->identity || !server->config->identity->user || !server->config->identity->group) { fprintf(stderr, "Error:" @@ -378,11 +401,11 @@ void silc_server_daemonise(SilcServer server) "\tthe server as non-root user.\n"); exit(1); } - + /* Get the values given for user and group in configuration file */ user=server->config->identity->user; group=server->config->identity->group; - + /* Check whether the user/group information is text */ if (atoi(user)!=0 || atoi(group)!=0) { SILC_LOG_DEBUG(("Invalid user and/or group information")); @@ -391,14 +414,14 @@ void silc_server_daemonise(SilcServer server) fprintf(stderr, "Please assign them as names, not numbers\n"); exit(1); } - + /* Catch the nasty incident of string "0" returning 0 from atoi */ if (strcmp("0", user)==0 || strcmp("0", group)==0) { SILC_LOG_DEBUG(("User and/or group configured to 0. Unacceptable")); fprintf(stderr, "User and/or group configured to 0. Exiting\n"); exit(1); } - + pw=getpwnam(user); gr=getgrnam(group); @@ -411,7 +434,7 @@ void silc_server_daemonise(SilcServer server) fprintf(stderr, "No such group %s found\n", group); exit(1); } - + /* Check whether user and/or group is set to root. If yes, exit immediately. Otherwise, setgid and setuid server to user.group */ if (gr->gr_gid==0 || pw->pw_uid==0) { @@ -422,14 +445,6 @@ void silc_server_daemonise(SilcServer server) "\tthe server as non-root user.\n"); exit(1); } else { - /* Fork server to background, making it a daemon */ - if (fork()) { - SILC_LOG_DEBUG(("Server started as root. Dropping privileges.")); - SILC_LOG_DEBUG(("Forking SILC server to background")); - exit(0); - } - setsid(); - SILC_LOG_DEBUG(("Changing to group %s", group)); if(setgid(gr->gr_gid)==0) { SILC_LOG_DEBUG(("Setgid to %s", group)); @@ -449,14 +464,6 @@ void silc_server_daemonise(SilcServer server) exit(1); } } - } else { - /* Fork server to background, making it a daemon */ - if (fork()) { - SILC_LOG_DEBUG(("Server started as user")); - SILC_LOG_DEBUG(("Forking SILC server to background")); - exit(0); - } - setsid(); } } diff --git a/apps/silcd/server.h b/apps/silcd/server.h index 1020a81a..8ea3293b 100644 --- a/apps/silcd/server.h +++ b/apps/silcd/server.h @@ -121,6 +121,7 @@ int silc_server_alloc(SilcServer *new_server); void silc_server_free(SilcServer server); int silc_server_init(SilcServer server); void silc_server_daemonise(SilcServer server); +void silc_server_drop(SilcServer server); void silc_server_run(SilcServer server); void silc_server_stop(SilcServer server); void silc_server_start_key_exchange(SilcServer server, diff --git a/apps/silcd/serverconfig.c b/apps/silcd/serverconfig.c index 5aaec489..d91db49b 100644 --- a/apps/silcd/serverconfig.c +++ b/apps/silcd/serverconfig.c @@ -57,6 +57,8 @@ SilcServerConfigSection silc_server_config_sections[] = { SILC_CONFIG_SERVER_SECTION_TYPE_DENY_CONNECTION, 3 }, { "[motd]", SILC_CONFIG_SERVER_SECTION_TYPE_MOTD, 1 }, + { "[pid]", + SILC_CONFIG_SERVER_SECTION_TYPE_PID, 1}, { NULL, SILC_CONFIG_SERVER_SECTION_TYPE_NONE, 0 } }; @@ -121,6 +123,7 @@ void silc_server_config_free(SilcServerConfig config) silc_free(config->routers); silc_free(config->denied); silc_free(config->motd); + silc_free(config->pidfile); silc_free(config); } } @@ -1107,6 +1110,19 @@ int silc_server_config_parse_lines(SilcServerConfig config, checkmask |= (1L << pc->section->type); break; + case SILC_CONFIG_SERVER_SECTION_TYPE_PID: + + if (!config->pidfile) + config->pidfile = silc_calloc(1, sizeof(*config->pidfile)); + + ret = silc_config_get_token(line, &config->pidfile->pid_file); + if (ret < 0) + break; + + check = TRUE; + checkmask |= (1L << pc->section->type); + break; + case SILC_CONFIG_SERVER_SECTION_TYPE_NONE: default: /* Error */ diff --git a/apps/silcd/serverconfig.h b/apps/silcd/serverconfig.h index 1df1e95a..ef284fb0 100644 --- a/apps/silcd/serverconfig.h +++ b/apps/silcd/serverconfig.h @@ -154,6 +154,11 @@ typedef struct { char *motd_file; } SilcServerConfigSectionMotd; +/* holds pid file */ +typedef struct { + char *pid_file; +} SilcServerConfigSectionPid; + /* SILC Server Config object. @@ -186,6 +191,7 @@ typedef struct { SilcServerConfigSectionAdminConnection *admins; SilcServerConfigSectionDenyConnection *denied; SilcServerConfigSectionMotd *motd; + SilcServerConfigSectionPid *pidfile; } SilcServerConfigObject; typedef SilcServerConfigObject *SilcServerConfig; @@ -210,6 +216,7 @@ typedef enum { SILC_CONFIG_SERVER_SECTION_TYPE_ADMIN_CONNECTION, SILC_CONFIG_SERVER_SECTION_TYPE_DENY_CONNECTION, SILC_CONFIG_SERVER_SECTION_TYPE_MOTD, + SILC_CONFIG_SERVER_SECTION_TYPE_PID, } SilcServerConfigSectionType; /* SILC Configuration Section structure. */ diff --git a/apps/silcd/silcd.c b/apps/silcd/silcd.c index bfb50ca7..085a2522 100644 --- a/apps/silcd/silcd.c +++ b/apps/silcd/silcd.c @@ -43,6 +43,7 @@ static struct option long_opts[] = { "config-file", 1, NULL, 'f' }, { "debug", 1, NULL, 'd' }, { "help", 0, NULL, 'h' }, + { "foreground", 0, NULL, 'F' }, { "version", 0, NULL,'V' }, /* Key management options */ @@ -70,8 +71,9 @@ Usage: silcd [options]\n\ \n\ Generic Options:\n\ -f --config-file=FILE Alternate configuration file\n\ - -d --debug=string Enable debugging (no daemon)\n\ + -d --debug=string Enable debugging (Implies --foreground)\n\ -h --help Display this message\n\ + -F --foreground Dont fork\n\ -V --version Display version\n\ \n\ Key Management Options:\n\ @@ -99,6 +101,7 @@ int main(int argc, char **argv) { int ret; int opt, option_index; + int foreground = FALSE; char *config_file = NULL; SilcServer silcd; struct sigaction sa; @@ -108,7 +111,7 @@ int main(int argc, char **argv) /* Parse command line arguments */ if (argc > 1) { - while ((opt = getopt_long(argc, argv, "cf:d:hVC:", + while ((opt = getopt_long(argc, argv, "cf:d:hFVC:", long_opts, &option_index)) != EOF) { switch(opt) { @@ -136,6 +139,9 @@ int main(int argc, char **argv) case 'f': config_file = strdup(optarg); break; + case 'F': + foreground = TRUE; + break; /* * Key management options @@ -201,17 +207,23 @@ int main(int argc, char **argv) sigemptyset(&sa.sa_mask); sigaction(SIGPIPE, &sa, NULL); - if (silc_debug == FALSE) - /* Before running the server, fork to background and set - both user and group no non-root */ + if ((silc_debug == FALSE) && (foreground == FALSE)) + /* Before running the server, fork to background. */ silc_server_daemonise(silcd); /* Set /var/run/silcd.pid */ unlink(SILC_SERVER_PID_FILE); memset(pid, 0, sizeof(pid)); snprintf(pid, sizeof(pid) - 1, "%d\n", getpid()); - silc_file_writefile(SILC_SERVER_PID_FILE, pid, strlen(pid)); + if (silcd->config->pidfile && silcd->config->pidfile->pid_file) { + silc_file_writefile(silcd->config->pidfile->pid_file, pid, strlen(pid)); + } else { + silc_file_writefile(SILC_SERVER_PID_FILE, pid, strlen(pid)); + } + /* Drop root. */ + silc_server_drop(silcd); + /* Run the server. When this returns the server has been stopped and we will exit. */ silc_server_run(silcd); diff --git a/lib/silcclient/command_reply.c b/lib/silcclient/command_reply.c index 29cfc2bb..f775d60c 100644 --- a/lib/silcclient/command_reply.c +++ b/lib/silcclient/command_reply.c @@ -838,6 +838,9 @@ SILC_CLIENT_CMD_REPLY_FUNC(info) /* Add it to the cache */ silc_idcache_add(conn->server_cache, server->server_name, server->server_id, (void *)server, FALSE); + + if (SILC_ID_SERVER_COMPARE(server_id, conn->remote_id)) + goto out; } else { server = (SilcServerEntry)id_cache->context; } diff --git a/lib/silccrypt/silcrng.c b/lib/silccrypt/silcrng.c index 3c2dea53..f68c1b80 100644 --- a/lib/silccrypt/silcrng.c +++ b/lib/silccrypt/silcrng.c @@ -29,11 +29,11 @@ #include "silcincludes.h" #ifdef HAVE_GETSID -extern __pid_t getsid (__pid_t __pid); +extern pid_t getsid (pid_t __pid); #endif #ifdef HAVE_GETPGID -extern __pid_t getpgid (__pid_t __pid); +extern pid_t getpgid (pid_t __pid); #endif #undef SILC_RNG_DEBUG