Coverage Report

Created: 2023-09-25 06:08

/src/dropbear/src/common-runopts.c
Line
Count
Source (jump to first uncovered line)
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
25
#include "includes.h"
26
#include "runopts.h"
27
#include "signkey.h"
28
#include "buffer.h"
29
#include "dbutil.h"
30
#include "auth.h"
31
#include "algo.h"
32
#include "dbrandom.h"
33
34
runopts opts; /* GLOBAL */
35
36
/* returns success or failure, and the keytype in *type. If we want
37
 * to restrict the type, type can contain a type to return */
38
int readhostkey(const char * filename, sign_key * hostkey,
39
1
  enum signkey_type *type) {
40
41
1
  int ret = DROPBEAR_FAILURE;
42
1
  buffer *buf;
43
44
1
  buf = buf_new(MAX_PRIVKEY_SIZE);
45
46
1
  if (buf_readfile(buf, filename) == DROPBEAR_FAILURE) {
47
1
    goto out;
48
1
  }
49
0
  buf_setpos(buf, 0);
50
51
0
  addrandom(buf_getptr(buf, buf->len), buf->len);
52
53
0
  if (buf_get_priv_key(buf, hostkey, type) == DROPBEAR_FAILURE) {
54
0
    goto out;
55
0
  }
56
57
0
  ret = DROPBEAR_SUCCESS;
58
1
out:
59
60
1
  buf_burn_free(buf);
61
1
  return ret;
62
0
}
63
64
#if DROPBEAR_USER_ALGO_LIST
65
void
66
1
parse_ciphers_macs() {
67
1
  int printed_help = 0;
68
1
  if (opts.cipher_list) {
69
0
    if (strcmp(opts.cipher_list, "help") == 0) {
70
0
      char *ciphers = algolist_string(sshciphers);
71
0
      dropbear_log(LOG_INFO, "Available ciphers: %s", ciphers);
72
0
      m_free(ciphers);
73
0
      printed_help = 1;
74
0
    } else {
75
0
      if (check_user_algos(opts.cipher_list, sshciphers, "cipher") == 0) {
76
0
        dropbear_exit("No valid ciphers specified for '-c'");
77
0
      }
78
0
    }
79
0
  }
80
81
1
  if (opts.mac_list) {
82
0
    if (strcmp(opts.mac_list, "help") == 0) {
83
0
      char *macs = algolist_string(sshhashes);
84
0
      dropbear_log(LOG_INFO, "Available MACs: %s", macs);
85
0
      m_free(macs);
86
0
      printed_help = 1;
87
0
    } else {
88
0
      if (check_user_algos(opts.mac_list, sshhashes, "MAC") == 0) {
89
0
        dropbear_exit("No valid MACs specified for '-m'");
90
0
      }
91
0
    }
92
0
  }
93
1
  if (printed_help) {
94
0
    dropbear_exit(".");
95
0
  }
96
1
}
97
#endif
98
99
0
void print_version() {
100
0
  fprintf(stderr, "Dropbear v%s\n", DROPBEAR_VERSION);
101
0
}
102
103
0
void parse_recv_window(const char* recv_window_arg) {
104
0
  int ret;
105
0
  unsigned int rw;
106
107
0
  ret = m_str_to_uint(recv_window_arg, &rw);
108
0
  if (ret == DROPBEAR_FAILURE || rw == 0 || rw > MAX_RECV_WINDOW) {
109
0
    if (rw > MAX_RECV_WINDOW) {
110
0
      opts.recv_window = MAX_RECV_WINDOW;
111
0
    }
112
0
    dropbear_log(LOG_WARNING, "Bad recv window '%s', using %d",
113
0
      recv_window_arg, opts.recv_window);
114
0
  } else {
115
0
    opts.recv_window = rw;
116
0
  }
117
118
0
}
119
120
/* Splits addr:port. Handles IPv6 [2001:0011::4]:port style format.
121
   Returns first/second parts as malloced strings, second will
122
   be NULL if no separator is found.
123
   :port  ->  (NULL, "port")
124
   port  ->   (port, NULL)
125
   addr:port  (addr, port)
126
   addr: ->   (addr, "")
127
   Returns DROPBEAR_SUCCESS/DROPBEAR_FAILURE */
128
0
int split_address_port(const char* spec, char **first, char ** second) {
129
0
  char *spec_copy = NULL, *addr = NULL, *colon = NULL;
130
0
  int ret = DROPBEAR_FAILURE;
131
132
0
  *first = NULL;
133
0
  *second = NULL;
134
0
  spec_copy = m_strdup(spec);
135
0
  addr = spec_copy;
136
137
0
  if (*addr == '[') {
138
0
    addr++;
139
0
    colon = strchr(addr, ']');
140
0
    if (!colon) {
141
0
      dropbear_log(LOG_WARNING, "Bad address '%s'", spec);
142
0
      goto out;
143
0
    }
144
0
    *colon = '\0';
145
0
    colon++;
146
0
    if (*colon == '\0') {
147
      /* No port part */
148
0
      colon = NULL;
149
0
    } else if (*colon != ':') {
150
0
      dropbear_log(LOG_WARNING, "Bad address '%s'", spec);
151
0
      goto out;
152
0
    }
153
0
  } else {
154
    /* search for ':', that separates address and port */
155
0
    colon = strrchr(addr, ':');
156
0
  }
157
158
  /* colon points to ':' now, or is NULL */
159
0
  if (colon) {
160
    /* Split the address/port */
161
0
    *colon = '\0';
162
0
    colon++;
163
0
    *second = m_strdup(colon);
164
0
  }
165
0
  if (strlen(addr)) {
166
0
    *first = m_strdup(addr);
167
0
  }
168
0
  ret = DROPBEAR_SUCCESS;
169
170
0
out:
171
0
  m_free(spec_copy);
172
0
  return ret;
173
0
}