int main(int argc, char *argv[], char *envp[]) { int c; int show_version = 0; const char *cmdopts = OPTION_COMMANDS; char SoftBotRoot[MAX_PATH_LEN], *tmp; // set gSoftBotRoot if ( realpath(argv[0], SoftBotRoot) ) { tmp = strstr( SoftBotRoot, "/bin/softbotd" ); if ( tmp ) { *tmp = '\0'; strcpy( gSoftBotRoot, SoftBotRoot ); } } info("server has started in [%s].", gSoftBotRoot ); #ifdef AIX5 { char *env = getenv("EXTSHM"); if (!env || strcmp(env, "ON")) { /* we need EXTSHM=ON in our environment to make the shmat and mmaps work */ setenv("EXTSHM", "ON", 1); fprintf(stderr, "re-executing self with EXTSHM=ON exported to fix AIX shmat problem.\n"); execv(argv[0], argv); fprintf(stderr, "re-execution failed - terminating.\n"); exit(1); } } #endif init_setproctitle(argc, argv, envp); set_static_modules(server_static_modules); /* getopt stuff */ opterr = 0; while ( (c = #ifdef HAVE_GETOPT_LONG getopt_long(argc, argv, cmdopts, opts, NULL) #elif HAVE_GETOPT getopt(argc, argv, cmdopts) #else # error at least one of getopt, getopt_long must exist. #endif /* HAVE_GETOPT_LONG */ ) != -1 ) { int i = 0; switch ( c ) { case 'n': nodaemon++; set_screen_log(); break; case 'p': clc_listen_port++; if (strcmp(optarg,"show")==0) { /* show using ports info */ clc_listen_port++; nodaemon++; log_setlevelstr("error"); clc_log_level++; break; } i=atoi(optarg); if (i<1024) gSoftBotListenPort += i; else gSoftBotListenPort = i; info("Setting gSoftBotListenPort to %d",gSoftBotListenPort); break; case 'g': if ( !optarg ) { error("Fatal: -g requires error log level argument."); exit(1); } if (log_setlevelstr(optarg) == SUCCESS) { clc_log_level++; } else { error("Invalid log level."); info(" arg for -d is like following"); for (i=0; gLogLevelStr[i]; i++) { info(" %s",gLogLevelStr[i]); } exit(1); } break; case 'm': if ( !optarg ) { error("Fatal: -m requires module name."); exit(1); } strncpy(debug_module, optarg, MAX_MODULE_NAME); debug_module[MAX_MODULE_NAME-1] = '\0'; /* nodaemon is on by default when debugging a module */ nodaemon++; set_screen_log(); debug("debugging module[%s]", debug_module); break; case 'u': if (unittest==0) { info("When doing unittest,"); info("always check that modules to be tested are loaded"); } nodaemon++; set_screen_log(); unittest++; break; case 'k': /* refer to hook.c, hook.h, modules.c */ debug_module_hooks++; break; case 'l': list_static_modules(stdout); exit(0); break; case 'c': if ( !optarg ) { error("Fatal: -c requires configuration path"); exit(1); } strncpy(mConfigFile, optarg, MAX_PATH_LEN); mConfigFile[MAX_PATH_LEN-1] = '\0'; debug("configuration file path set to %s",mConfigFile); break; case 't': check_config_syntax = 1; clc_log_level++; gLogLevel = LEVEL_INFO; set_screen_log(); // syntax checking 시 error를 화면으로.. printf("Checking syntax of configuration file\n"); fflush(stdout); break; case 'r': if ( !optarg ) { error("Fatal: -r requires server root path"); exit(1); } strncpy(gSoftBotRoot, optarg, MAX_PATH_LEN); gSoftBotRoot[MAX_PATH_LEN-1] = '\0'; clc_server_root++; debug("gSoftBotRoot is set to %s by -r option.",gSoftBotRoot); break; case 'v': show_version++; break; case 'h': show_usage(0); case '?': error("Unknown option: %c", (char)optopt); show_usage(1); } /* switch ( c ) */ } /* end of getopt stuff */ if ( show_version ) show_version_and_exit(); /* initializing */ /* we're only doing a syntax check of the configuration file. */ if ( check_config_syntax ) { int ret=FAIL; // TODO: is this enough? -aragorn debug("checking syntax"); debug("configuration file:%s",mConfigFile); load_static_modules(); ret = read_config(mConfigFile, NULL); printf("Syntax check completed.\n"); printf("Configuration module returned %d\n",ret); exit(0); } /* setuid, setgid stuff */ /* signal handler stuff */ set_signal_handlers(); /* resource limits */ if ( nodaemon == 0 ) detach(); /* RESTART:*/ /* set pid */ gRootPid = getpid(); server_main(); /* if ( restart ) goto RESTART; */ return 0; }
int main(int argc, char **argv) { int *fdlist; int fd; int i, fdstate; size_t fdsize; unsigned long now; ssh_key **hostkeys = NULL; size_t nhostkeys = 0, hostkeysize = 0; RSAKey *hostkey1 = NULL; AuthPolicy ap; Conf *conf = conf_new(); load_open_settings(NULL, conf); ap.kbdint_state = 0; ap.ssh1keys = NULL; ap.ssh2keys = NULL; if (argc <= 1) { /* * We're going to terminate with an error message below, * because there are no host keys. But we'll display the help * as additional standard-error output, if nothing else so * that people see the giant safety warning. */ show_help(stderr); fputc('\n', stderr); } while (--argc > 0) { const char *arg = *++argv; const char *val; if (!strcmp(arg, "--help")) { show_help(stdout); exit(0); } else if (!strcmp(arg, "--version")) { show_version_and_exit(); } else if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) { verbose = true; } else if (longoptarg(arg, "--hostkey", &val, &argc, &argv)) { Filename *keyfile; int keytype; const char *error; keyfile = filename_from_str(val); keytype = key_type(keyfile); if (keytype == SSH_KEYTYPE_SSH2) { ssh2_userkey *uk; ssh_key *key; uk = ssh2_load_userkey(keyfile, NULL, &error); filename_free(keyfile); if (!uk || !uk->key) { fprintf(stderr, "%s: unable to load host key '%s': " "%s\n", appname, val, error); exit(1); } char *invalid = ssh_key_invalid(uk->key, 0); if (invalid) { fprintf(stderr, "%s: host key '%s' is unusable: " "%s\n", appname, val, invalid); exit(1); } key = uk->key; sfree(uk->comment); sfree(uk); for (i = 0; i < nhostkeys; i++) if (ssh_key_alg(hostkeys[i]) == ssh_key_alg(key)) { fprintf(stderr, "%s: host key '%s' duplicates key " "type %s\n", appname, val, ssh_key_alg(key)->ssh_id); exit(1); } sgrowarray(hostkeys, hostkeysize, nhostkeys); hostkeys[nhostkeys++] = key; } else if (keytype == SSH_KEYTYPE_SSH1) { if (hostkey1) { fprintf(stderr, "%s: host key '%s' is a redundant " "SSH-1 host key\n", appname, val); exit(1); } hostkey1 = snew(RSAKey); if (!rsa_ssh1_loadkey(keyfile, hostkey1, NULL, &error)) { fprintf(stderr, "%s: unable to load host key '%s': " "%s\n", appname, val, error); exit(1); } } else { fprintf(stderr, "%s: '%s' is not loadable as a " "private key (%s)", appname, val, key_type_to_str(keytype)); exit(1); } } else if (longoptarg(arg, "--userkey", &val, &argc, &argv)) { Filename *keyfile; int keytype; const char *error; keyfile = filename_from_str(val); keytype = key_type(keyfile); if (keytype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 || keytype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH) { strbuf *sb = strbuf_new(); struct AuthPolicy_ssh2_pubkey *node; void *blob; if (!ssh2_userkey_loadpub(keyfile, NULL, BinarySink_UPCAST(sb), NULL, &error)) { fprintf(stderr, "%s: unable to load user key '%s': " "%s\n", appname, val, error); exit(1); } node = snew_plus(struct AuthPolicy_ssh2_pubkey, sb->len); blob = snew_plus_get_aux(node); memcpy(blob, sb->u, sb->len); node->public_blob = make_ptrlen(blob, sb->len); node->next = ap.ssh2keys; ap.ssh2keys = node; strbuf_free(sb); } else if (keytype == SSH_KEYTYPE_SSH1_PUBLIC) {