| 1 |
/* |
| 2 |
* This file is part of the SSH Library |
| 3 |
* |
| 4 |
* Copyright (c) 2018 by Red Hat, Inc. |
| 5 |
* |
| 6 |
* Author: Anderson Toshiyuki Sasaki <[email protected]> |
| 7 |
* |
| 8 |
* The SSH Library is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2.1 of the License, or (at your |
| 11 |
* option) any later version. |
| 12 |
* |
| 13 |
* The SSH Library is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 16 |
* License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with the SSH Library; see the file COPYING. If not, write to |
| 20 |
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 21 |
* MA 02111-1307, USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "config.h" |
| 25 |
#include "test_server.h" |
| 26 |
#include "default_cb.h" |
| 27 |
|
| 28 |
#include <libssh/priv.h> |
| 29 |
|
| 30 |
#include <libssh/libssh.h> |
| 31 |
#include <libssh/server.h> |
| 32 |
#include <libssh/callbacks.h> |
| 33 |
|
| 34 |
#ifdef HAVE_ARGP_H |
| 35 |
#include <argp.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#include <stdlib.h> |
| 39 |
#include <string.h> |
| 40 |
#include <stdio.h> |
| 41 |
#include <stdbool.h> |
| 42 |
|
| 43 |
struct arguments_st { |
| 44 |
char *address; |
| 45 |
char *port; |
| 46 |
|
| 47 |
char *ecdsa_key; |
| 48 |
char *ed25519_key; |
| 49 |
char *rsa_key; |
| 50 |
char *host_key; |
| 51 |
|
| 52 |
char *verbosity; |
| 53 |
char *auth_methods; |
| 54 |
bool with_pcap; |
| 55 |
|
| 56 |
char *pcap_file; |
| 57 |
|
| 58 |
char *username; |
| 59 |
char *password; |
| 60 |
|
| 61 |
char *config_file; |
| 62 |
char *log_file; |
| 63 |
bool with_global_config; |
| 64 |
char *pid_file; |
| 65 |
}; |
| 66 |
|
| 67 |
static void free_arguments(struct arguments_st *arguments) |
| 68 |
{ |
| 69 |
if (arguments == NULL) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
SAFE_FREE(arguments->address); |
| 74 |
SAFE_FREE(arguments->port); |
| 75 |
|
| 76 |
SAFE_FREE(arguments->ecdsa_key); |
| 77 |
SAFE_FREE(arguments->ed25519_key); |
| 78 |
SAFE_FREE(arguments->rsa_key); |
| 79 |
SAFE_FREE(arguments->host_key); |
| 80 |
|
| 81 |
SAFE_FREE(arguments->verbosity); |
| 82 |
SAFE_FREE(arguments->auth_methods); |
| 83 |
SAFE_FREE(arguments->pcap_file); |
| 84 |
|
| 85 |
SAFE_FREE(arguments->username); |
| 86 |
SAFE_FREE(arguments->password); |
| 87 |
SAFE_FREE(arguments->config_file); |
| 88 |
SAFE_FREE(arguments->log_file); |
| 89 |
SAFE_FREE(arguments->pid_file); |
| 90 |
} |
| 91 |
|
| 92 |
#ifdef HAVE_ARGP_H |
| 93 |
|
| 94 |
static void print_auth_methods(int auth_methods) |
| 95 |
{ |
| 96 |
printf("auth_methods = \n"); |
| 97 |
if (auth_methods & SSH_AUTH_METHOD_NONE) { |
| 98 |
printf("\tSSH_AUTH_METHOD_NONE\n"); |
| 99 |
} |
| 100 |
if (auth_methods & SSH_AUTH_METHOD_PASSWORD) { |
| 101 |
printf("\tSSH_AUTH_METHOD_PASSWORD\n"); |
| 102 |
} |
| 103 |
if (auth_methods & SSH_AUTH_METHOD_PUBLICKEY) { |
| 104 |
printf("\tSSH_AUTH_METHOD_PUBLICKEY\n"); |
| 105 |
} |
| 106 |
if (auth_methods & SSH_AUTH_METHOD_HOSTBASED) { |
| 107 |
printf("\tSSH_AUTH_METHOD_HOSTBASED\n"); |
| 108 |
} |
| 109 |
if (auth_methods & SSH_AUTH_METHOD_INTERACTIVE) { |
| 110 |
printf("\tSSH_AUTH_METHOD_INTERACTIVE\n"); |
| 111 |
} |
| 112 |
if (auth_methods & SSH_AUTH_METHOD_GSSAPI_MIC) { |
| 113 |
printf("\tSSH_AUTH_METHOD_GSSAPI_MIC\n"); |
| 114 |
} |
| 115 |
if (auth_methods & SSH_AUTH_METHOD_GSSAPI_KEYEX) { |
| 116 |
printf("\tSSH_AUTH_METHOD_GSSAPI_KEYEX\n"); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
static void print_verbosity(int verbosity) |
| 121 |
{ |
| 122 |
printf("verbosity = "); |
| 123 |
switch(verbosity) { |
| 124 |
case SSH_LOG_NOLOG: |
| 125 |
printf("NO LOG\n"); |
| 126 |
break; |
| 127 |
case SSH_LOG_WARNING: |
| 128 |
printf("WARNING\n"); |
| 129 |
break; |
| 130 |
case SSH_LOG_PROTOCOL: |
| 131 |
printf("PROTOCOL\n"); |
| 132 |
break; |
| 133 |
case SSH_LOG_PACKET: |
| 134 |
printf("PACKET\n"); |
| 135 |
break; |
| 136 |
case SSH_LOG_FUNCTIONS: |
| 137 |
printf("FUNCTIONS\n"); |
| 138 |
break; |
| 139 |
default: |
| 140 |
printf("UNKNOWN\n");; |
| 141 |
break; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
static void print_server_state(struct server_state_st *state) |
| 146 |
{ |
| 147 |
if (state) { |
| 148 |
printf("===================| STATE |=====================\n"); |
| 149 |
printf("address = %s\n", |
| 150 |
state->address? state->address: "NULL"); |
| 151 |
printf("port = %d\n", |
| 152 |
state->port? state->port: 0); |
| 153 |
printf("=================================================\n"); |
| 154 |
printf("ecdsa_key = %s\n", |
| 155 |
state->ecdsa_key? state->ecdsa_key: "NULL"); |
| 156 |
printf("ed25519_key = %s\n", |
| 157 |
state->ed25519_key? state->ed25519_key: "NULL"); |
| 158 |
printf("rsa_key = %s\n", |
| 159 |
state->rsa_key? state->rsa_key: "NULL"); |
| 160 |
printf("host_key = %s\n", |
| 161 |
state->host_key? state->host_key: "NULL"); |
| 162 |
printf("=================================================\n"); |
| 163 |
print_auth_methods(state->auth_methods); |
| 164 |
print_verbosity(state->verbosity); |
| 165 |
printf("with_pcap = %s\n", |
| 166 |
state->with_pcap? "TRUE": "FALSE"); |
| 167 |
printf("pcap_file = %s\n", |
| 168 |
state->pcap_file? state->pcap_file: "NULL"); |
| 169 |
printf("=================================================\n"); |
| 170 |
printf("username = %s\n", |
| 171 |
state->expected_username? state->expected_username: "NULL"); |
| 172 |
printf("password = %s\n", |
| 173 |
state->expected_password? state->expected_password: "NULL"); |
| 174 |
printf("=================================================\n"); |
| 175 |
printf("with_global_config = %s\n", |
| 176 |
state->parse_global_config? "TRUE": "FALSE"); |
| 177 |
printf("config_file = %s\n", |
| 178 |
state->config_file? state->config_file: "NULL"); |
| 179 |
printf("log_file = %s\n", state->log_file ? state->log_file : "NULL"); |
| 180 |
printf("=================================================\n"); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
static int init_server_state(struct server_state_st *state, |
| 185 |
struct arguments_st *arguments) |
| 186 |
{ |
| 187 |
int rc = 0; |
| 188 |
|
| 189 |
if (state == NULL) { |
| 190 |
rc = SSH_ERROR; |
| 191 |
goto end; |
| 192 |
} |
| 193 |
|
| 194 |
/* Initialize server state. The "arguments structure" */ |
| 195 |
if (arguments->address) { |
| 196 |
state->address = arguments->address; |
| 197 |
arguments->address = NULL; |
| 198 |
} else { |
| 199 |
state->address = strdup(SSHD_DEFAULT_ADDRESS); |
| 200 |
if (state->address == NULL) { |
| 201 |
fprintf(stderr, "Out of memory\n"); |
| 202 |
rc = SSH_ERROR; |
| 203 |
goto end; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
if (arguments->port) { |
| 208 |
state->port = atoi(arguments->port); |
| 209 |
} else { |
| 210 |
state->port = SSHD_DEFAULT_PORT; |
| 211 |
} |
| 212 |
|
| 213 |
if (arguments->ecdsa_key) { |
| 214 |
state->ecdsa_key = arguments->ecdsa_key; |
| 215 |
arguments->ecdsa_key = NULL; |
| 216 |
} else { |
| 217 |
state->ecdsa_key = NULL; |
| 218 |
} |
| 219 |
|
| 220 |
if (arguments->ed25519_key) { |
| 221 |
state->ed25519_key = arguments->ed25519_key; |
| 222 |
arguments->ed25519_key = NULL; |
| 223 |
} else { |
| 224 |
state->ed25519_key = NULL; |
| 225 |
} |
| 226 |
|
| 227 |
if (arguments->rsa_key) { |
| 228 |
state->rsa_key = arguments->rsa_key; |
| 229 |
arguments->rsa_key = NULL; |
| 230 |
} else { |
| 231 |
state->rsa_key = NULL; |
| 232 |
} |
| 233 |
|
| 234 |
if (arguments->host_key) { |
| 235 |
state->host_key = arguments->host_key; |
| 236 |
arguments->host_key = NULL; |
| 237 |
} else { |
| 238 |
state->host_key = NULL; |
| 239 |
} |
| 240 |
|
| 241 |
if (arguments->username) { |
| 242 |
state->expected_username = arguments->username; |
| 243 |
arguments->username = NULL; |
| 244 |
} else { |
| 245 |
state->expected_username = strdup(SSHD_DEFAULT_USER); |
| 246 |
if (state->expected_username == NULL) { |
| 247 |
fprintf(stderr, "Out of memory\n"); |
| 248 |
rc = SSH_ERROR; |
| 249 |
goto end; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
if (arguments->password) { |
| 254 |
state->expected_password = arguments->password; |
| 255 |
arguments->password = NULL; |
| 256 |
} else { |
| 257 |
state->expected_password = strdup(SSHD_DEFAULT_PASSWORD); |
| 258 |
if (state->expected_password == NULL) { |
| 259 |
fprintf(stderr, "Out of memory\n"); |
| 260 |
rc = SSH_ERROR; |
| 261 |
goto end; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
if (arguments->verbosity) { |
| 266 |
state->verbosity = atoi(arguments->verbosity); |
| 267 |
} else { |
| 268 |
state->verbosity = 0; |
| 269 |
} |
| 270 |
|
| 271 |
if (arguments->auth_methods) { |
| 272 |
state->auth_methods = atoi(arguments->auth_methods); |
| 273 |
} else { |
| 274 |
state->auth_methods = SSH_AUTH_METHOD_PASSWORD | |
| 275 |
SSH_AUTH_METHOD_PUBLICKEY | |
| 276 |
SSH_AUTH_METHOD_INTERACTIVE | |
| 277 |
SSH_AUTH_METHOD_GSSAPI_MIC; |
| 278 |
} |
| 279 |
|
| 280 |
state->with_pcap = arguments->with_pcap; |
| 281 |
|
| 282 |
if (arguments->pcap_file) { |
| 283 |
state->pcap_file = arguments->pcap_file; |
| 284 |
arguments->pcap_file = NULL; |
| 285 |
} else { |
| 286 |
if (arguments->with_pcap) { |
| 287 |
state->pcap_file = strdup(SSHD_DEFAULT_PCAP_FILE); |
| 288 |
if (state->pcap_file == NULL) { |
| 289 |
fprintf(stderr, "Out of memory\n"); |
| 290 |
rc = SSH_ERROR; |
| 291 |
goto end; |
| 292 |
} |
| 293 |
} else { |
| 294 |
state->pcap_file = NULL; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
state->parse_global_config = arguments->with_global_config; |
| 299 |
state->gssapi_key_exchange_algs = NULL; |
| 300 |
|
| 301 |
if (arguments->config_file) { |
| 302 |
state->config_file = arguments->config_file; |
| 303 |
arguments->config_file = NULL; |
| 304 |
} |
| 305 |
|
| 306 |
if (arguments->log_file) { |
| 307 |
state->log_file = arguments->log_file; |
| 308 |
arguments->log_file = NULL; |
| 309 |
} |
| 310 |
|
| 311 |
/* TODO make configurable */ |
| 312 |
state->max_tries = 3; |
| 313 |
state->error = 0; |
| 314 |
|
| 315 |
|
| 316 |
if (state) { |
| 317 |
print_server_state(state); |
| 318 |
} |
| 319 |
|
| 320 |
/* TODO make callbacks configurable through command line ? */ |
| 321 |
/* Set callbacks to be used */ |
| 322 |
state->handle_session = default_handle_session_cb; |
| 323 |
|
| 324 |
/* Check required parameters */ |
| 325 |
if (state->address == NULL) { |
| 326 |
rc = SSH_ERROR; |
| 327 |
goto end; |
| 328 |
} |
| 329 |
|
| 330 |
end: |
| 331 |
if (rc != 0) { |
| 332 |
free_server_state(state); |
| 333 |
} |
| 334 |
|
| 335 |
return rc; |
| 336 |
} |
| 337 |
|
| 338 |
const char *argp_program_version = "libssh test server " |
| 339 |
SSH_STRINGIFY(LIBSSH_VERSION); |
| 340 |
const char *argp_program_bug_address = "<[email protected]>"; |
| 341 |
|
| 342 |
/* Program documentation. */ |
| 343 |
static char doc[] = "libssh -- a Secure Shell protocol implementation"; |
| 344 |
|
| 345 |
/* A description of the arguments we accept. */ |
| 346 |
static char args_doc[] = "BINDADDR"; |
| 347 |
|
| 348 |
/* The options we understand. */ |
| 349 |
static struct argp_option options[] = { |
| 350 |
{ |
| 351 |
.name = "port", |
| 352 |
.key = 'p', |
| 353 |
.arg = "PORT", |
| 354 |
.flags = 0, |
| 355 |
.doc = "Set the port to bind.", |
| 356 |
.group = 0 |
| 357 |
}, |
| 358 |
{ |
| 359 |
.name = "ecdsakey", |
| 360 |
.key = 'c', |
| 361 |
.arg = "FILE", |
| 362 |
.flags = 0, |
| 363 |
.doc = "Set the ECDSA key.", |
| 364 |
.group = 0 |
| 365 |
}, |
| 366 |
{ |
| 367 |
.name = "ed25519key", |
| 368 |
.key = 'e', |
| 369 |
.arg = "FILE", |
| 370 |
.flags = 0, |
| 371 |
.doc = "Set the ed25519 key.", |
| 372 |
.group = 0 |
| 373 |
}, |
| 374 |
{ |
| 375 |
.name = "rsakey", |
| 376 |
.key = 'r', |
| 377 |
.arg = "FILE", |
| 378 |
.flags = 0, |
| 379 |
.doc = "Set the RSA key.", |
| 380 |
.group = 0 |
| 381 |
}, |
| 382 |
{ |
| 383 |
.name = "hostkey", |
| 384 |
.key = 'k', |
| 385 |
.arg = "FILE", |
| 386 |
.flags = 0, |
| 387 |
.doc = "Set the host key.", |
| 388 |
.group = 0 |
| 389 |
}, |
| 390 |
{ |
| 391 |
.name = "pcapfile", |
| 392 |
.key = 'f', |
| 393 |
.arg = "FILE", |
| 394 |
.flags = 0, |
| 395 |
.doc = "Set the pcap output file.", |
| 396 |
.group = 0 |
| 397 |
}, |
| 398 |
{ |
| 399 |
.name = "pid_file", |
| 400 |
.key = 'i', |
| 401 |
.arg = "FILE", |
| 402 |
.flags = 0, |
| 403 |
.doc = "The server will write its pid in this file, if provided.", |
| 404 |
.group = 0 |
| 405 |
}, |
| 406 |
{ |
| 407 |
.name = "auth-methods", |
| 408 |
.key = 'a', |
| 409 |
.arg = "METHODS", |
| 410 |
.flags = 0, |
| 411 |
.doc = "Set supported authentication methods.", |
| 412 |
.group = 0 |
| 413 |
}, |
| 414 |
{ |
| 415 |
.name = "user", |
| 416 |
.key = 'u', |
| 417 |
.arg = "USERNAME", |
| 418 |
.flags = 0, |
| 419 |
.doc = "Set expected username.", |
| 420 |
.group = 0 |
| 421 |
}, |
| 422 |
{ |
| 423 |
.name = "verbosity", |
| 424 |
.key = 'v', |
| 425 |
.arg = "VERBOSITY", |
| 426 |
.flags = 0, |
| 427 |
.doc = "Set output verbosity [0-4].", |
| 428 |
.group = 0 |
| 429 |
}, |
| 430 |
{ |
| 431 |
.name = "with-pcap", |
| 432 |
.key = 'w', |
| 433 |
.arg = NULL, |
| 434 |
.flags = 0, |
| 435 |
.doc = "Use PCAP.", |
| 436 |
.group = 0 |
| 437 |
}, |
| 438 |
{ |
| 439 |
.name = "without-global-config", |
| 440 |
.key = 'g', |
| 441 |
.arg = NULL, |
| 442 |
.flags = 0, |
| 443 |
.doc = "Do not use system-wide configuration file.", |
| 444 |
.group = 0 |
| 445 |
}, |
| 446 |
{ |
| 447 |
.name = "config", |
| 448 |
.key = 'C', |
| 449 |
.arg = "CONFIG_FILE", |
| 450 |
.flags = 0, |
| 451 |
.doc = "Use this server configuration file.", |
| 452 |
.group = 0 |
| 453 |
}, |
| 454 |
{ |
| 455 |
.name = "log_file", |
| 456 |
.key = 'l', |
| 457 |
.arg = "LOG_FILE", |
| 458 |
.flags = 0, |
| 459 |
.doc = "Output log to this file.", |
| 460 |
.group = 0 |
| 461 |
}, |
| 462 |
{ .name = NULL } |
| 463 |
}; |
| 464 |
|
| 465 |
/* Parse a single option. */ |
| 466 |
static error_t parse_opt (int key, char *arg, struct argp_state *state) |
| 467 |
{ |
| 468 |
/* Get the input argument from argp_parse, which we |
| 469 |
* know is a pointer to our arguments structure. |
| 470 |
*/ |
| 471 |
struct arguments_st *arguments = state->input; |
| 472 |
error_t rc = 0; |
| 473 |
|
| 474 |
if (arguments == NULL) { |
| 475 |
fprintf(stderr, "NULL pointer to arguments structure provided\n"); |
| 476 |
rc = EINVAL; |
| 477 |
goto end; |
| 478 |
} |
| 479 |
|
| 480 |
switch (key) { |
| 481 |
case 'c': |
| 482 |
arguments->ecdsa_key = strdup(arg); |
| 483 |
if (arguments->ecdsa_key == NULL) { |
| 484 |
fprintf(stderr, "Out of memory\n"); |
| 485 |
rc = ENOMEM; |
| 486 |
goto end; |
| 487 |
} |
| 488 |
break; |
| 489 |
case 'e': |
| 490 |
arguments->ed25519_key = strdup(arg); |
| 491 |
if (arguments->ed25519_key == NULL) { |
| 492 |
fprintf(stderr, "Out of memory\n"); |
| 493 |
rc = ENOMEM; |
| 494 |
goto end; |
| 495 |
} |
| 496 |
break; |
| 497 |
case 'f': |
| 498 |
arguments->pcap_file = strdup(arg); |
| 499 |
if (arguments->pcap_file == NULL) { |
| 500 |
fprintf(stderr, "Out of memory\n"); |
| 501 |
rc = ENOMEM; |
| 502 |
goto end; |
| 503 |
} |
| 504 |
break; |
| 505 |
case 'i': |
| 506 |
arguments->pid_file = strdup(arg); |
| 507 |
if (arguments->pid_file == NULL) { |
| 508 |
fprintf(stderr, "Out of memory\n"); |
| 509 |
rc = ENOMEM; |
| 510 |
goto end; |
| 511 |
} |
| 512 |
break; |
| 513 |
case 'k': |
| 514 |
arguments->host_key = strdup(arg); |
| 515 |
if (arguments->host_key == NULL) { |
| 516 |
fprintf(stderr, "Out of memory\n"); |
| 517 |
rc = ENOMEM; |
| 518 |
goto end; |
| 519 |
} |
| 520 |
break; |
| 521 |
case 'a': |
| 522 |
arguments->auth_methods = strdup(arg); |
| 523 |
if (arguments->auth_methods == NULL) { |
| 524 |
fprintf(stderr, "Out of memory\n"); |
| 525 |
rc = ENOMEM; |
| 526 |
goto end; |
| 527 |
} |
| 528 |
break; |
| 529 |
case 'p': |
| 530 |
arguments->port = strdup(arg); |
| 531 |
if (arguments->port == NULL) { |
| 532 |
fprintf(stderr, "Out of memory\n"); |
| 533 |
rc = ENOMEM; |
| 534 |
goto end; |
| 535 |
} |
| 536 |
break; |
| 537 |
case 'r': |
| 538 |
arguments->rsa_key = strdup(arg); |
| 539 |
if (arguments->rsa_key == NULL) { |
| 540 |
fprintf(stderr, "Out of memory\n"); |
| 541 |
rc = ENOMEM; |
| 542 |
goto end; |
| 543 |
} |
| 544 |
break; |
| 545 |
case 'u': |
| 546 |
arguments->username = strdup(arg); |
| 547 |
if (arguments->username == NULL) { |
| 548 |
fprintf(stderr, "Out of memory\n"); |
| 549 |
rc = ENOMEM; |
| 550 |
goto end; |
| 551 |
} |
| 552 |
break; |
| 553 |
case 'v': |
| 554 |
arguments->verbosity = strdup(arg); |
| 555 |
if (arguments->verbosity == NULL) { |
| 556 |
fprintf(stderr, "Out of memory\n"); |
| 557 |
rc = ENOMEM; |
| 558 |
goto end; |
| 559 |
} |
| 560 |
break; |
| 561 |
case 'w': |
| 562 |
arguments->with_pcap = true; |
| 563 |
break; |
| 564 |
case 'g': |
| 565 |
arguments->with_global_config = false; |
| 566 |
break; |
| 567 |
case 'C': |
| 568 |
arguments->config_file = strdup(arg); |
| 569 |
if (arguments->config_file == NULL) { |
| 570 |
fprintf(stderr, "Out of memory\n"); |
| 571 |
rc = ENOMEM; |
| 572 |
goto end; |
| 573 |
} |
| 574 |
break; |
| 575 |
case 'l': |
| 576 |
arguments->log_file = strdup(arg); |
| 577 |
if (arguments->log_file == NULL) { |
| 578 |
fprintf(stderr, "Out of memory\n"); |
| 579 |
rc = ENOMEM; |
| 580 |
goto end; |
| 581 |
} |
| 582 |
break; |
| 583 |
case ARGP_KEY_ARG: |
| 584 |
if (state->arg_num >= 1) { |
| 585 |
/* Too many arguments. */ |
| 586 |
printf("Too many arguments\n"); |
| 587 |
argp_usage(state); |
| 588 |
} |
| 589 |
arguments->address = strdup(arg); |
| 590 |
if (arguments->address == NULL) { |
| 591 |
fprintf(stderr, "Out of memory\n"); |
| 592 |
rc = ENOMEM; |
| 593 |
goto end; |
| 594 |
} |
| 595 |
break; |
| 596 |
case ARGP_KEY_END: |
| 597 |
if (state->arg_num < 1) { |
| 598 |
printf("Too few arguments\n"); |
| 599 |
/* Not enough arguments. */ |
| 600 |
argp_usage(state); |
| 601 |
} |
| 602 |
break; |
| 603 |
default: |
| 604 |
return ARGP_ERR_UNKNOWN; |
| 605 |
} |
| 606 |
|
| 607 |
end: |
| 608 |
return rc; |
| 609 |
} |
| 610 |
|
| 611 |
/* Our argp parser. */ |
| 612 |
static struct argp argp = {options, parse_opt, args_doc, doc, NULL, NULL, NULL}; |
| 613 |
|
| 614 |
#endif /* HAVE_ARGP_H */ |
| 615 |
|
| 616 |
int main(UNUSED_PARAM(int argc), UNUSED_PARAM(char **argv)) |
| 617 |
{ |
| 618 |
int rc; |
| 619 |
FILE *pid_file; |
| 620 |
pid_t pid; |
| 621 |
|
| 622 |
struct arguments_st arguments = { |
| 623 |
.address = NULL, |
| 624 |
.with_global_config = true, |
| 625 |
}; |
| 626 |
struct server_state_st *state = calloc(1, sizeof(struct server_state_st)); |
| 627 |
|
| 628 |
if (state == NULL) { |
| 629 |
printf("Failed to allocate memory\n"); |
| 630 |
return -1; |
| 631 |
} |
| 632 |
|
| 633 |
#ifdef HAVE_ARGP_H |
| 634 |
argp_parse (&argp, argc, argv, 0, 0, &arguments); |
| 635 |
#endif |
| 636 |
|
| 637 |
if (arguments.pid_file) { |
| 638 |
pid_file = fopen(arguments.pid_file, "w"); |
| 639 |
if (pid_file == NULL) { |
| 640 |
rc = -1; |
| 641 |
free_server_state(state); |
| 642 |
SAFE_FREE(state); |
| 643 |
goto free_arguments; |
| 644 |
} |
| 645 |
pid = getpid(); |
| 646 |
fprintf(pid_file, "%d\n", pid); |
| 647 |
fclose(pid_file); |
| 648 |
} |
| 649 |
|
| 650 |
/* Initialize the state using default or given parameters */ |
| 651 |
rc = init_server_state(state, &arguments); |
| 652 |
if (rc != 0) { |
| 653 |
free_server_state(state); |
| 654 |
SAFE_FREE(state); |
| 655 |
goto free_arguments; |
| 656 |
} |
| 657 |
|
| 658 |
/* Free the arguments used to initialize the state before fork */ |
| 659 |
free_arguments(&arguments); |
| 660 |
|
| 661 |
/* Run the server: Frees the state in all processes */ |
| 662 |
rc = run_server(state); |
| 663 |
|
| 664 |
free_arguments: |
| 665 |
free_arguments(&arguments); |
| 666 |
return rc; |
| 667 |
} |