svr-runopts.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. * Dropbear - a SSH2 server
  3. *
  4. * Copyright (c) 2002,2003 Matt Johnston
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE. */
  24. #include "includes.h"
  25. #include "runopts.h"
  26. #include "signkey.h"
  27. #include "buffer.h"
  28. #include "dbutil.h"
  29. #include "algo.h"
  30. #include "ecdsa.h"
  31. #include <grp.h>
  32. svr_runopts svr_opts; /* GLOBAL */
  33. static void printhelp(const char * progname);
  34. static void addportandaddress(const char* spec);
  35. static void loadhostkey(const char *keyfile, int fatal_duplicate);
  36. static void addhostkey(const char *keyfile);
  37. static void printhelp(const char * progname) {
  38. fprintf(stderr, "Dropbear server v%s https://matt.ucc.asn.au/dropbear/dropbear.html\n"
  39. "Usage: %s [options]\n"
  40. "-b bannerfile Display the contents of bannerfile"
  41. " before user login\n"
  42. " (default: none)\n"
  43. "-r keyfile Specify hostkeys (repeatable)\n"
  44. " defaults: \n"
  45. #if DROPBEAR_DSS
  46. " - dss %s\n"
  47. #endif
  48. #if DROPBEAR_RSA
  49. " - rsa %s\n"
  50. #endif
  51. #if DROPBEAR_ECDSA
  52. " - ecdsa %s\n"
  53. #endif
  54. #if DROPBEAR_ED25519
  55. " - ed25519 %s\n"
  56. #endif
  57. #if DROPBEAR_DELAY_HOSTKEY
  58. "-R Create hostkeys as required\n"
  59. #endif
  60. "-F Don't fork into background\n"
  61. "-e Pass on server process environment to child process\n"
  62. #ifdef DISABLE_SYSLOG
  63. "(Syslog support not compiled in, using stderr)\n"
  64. #else
  65. "-E Log to stderr rather than syslog\n"
  66. #endif
  67. #if DO_MOTD
  68. "-m Don't display the motd on login\n"
  69. #endif
  70. "-w Disallow root logins\n"
  71. #ifdef HAVE_GETGROUPLIST
  72. "-G Restrict logins to members of specified group\n"
  73. #endif
  74. #if DROPBEAR_SVR_PASSWORD_AUTH || DROPBEAR_SVR_PAM_AUTH
  75. "-s Disable password logins\n"
  76. "-g Disable password logins for root\n"
  77. "-B Allow blank password logins\n"
  78. #endif
  79. "-T Maximum authentication tries (default %d)\n"
  80. #if DROPBEAR_SVR_LOCALTCPFWD
  81. "-j Disable local port forwarding\n"
  82. #endif
  83. #if DROPBEAR_SVR_REMOTETCPFWD
  84. "-k Disable remote port forwarding\n"
  85. "-a Allow connections to forwarded ports from any host\n"
  86. "-c command Force executed command\n"
  87. #endif
  88. "-p [address:]port\n"
  89. " Listen on specified tcp port (and optionally address),\n"
  90. " up to %d can be specified\n"
  91. " (default port is %s if none specified)\n"
  92. "-P PidFile Create pid file PidFile\n"
  93. " (default %s)\n"
  94. #if INETD_MODE
  95. "-i Start for inetd\n"
  96. #endif
  97. "-W <receive_window_buffer> (default %d, larger may be faster, max 10MB)\n"
  98. "-K <keepalive> (0 is never, default %d, in seconds)\n"
  99. "-I <idle_timeout> (0 is never, default %d, in seconds)\n"
  100. #if DROPBEAR_PLUGIN
  101. "-A <authplugin>[,<options>]\n"
  102. " Enable external public key auth through <authplugin>\n"
  103. #endif
  104. "-V Version\n"
  105. #if DEBUG_TRACE
  106. "-v verbose (repeat for more verbose)\n"
  107. #endif
  108. ,DROPBEAR_VERSION, progname,
  109. #if DROPBEAR_DSS
  110. DSS_PRIV_FILENAME,
  111. #endif
  112. #if DROPBEAR_RSA
  113. RSA_PRIV_FILENAME,
  114. #endif
  115. #if DROPBEAR_ECDSA
  116. ECDSA_PRIV_FILENAME,
  117. #endif
  118. #if DROPBEAR_ED25519
  119. ED25519_PRIV_FILENAME,
  120. #endif
  121. MAX_AUTH_TRIES,
  122. DROPBEAR_MAX_PORTS, DROPBEAR_DEFPORT, DROPBEAR_PIDFILE,
  123. DEFAULT_RECV_WINDOW, DEFAULT_KEEPALIVE, DEFAULT_IDLE_TIMEOUT);
  124. }
  125. void svr_getopts(int argc, char ** argv) {
  126. unsigned int i, j;
  127. char ** next = NULL;
  128. int nextisport = 0;
  129. char* recv_window_arg = NULL;
  130. char* keepalive_arg = NULL;
  131. char* idle_timeout_arg = NULL;
  132. char* maxauthtries_arg = NULL;
  133. char* keyfile = NULL;
  134. char c;
  135. #if DROPBEAR_PLUGIN
  136. char* pubkey_plugin = NULL;
  137. #endif
  138. /* see printhelp() for options */
  139. svr_opts.bannerfile = NULL;
  140. svr_opts.banner = NULL;
  141. svr_opts.forced_command = NULL;
  142. svr_opts.forkbg = 1;
  143. svr_opts.norootlogin = 0;
  144. #ifdef HAVE_GETGROUPLIST
  145. svr_opts.restrict_group = NULL;
  146. svr_opts.restrict_group_gid = 0;
  147. #endif
  148. svr_opts.noauthpass = 0;
  149. svr_opts.norootpass = 0;
  150. svr_opts.allowblankpass = 0;
  151. svr_opts.maxauthtries = MAX_AUTH_TRIES;
  152. svr_opts.inetdmode = 0;
  153. svr_opts.portcount = 0;
  154. svr_opts.hostkey = NULL;
  155. svr_opts.delay_hostkey = 0;
  156. svr_opts.pidfile = expand_homedir_path(DROPBEAR_PIDFILE);
  157. #if DROPBEAR_SVR_LOCALTCPFWD
  158. svr_opts.nolocaltcp = 0;
  159. #endif
  160. #if DROPBEAR_SVR_REMOTETCPFWD
  161. svr_opts.noremotetcp = 0;
  162. #endif
  163. #if DROPBEAR_PLUGIN
  164. svr_opts.pubkey_plugin = NULL;
  165. svr_opts.pubkey_plugin_options = NULL;
  166. #endif
  167. svr_opts.pass_on_env = 0;
  168. #ifndef DISABLE_ZLIB
  169. opts.compress_mode = DROPBEAR_COMPRESS_DELAYED;
  170. #endif
  171. /* not yet
  172. opts.ipv4 = 1;
  173. opts.ipv6 = 1;
  174. */
  175. #if DO_MOTD
  176. svr_opts.domotd = 1;
  177. #endif
  178. #ifndef DISABLE_SYSLOG
  179. opts.usingsyslog = 1;
  180. #endif
  181. opts.recv_window = DEFAULT_RECV_WINDOW;
  182. opts.keepalive_secs = DEFAULT_KEEPALIVE;
  183. opts.idle_timeout_secs = DEFAULT_IDLE_TIMEOUT;
  184. #if DROPBEAR_SVR_REMOTETCPFWD
  185. opts.listen_fwd_all = 0;
  186. #endif
  187. for (i = 1; i < (unsigned int)argc; i++) {
  188. if (argv[i][0] != '-' || argv[i][1] == '\0')
  189. dropbear_exit("Invalid argument: %s", argv[i]);
  190. for (j = 1; (c = argv[i][j]) != '\0' && !next && !nextisport; j++) {
  191. switch (c) {
  192. case 'b':
  193. next = &svr_opts.bannerfile;
  194. break;
  195. case 'c':
  196. next = &svr_opts.forced_command;
  197. break;
  198. case 'd':
  199. case 'r':
  200. next = &keyfile;
  201. break;
  202. case 'R':
  203. svr_opts.delay_hostkey = 1;
  204. break;
  205. case 'F':
  206. svr_opts.forkbg = 0;
  207. break;
  208. #ifndef DISABLE_SYSLOG
  209. case 'E':
  210. opts.usingsyslog = 0;
  211. break;
  212. #endif
  213. case 'e':
  214. svr_opts.pass_on_env = 1;
  215. break;
  216. #if DROPBEAR_SVR_LOCALTCPFWD
  217. case 'j':
  218. svr_opts.nolocaltcp = 1;
  219. break;
  220. #endif
  221. #if DROPBEAR_SVR_REMOTETCPFWD
  222. case 'k':
  223. svr_opts.noremotetcp = 1;
  224. break;
  225. case 'a':
  226. opts.listen_fwd_all = 1;
  227. break;
  228. #endif
  229. #if INETD_MODE
  230. case 'i':
  231. svr_opts.inetdmode = 1;
  232. break;
  233. #endif
  234. #if DROPBEAR_DO_REEXEC && NON_INETD_MODE
  235. /* For internal use by re-exec */
  236. case '2':
  237. svr_opts.reexec_child = 1;
  238. break;
  239. #endif
  240. case 'p':
  241. nextisport = 1;
  242. break;
  243. case 'P':
  244. next = &svr_opts.pidfile;
  245. break;
  246. #if DO_MOTD
  247. /* motd is displayed by default, -m turns it off */
  248. case 'm':
  249. svr_opts.domotd = 0;
  250. break;
  251. #endif
  252. case 'w':
  253. svr_opts.norootlogin = 1;
  254. break;
  255. #ifdef HAVE_GETGROUPLIST
  256. case 'G':
  257. next = &svr_opts.restrict_group;
  258. break;
  259. #endif
  260. case 'W':
  261. next = &recv_window_arg;
  262. break;
  263. case 'K':
  264. next = &keepalive_arg;
  265. break;
  266. case 'I':
  267. next = &idle_timeout_arg;
  268. break;
  269. case 'T':
  270. next = &maxauthtries_arg;
  271. break;
  272. #if DROPBEAR_SVR_PASSWORD_AUTH || DROPBEAR_SVR_PAM_AUTH
  273. case 's':
  274. svr_opts.noauthpass = 1;
  275. break;
  276. case 'g':
  277. svr_opts.norootpass = 1;
  278. break;
  279. case 'B':
  280. svr_opts.allowblankpass = 1;
  281. break;
  282. #endif
  283. case 'h':
  284. printhelp(argv[0]);
  285. exit(EXIT_SUCCESS);
  286. break;
  287. case 'u':
  288. /* backwards compatibility with old urandom option */
  289. break;
  290. #if DROPBEAR_PLUGIN
  291. case 'A':
  292. next = &pubkey_plugin;
  293. break;
  294. #endif
  295. #if DEBUG_TRACE
  296. case 'v':
  297. debug_trace++;
  298. break;
  299. #endif
  300. case 'V':
  301. print_version();
  302. exit(EXIT_SUCCESS);
  303. break;
  304. default:
  305. fprintf(stderr, "Invalid option -%c\n", c);
  306. printhelp(argv[0]);
  307. exit(EXIT_FAILURE);
  308. break;
  309. }
  310. }
  311. if (!next && !nextisport)
  312. continue;
  313. if (c == '\0') {
  314. i++;
  315. j = 0;
  316. if (!argv[i]) {
  317. dropbear_exit("Missing argument");
  318. }
  319. }
  320. if (nextisport) {
  321. addportandaddress(&argv[i][j]);
  322. nextisport = 0;
  323. } else if (next) {
  324. *next = &argv[i][j];
  325. if (*next == NULL) {
  326. dropbear_exit("Invalid null argument");
  327. }
  328. next = NULL;
  329. if (keyfile) {
  330. addhostkey(keyfile);
  331. keyfile = NULL;
  332. }
  333. }
  334. }
  335. /* Set up listening ports */
  336. if (svr_opts.portcount == 0) {
  337. svr_opts.ports[0] = m_strdup(DROPBEAR_DEFPORT);
  338. svr_opts.addresses[0] = m_strdup(DROPBEAR_DEFADDRESS);
  339. svr_opts.portcount = 1;
  340. }
  341. if (svr_opts.bannerfile) {
  342. struct stat buf;
  343. if (stat(svr_opts.bannerfile, &buf) != 0) {
  344. dropbear_exit("Error opening banner file '%s'",
  345. svr_opts.bannerfile);
  346. }
  347. if (buf.st_size > MAX_BANNER_SIZE) {
  348. dropbear_exit("Banner file too large, max is %d bytes",
  349. MAX_BANNER_SIZE);
  350. }
  351. svr_opts.banner = buf_new(buf.st_size);
  352. if (buf_readfile(svr_opts.banner, svr_opts.bannerfile)!=DROPBEAR_SUCCESS) {
  353. dropbear_exit("Error reading banner file '%s'",
  354. svr_opts.bannerfile);
  355. }
  356. buf_setpos(svr_opts.banner, 0);
  357. }
  358. #ifdef HAVE_GETGROUPLIST
  359. if (svr_opts.restrict_group) {
  360. struct group *restrictedgroup = getgrnam(svr_opts.restrict_group);
  361. if (restrictedgroup){
  362. svr_opts.restrict_group_gid = restrictedgroup->gr_gid;
  363. } else {
  364. dropbear_exit("Cannot restrict logins to group '%s' as the group does not exist", svr_opts.restrict_group);
  365. }
  366. }
  367. #endif
  368. if (recv_window_arg) {
  369. parse_recv_window(recv_window_arg);
  370. }
  371. if (maxauthtries_arg) {
  372. unsigned int val = 0;
  373. if (m_str_to_uint(maxauthtries_arg, &val) == DROPBEAR_FAILURE
  374. || val == 0) {
  375. dropbear_exit("Bad maxauthtries '%s'", maxauthtries_arg);
  376. }
  377. svr_opts.maxauthtries = val;
  378. }
  379. if (keepalive_arg) {
  380. unsigned int val;
  381. if (m_str_to_uint(keepalive_arg, &val) == DROPBEAR_FAILURE) {
  382. dropbear_exit("Bad keepalive '%s'", keepalive_arg);
  383. }
  384. opts.keepalive_secs = val;
  385. }
  386. if (idle_timeout_arg) {
  387. unsigned int val;
  388. if (m_str_to_uint(idle_timeout_arg, &val) == DROPBEAR_FAILURE) {
  389. dropbear_exit("Bad idle_timeout '%s'", idle_timeout_arg);
  390. }
  391. opts.idle_timeout_secs = val;
  392. }
  393. if (svr_opts.forced_command) {
  394. dropbear_log(LOG_INFO, "Forced command set to '%s'", svr_opts.forced_command);
  395. }
  396. #if INETD_MODE
  397. if (svr_opts.inetdmode && (
  398. opts.usingsyslog == 0
  399. #if DEBUG_TRACE
  400. || debug_trace
  401. #endif
  402. )) {
  403. /* log output goes to stderr which would get sent over the inetd network socket */
  404. dropbear_exit("Dropbear inetd mode is incompatible with debug -v or non-syslog");
  405. }
  406. #endif
  407. #if DROPBEAR_PLUGIN
  408. if (pubkey_plugin) {
  409. char *args = strchr(pubkey_plugin, ',');
  410. if (args) {
  411. *args='\0';
  412. ++args;
  413. }
  414. svr_opts.pubkey_plugin = pubkey_plugin;
  415. svr_opts.pubkey_plugin_options = args;
  416. }
  417. #endif
  418. }
  419. static void addportandaddress(const char* spec) {
  420. char *port = NULL, *address = NULL;
  421. if (svr_opts.portcount >= DROPBEAR_MAX_PORTS) {
  422. return;
  423. }
  424. if (split_address_port(spec, &address, &port) == DROPBEAR_FAILURE) {
  425. dropbear_exit("Bad -p argument");
  426. }
  427. /* A bare port */
  428. if (!port) {
  429. port = address;
  430. address = NULL;
  431. }
  432. if (!address) {
  433. /* no address given -> fill in the default address */
  434. address = m_strdup(DROPBEAR_DEFADDRESS);
  435. }
  436. if (port[0] == '\0') {
  437. /* empty port -> exit */
  438. dropbear_exit("Bad port");
  439. }
  440. svr_opts.ports[svr_opts.portcount] = port;
  441. svr_opts.addresses[svr_opts.portcount] = address;
  442. svr_opts.portcount++;
  443. }
  444. static void disablekey(int type) {
  445. int i;
  446. TRACE(("Disabling key type %d", type))
  447. for (i = 0; sigalgs[i].name != NULL; i++) {
  448. if (sigalgs[i].val == type) {
  449. sigalgs[i].usable = 0;
  450. break;
  451. }
  452. }
  453. }
  454. static void loadhostkey_helper(const char *name, void** src, void** dst, int fatal_duplicate) {
  455. if (*dst) {
  456. if (fatal_duplicate) {
  457. dropbear_exit("Only one %s key can be specified", name);
  458. }
  459. } else {
  460. *dst = *src;
  461. *src = NULL;
  462. }
  463. }
  464. /* Must be called after syslog/etc is working */
  465. static void loadhostkey(const char *keyfile, int fatal_duplicate) {
  466. sign_key * read_key = new_sign_key();
  467. char *expand_path = expand_homedir_path(keyfile);
  468. enum signkey_type type = DROPBEAR_SIGNKEY_ANY;
  469. if (readhostkey(expand_path, read_key, &type) == DROPBEAR_FAILURE) {
  470. if (!svr_opts.delay_hostkey) {
  471. dropbear_log(LOG_WARNING, "Failed loading %s", expand_path);
  472. }
  473. }
  474. m_free(expand_path);
  475. #if DROPBEAR_RSA
  476. if (type == DROPBEAR_SIGNKEY_RSA) {
  477. loadhostkey_helper("RSA", (void**)&read_key->rsakey, (void**)&svr_opts.hostkey->rsakey, fatal_duplicate);
  478. }
  479. #endif
  480. #if DROPBEAR_DSS
  481. if (type == DROPBEAR_SIGNKEY_DSS) {
  482. loadhostkey_helper("DSS", (void**)&read_key->dsskey, (void**)&svr_opts.hostkey->dsskey, fatal_duplicate);
  483. }
  484. #endif
  485. #if DROPBEAR_ECDSA
  486. #if DROPBEAR_ECC_256
  487. if (type == DROPBEAR_SIGNKEY_ECDSA_NISTP256) {
  488. loadhostkey_helper("ECDSA256", (void**)&read_key->ecckey256, (void**)&svr_opts.hostkey->ecckey256, fatal_duplicate);
  489. }
  490. #endif
  491. #if DROPBEAR_ECC_384
  492. if (type == DROPBEAR_SIGNKEY_ECDSA_NISTP384) {
  493. loadhostkey_helper("ECDSA384", (void**)&read_key->ecckey384, (void**)&svr_opts.hostkey->ecckey384, fatal_duplicate);
  494. }
  495. #endif
  496. #if DROPBEAR_ECC_521
  497. if (type == DROPBEAR_SIGNKEY_ECDSA_NISTP521) {
  498. loadhostkey_helper("ECDSA521", (void**)&read_key->ecckey521, (void**)&svr_opts.hostkey->ecckey521, fatal_duplicate);
  499. }
  500. #endif
  501. #endif /* DROPBEAR_ECDSA */
  502. #if DROPBEAR_ED25519
  503. if (type == DROPBEAR_SIGNKEY_ED25519) {
  504. loadhostkey_helper("ed25519", (void**)&read_key->ed25519key, (void**)&svr_opts.hostkey->ed25519key, fatal_duplicate);
  505. }
  506. #endif
  507. sign_key_free(read_key);
  508. TRACE(("leave loadhostkey"))
  509. }
  510. static void addhostkey(const char *keyfile) {
  511. if (svr_opts.num_hostkey_files >= MAX_HOSTKEYS) {
  512. dropbear_exit("Too many hostkeys");
  513. }
  514. svr_opts.hostkey_files[svr_opts.num_hostkey_files] = m_strdup(keyfile);
  515. svr_opts.num_hostkey_files++;
  516. }
  517. void load_all_hostkeys() {
  518. int i;
  519. int any_keys = 0;
  520. #if DROPBEAR_ECDSA
  521. int loaded_any_ecdsa = 0;
  522. #endif
  523. svr_opts.hostkey = new_sign_key();
  524. for (i = 0; i < svr_opts.num_hostkey_files; i++) {
  525. char *hostkey_file = svr_opts.hostkey_files[i];
  526. loadhostkey(hostkey_file, 1);
  527. m_free(hostkey_file);
  528. }
  529. /* Only load default host keys if a host key is not specified by the user */
  530. if (svr_opts.num_hostkey_files == 0) {
  531. #if DROPBEAR_RSA
  532. loadhostkey(RSA_PRIV_FILENAME, 0);
  533. #endif
  534. #if DROPBEAR_DSS
  535. loadhostkey(DSS_PRIV_FILENAME, 0);
  536. #endif
  537. #if DROPBEAR_ECDSA
  538. loadhostkey(ECDSA_PRIV_FILENAME, 0);
  539. #endif
  540. #if DROPBEAR_ED25519
  541. loadhostkey(ED25519_PRIV_FILENAME, 0);
  542. #endif
  543. }
  544. #if DROPBEAR_RSA
  545. if (!svr_opts.delay_hostkey && !svr_opts.hostkey->rsakey) {
  546. disablekey(DROPBEAR_SIGNKEY_RSA);
  547. } else {
  548. any_keys = 1;
  549. }
  550. #endif
  551. #if DROPBEAR_DSS
  552. if (!svr_opts.delay_hostkey && !svr_opts.hostkey->dsskey) {
  553. disablekey(DROPBEAR_SIGNKEY_DSS);
  554. } else {
  555. any_keys = 1;
  556. }
  557. #endif
  558. #if DROPBEAR_ECDSA
  559. /* We want to advertise a single ecdsa algorithm size.
  560. - If there is a ecdsa hostkey at startup we choose that that size.
  561. - If we generate at runtime we choose the default ecdsa size.
  562. - Otherwise no ecdsa keys will be advertised */
  563. /* check if any keys were loaded at startup */
  564. loaded_any_ecdsa =
  565. 0
  566. #if DROPBEAR_ECC_256
  567. || svr_opts.hostkey->ecckey256
  568. #endif
  569. #if DROPBEAR_ECC_384
  570. || svr_opts.hostkey->ecckey384
  571. #endif
  572. #if DROPBEAR_ECC_521
  573. || svr_opts.hostkey->ecckey521
  574. #endif
  575. ;
  576. any_keys |= loaded_any_ecdsa;
  577. /* Or an ecdsa key could be generated at runtime */
  578. any_keys |= svr_opts.delay_hostkey;
  579. /* At most one ecdsa key size will be left enabled */
  580. #if DROPBEAR_ECC_256
  581. if (!svr_opts.hostkey->ecckey256
  582. && (!svr_opts.delay_hostkey || loaded_any_ecdsa || ECDSA_DEFAULT_SIZE != 256 )) {
  583. disablekey(DROPBEAR_SIGNKEY_ECDSA_NISTP256);
  584. }
  585. #endif
  586. #if DROPBEAR_ECC_384
  587. if (!svr_opts.hostkey->ecckey384
  588. && (!svr_opts.delay_hostkey || loaded_any_ecdsa || ECDSA_DEFAULT_SIZE != 384 )) {
  589. disablekey(DROPBEAR_SIGNKEY_ECDSA_NISTP384);
  590. }
  591. #endif
  592. #if DROPBEAR_ECC_521
  593. if (!svr_opts.hostkey->ecckey521
  594. && (!svr_opts.delay_hostkey || loaded_any_ecdsa || ECDSA_DEFAULT_SIZE != 521 )) {
  595. disablekey(DROPBEAR_SIGNKEY_ECDSA_NISTP521);
  596. }
  597. #endif
  598. #endif /* DROPBEAR_ECDSA */
  599. #if DROPBEAR_ED25519
  600. if (!svr_opts.delay_hostkey && !svr_opts.hostkey->ed25519key) {
  601. disablekey(DROPBEAR_SIGNKEY_ED25519);
  602. } else {
  603. any_keys = 1;
  604. }
  605. #endif
  606. #if DROPBEAR_SK_ECDSA
  607. disablekey(DROPBEAR_SIGNKEY_SK_ECDSA_NISTP256);
  608. #endif
  609. #if DROPBEAR_SK_ED25519
  610. disablekey(DROPBEAR_SIGNKEY_SK_ED25519);
  611. #endif
  612. if (!any_keys) {
  613. dropbear_exit("No hostkeys available. 'dropbear -R' may be useful or run dropbearkey.");
  614. }
  615. }