Mercurial > dropbear
comparison svr-runopts.c @ 24:469950e86d0f
switching to global vars
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 20 Jul 2004 12:05:00 +0000 |
parents | fe6bca95afa7 |
children | f789045062e6 |
comparison
equal
deleted
inserted
replaced
23:c896a4dd65da | 24:469950e86d0f |
---|---|
27 #include "signkey.h" | 27 #include "signkey.h" |
28 #include "buffer.h" | 28 #include "buffer.h" |
29 #include "dbutil.h" | 29 #include "dbutil.h" |
30 #include "algo.h" | 30 #include "algo.h" |
31 | 31 |
32 svr_runopts svr_opts; /* GLOBAL */ | |
33 | |
32 static sign_key * loadhostkeys(const char * dsskeyfile, | 34 static sign_key * loadhostkeys(const char * dsskeyfile, |
33 const char * rsakeyfile); | 35 const char * rsakeyfile); |
34 static int readhostkey(const char * filename, sign_key * hostkey, int type); | 36 static int readhostkey(const char * filename, sign_key * hostkey, int type); |
35 static void printhelp(const char * progname); | 37 static void printhelp(const char * progname); |
36 | 38 |
82 RSA_PRIV_FILENAME, | 84 RSA_PRIV_FILENAME, |
83 #endif | 85 #endif |
84 DROPBEAR_MAX_PORTS, DROPBEAR_PORT); | 86 DROPBEAR_MAX_PORTS, DROPBEAR_PORT); |
85 } | 87 } |
86 | 88 |
87 /* returns NULL on failure, or a pointer to a freshly allocated | 89 void svr_getopts(int argc, char ** argv) { |
88 * runopts structure */ | |
89 runopts * svr_getopts(int argc, char ** argv) { | |
90 | 90 |
91 unsigned int i; | 91 unsigned int i; |
92 char ** next = 0; | 92 char ** next = 0; |
93 runopts * opts; | |
94 unsigned int portnum = 0; | 93 unsigned int portnum = 0; |
95 char *portstring[DROPBEAR_MAX_PORTS]; | 94 char *portstring[DROPBEAR_MAX_PORTS]; |
96 unsigned int longport; | 95 unsigned int longport; |
97 | 96 |
98 /* see printhelp() for options */ | 97 /* see printhelp() for options */ |
99 opts = (runopts*)m_malloc(sizeof(runopts)); | 98 svr_opts.rsakeyfile = NULL; |
100 opts->rsakeyfile = NULL; | 99 svr_opts.dsskeyfile = NULL; |
101 opts->dsskeyfile = NULL; | 100 svr_opts.bannerfile = NULL; |
102 opts->bannerfile = NULL; | 101 svr_opts.banner = NULL; |
103 opts->banner = NULL; | 102 svr_opts.forkbg = 1; |
104 opts->forkbg = 1; | 103 svr_opts.norootlogin = 0; |
105 opts->norootlogin = 0; | 104 svr_opts.noauthpass = 0; |
106 opts->noauthpass = 0; | 105 svr_opts.norootpass = 0; |
107 opts->norootpass = 0; | 106 opts.nolocaltcp = 0; |
108 opts->nolocaltcp = 0; | 107 opts.noremotetcp = 0; |
109 opts->noremotetcp = 0; | |
110 /* not yet | 108 /* not yet |
111 opts->ipv4 = 1; | 109 svr_opts.ipv4 = 1; |
112 opts->ipv6 = 1; | 110 svr_opts.ipv6 = 1; |
113 */ | 111 */ |
114 #ifdef DO_MOTD | 112 #ifdef DO_MOTD |
115 opts->domotd = 1; | 113 svr_opts.domotd = 1; |
116 #endif | 114 #endif |
117 #ifndef DISABLE_SYSLOG | 115 #ifndef DISABLE_SYSLOG |
118 usingsyslog = 1; | 116 svr_opts.usingsyslog = 1; |
119 #endif | 117 #endif |
120 | 118 |
121 for (i = 1; i < (unsigned int)argc; i++) { | 119 for (i = 1; i < (unsigned int)argc; i++) { |
122 if (next) { | 120 if (next) { |
123 *next = argv[i]; | 121 *next = argv[i]; |
129 } | 127 } |
130 | 128 |
131 if (argv[i][0] == '-') { | 129 if (argv[i][0] == '-') { |
132 switch (argv[i][1]) { | 130 switch (argv[i][1]) { |
133 case 'b': | 131 case 'b': |
134 next = &opts->bannerfile; | 132 next = &svr_opts.bannerfile; |
135 break; | 133 break; |
136 #ifdef DROPBEAR_DSS | 134 #ifdef DROPBEAR_DSS |
137 case 'd': | 135 case 'd': |
138 next = &opts->dsskeyfile; | 136 next = &svr_opts.dsskeyfile; |
139 break; | 137 break; |
140 #endif | 138 #endif |
141 #ifdef DROPBEAR_RSA | 139 #ifdef DROPBEAR_RSA |
142 case 'r': | 140 case 'r': |
143 next = &opts->rsakeyfile; | 141 next = &svr_opts.rsakeyfile; |
144 break; | 142 break; |
145 #endif | 143 #endif |
146 case 'F': | 144 case 'F': |
147 opts->forkbg = 0; | 145 svr_opts.forkbg = 0; |
148 break; | 146 break; |
149 #ifndef DISABLE_SYSLOG | 147 #ifndef DISABLE_SYSLOG |
150 case 'E': | 148 case 'E': |
151 usingsyslog = 0; | 149 svr_opts.usingsyslog = 0; |
152 break; | 150 break; |
153 #endif | 151 #endif |
154 #ifndef DISABLE_LOCALTCPFWD | 152 #ifndef DISABLE_LOCALTCPFWD |
155 case 'j': | 153 case 'j': |
156 opts->nolocaltcp = 1; | 154 opts.nolocaltcp = 1; |
157 break; | 155 break; |
158 #endif | 156 #endif |
159 #ifndef DISABLE_REMOTETCPFWD | 157 #ifndef DISABLE_REMOTETCPFWD |
160 case 'k': | 158 case 'k': |
161 opts->noremotetcp = 1; | 159 opts.noremotetcp = 1; |
162 break; | 160 break; |
163 #endif | 161 #endif |
164 case 'p': | 162 case 'p': |
165 if (portnum < DROPBEAR_MAX_PORTS) { | 163 if (portnum < DROPBEAR_MAX_PORTS) { |
166 portstring[portnum] = NULL; | 164 portstring[portnum] = NULL; |
169 } | 167 } |
170 break; | 168 break; |
171 #ifdef DO_MOTD | 169 #ifdef DO_MOTD |
172 /* motd is displayed by default, -m turns it off */ | 170 /* motd is displayed by default, -m turns it off */ |
173 case 'm': | 171 case 'm': |
174 opts->domotd = 0; | 172 svr_opts.domotd = 0; |
175 break; | 173 break; |
176 #endif | 174 #endif |
177 case 'w': | 175 case 'w': |
178 opts->norootlogin = 1; | 176 svr_opts.norootlogin = 1; |
179 break; | 177 break; |
180 #ifdef DROPBEAR_PASSWORD_AUTH | 178 #ifdef DROPBEAR_PASSWORD_AUTH |
181 case 's': | 179 case 's': |
182 opts->noauthpass = 1; | 180 svr_opts.noauthpass = 1; |
183 break; | 181 break; |
184 case 'g': | 182 case 'g': |
185 opts->norootpass = 1; | 183 svr_opts.norootpass = 1; |
186 break; | 184 break; |
187 #endif | 185 #endif |
188 case 'h': | 186 case 'h': |
189 printhelp(argv[0]); | 187 printhelp(argv[0]); |
190 exit(EXIT_FAILURE); | 188 exit(EXIT_FAILURE); |
191 break; | 189 break; |
192 /* | 190 /* |
193 case '4': | 191 case '4': |
194 opts->ipv4 = 0; | 192 svr_opts.ipv4 = 0; |
195 break; | 193 break; |
196 case '6': | 194 case '6': |
197 opts->ipv6 = 0; | 195 svr_opts.ipv6 = 0; |
198 break; | 196 break; |
199 */ | 197 */ |
200 default: | 198 default: |
201 fprintf(stderr, "Unknown argument %s\n", argv[i]); | 199 fprintf(stderr, "Unknown argument %s\n", argv[i]); |
202 printhelp(argv[0]); | 200 printhelp(argv[0]); |
204 break; | 202 break; |
205 } | 203 } |
206 } | 204 } |
207 } | 205 } |
208 | 206 |
209 if (opts->dsskeyfile == NULL) { | 207 if (svr_opts.dsskeyfile == NULL) { |
210 opts->dsskeyfile = DSS_PRIV_FILENAME; | 208 svr_opts.dsskeyfile = DSS_PRIV_FILENAME; |
211 } | 209 } |
212 if (opts->rsakeyfile == NULL) { | 210 if (svr_opts.rsakeyfile == NULL) { |
213 opts->rsakeyfile = RSA_PRIV_FILENAME; | 211 svr_opts.rsakeyfile = RSA_PRIV_FILENAME; |
214 } | 212 } |
215 opts->hostkey = loadhostkeys(opts->dsskeyfile, opts->rsakeyfile); | 213 svr_opts.hostkey = loadhostkeys(svr_opts.dsskeyfile, svr_opts.rsakeyfile); |
216 | 214 |
217 if (opts->bannerfile) { | 215 if (svr_opts.bannerfile) { |
218 struct stat buf; | 216 struct stat buf; |
219 if (stat(opts->bannerfile, &buf) != 0) { | 217 if (stat(svr_opts.bannerfile, &buf) != 0) { |
220 dropbear_exit("Error opening banner file '%s'", | 218 dropbear_exit("Error opening banner file '%s'", |
221 opts->bannerfile); | 219 svr_opts.bannerfile); |
222 } | 220 } |
223 | 221 |
224 if (buf.st_size > MAX_BANNER_SIZE) { | 222 if (buf.st_size > MAX_BANNER_SIZE) { |
225 dropbear_exit("Banner file too large, max is %d bytes", | 223 dropbear_exit("Banner file too large, max is %d bytes", |
226 MAX_BANNER_SIZE); | 224 MAX_BANNER_SIZE); |
227 } | 225 } |
228 | 226 |
229 opts->banner = buf_new(buf.st_size); | 227 svr_opts.banner = buf_new(buf.st_size); |
230 if (buf_readfile(opts->banner, opts->bannerfile)!=DROPBEAR_SUCCESS) { | 228 if (buf_readfile(svr_opts.banner, svr_opts.bannerfile)!=DROPBEAR_SUCCESS) { |
231 dropbear_exit("Error reading banner file '%s'", | 229 dropbear_exit("Error reading banner file '%s'", |
232 opts->bannerfile); | 230 svr_opts.bannerfile); |
233 } | 231 } |
234 buf_setpos(opts->banner, 0); | 232 buf_setpos(svr_opts.banner, 0); |
235 } | 233 } |
236 | 234 |
237 /* not yet | 235 /* not yet |
238 if (!(opts->ipv4 || opts->ipv6)) { | 236 if (!(svr_opts.ipv4 || svr_opts.ipv6)) { |
239 fprintf(stderr, "You can't disable ipv4 and ipv6.\n"); | 237 fprintf(stderr, "You can't disable ipv4 and ipv6.\n"); |
240 exit(1); | 238 exit(1); |
241 } | 239 } |
242 */ | 240 */ |
243 | 241 |
244 /* create the array of listening ports */ | 242 /* create the array of listening ports */ |
245 if (portnum == 0) { | 243 if (portnum == 0) { |
246 /* non specified */ | 244 /* non specified */ |
247 opts->portcount = 1; | 245 svr_opts.portcount = 1; |
248 opts->ports = m_malloc(sizeof(uint16_t)); | 246 svr_opts.ports = m_malloc(sizeof(uint16_t)); |
249 opts->ports[0] = DROPBEAR_PORT; | 247 svr_opts.ports[0] = DROPBEAR_PORT; |
250 } else { | 248 } else { |
251 opts->portcount = portnum; | 249 svr_opts.portcount = portnum; |
252 opts->ports = (uint16_t*)m_malloc(sizeof(uint16_t)*portnum); | 250 svr_opts.ports = (uint16_t*)m_malloc(sizeof(uint16_t)*portnum); |
253 for (i = 0; i < portnum; i++) { | 251 for (i = 0; i < portnum; i++) { |
254 if (portstring[i]) { | 252 if (portstring[i]) { |
255 longport = atoi(portstring[i]); | 253 longport = atoi(portstring[i]); |
256 if (longport <= 65535 && longport > 0) { | 254 if (longport <= 65535 && longport > 0) { |
257 opts->ports[i] = (uint16_t)longport; | 255 svr_opts.ports[i] = (uint16_t)longport; |
258 continue; | 256 continue; |
259 } | 257 } |
260 } | 258 } |
261 fprintf(stderr, "Bad port '%s'\n", | 259 fprintf(stderr, "Bad port '%s'\n", |
262 portstring[i] ? portstring[i] : "null"); | 260 portstring[i] ? portstring[i] : "null"); |
263 } | 261 } |
264 } | 262 } |
265 | 263 |
266 return opts; | |
267 } | 264 } |
268 | 265 |
269 void freerunopts(runopts* opts) { | |
270 | |
271 if (!opts) { | |
272 return; | |
273 } | |
274 | |
275 if (opts->hostkey) { | |
276 sign_key_free(opts->hostkey); | |
277 opts->hostkey = NULL; | |
278 } | |
279 | |
280 m_free(opts->ports); | |
281 m_free(opts); | |
282 } | |
283 | 266 |
284 /* returns success or failure */ | 267 /* returns success or failure */ |
285 static int readhostkey(const char * filename, sign_key * hostkey, int type) { | 268 static int readhostkey(const char * filename, sign_key * hostkey, int type) { |
286 | 269 |
287 int ret = DROPBEAR_FAILURE; | 270 int ret = DROPBEAR_FAILURE; |