1 /* $OpenBSD: auth-options.c,v 1.40 2006/08/03 03:34:41 deraadt Exp $ */ 2 /* 3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 5 * All rights reserved 6 * As far as I am concerned, the code I have written for this software 7 * can be used freely for any purpose. Any derived versions of this 8 * software must be clearly marked as such, and if the derived work is 9 * incompatible with the protocol description in the RFC file, it must be 10 * called by a name other than "ssh" or "Secure Shell". 11 */ 12 13 #include <sys/types.h> 14 15 #include <netdb.h> 16 #include <pwd.h> 17 #include <string.h> 18 #include <stdio.h> 19 #include <stdarg.h> 20 21 #include "xmalloc.h" 22 #include "match.h" 23 #include "log.h" 24 #include "canohost.h" 25 #include "buffer.h" 26 #include "channels.h" 27 #include "auth-options.h" 28 #include "servconf.h" 29 #include "misc.h" 30 #include "key.h" 31 #include "hostfile.h" 32 #include "auth.h" 33 #ifdef GSSAPI 34 #include "ssh-gss.h" 35 #endif 36 #include "monitor_wrap.h" 37 38 /* Flags set authorized_keys flags */ 39 int no_port_forwarding_flag = 0; 40 int no_agent_forwarding_flag = 0; 41 int no_x11_forwarding_flag = 0; 42 int no_pty_flag = 0; 43 44 /* "command=" option. */ 45 char *forced_command = NULL; 46 47 /* "environment=" options. */ 48 struct envstring *custom_environment = NULL; 49 50 /* "tunnel=" option. */ 51 int forced_tun_device = -1; 52 53 extern ServerOptions options; 54 55 void 56 auth_clear_options(void) 57 { 58 no_agent_forwarding_flag = 0; 59 no_port_forwarding_flag = 0; 60 no_pty_flag = 0; 61 no_x11_forwarding_flag = 0; 62 while (custom_environment) { 63 struct envstring *ce = custom_environment; 64 custom_environment = ce->next; 65 xfree(ce->s); 66 xfree(ce); 67 } 68 if (forced_command) { 69 xfree(forced_command); 70 forced_command = NULL; 71 } 72 forced_tun_device = -1; 73 channel_clear_permitted_opens(); 74 auth_debug_reset(); 75 } 76 77 /* 78 * return 1 if access is granted, 0 if not. 79 * side effect: sets key option flags 80 */ 81 int 82 auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum) 83 { 84 const char *cp; 85 int i; 86 87 /* reset options */ 88 auth_clear_options(); 89 90 if (!opts) 91 return 1; 92 93 while (*opts && *opts != ' ' && *opts != '\t') { 94 cp = "no-port-forwarding"; 95 if (strncasecmp(opts, cp, strlen(cp)) == 0) { 96 auth_debug_add("Port forwarding disabled."); 97 no_port_forwarding_flag = 1; 98 opts += strlen(cp); 99 goto next_option; 100 } 101 cp = "no-agent-forwarding"; 102 if (strncasecmp(opts, cp, strlen(cp)) == 0) { 103 auth_debug_add("Agent forwarding disabled."); 104 no_agent_forwarding_flag = 1; 105 opts += strlen(cp); 106 goto next_option; 107 } 108 cp = "no-X11-forwarding"; 109 if (strncasecmp(opts, cp, strlen(cp)) == 0) { 110 auth_debug_add("X11 forwarding disabled."); 111 no_x11_forwarding_flag = 1; 112 opts += strlen(cp); 113 goto next_option; 114 } 115 cp = "no-pty"; 116 if (strncasecmp(opts, cp, strlen(cp)) == 0) { 117 auth_debug_add("Pty allocation disabled."); 118 no_pty_flag = 1; 119 opts += strlen(cp); 120 goto next_option; 121 } 122 cp = "command=\""; 123 if (strncasecmp(opts, cp, strlen(cp)) == 0) { 124 opts += strlen(cp); 125 forced_command = xmalloc(strlen(opts) + 1); 126 i = 0; 127 while (*opts) { 128 if (*opts == '"') 129 break; 130 if (*opts == '\\' && opts[1] == '"') { 131 opts += 2; 132 forced_command[i++] = '"'; 133 continue; 134 } 135 forced_command[i++] = *opts++; 136 } 137 if (!*opts) { 138 debug("%.100s, line %lu: missing end quote", 139 file, linenum); 140 auth_debug_add("%.100s, line %lu: missing end quote", 141 file, linenum); 142 xfree(forced_command); 143 forced_command = NULL; 144 goto bad_option; 145 } 146 forced_command[i] = '\0'; 147 auth_debug_add("Forced command: %.900s", forced_command); 148 opts++; 149 goto next_option; 150 } 151 cp = "environment=\""; 152 if (options.permit_user_env && 153 strncasecmp(opts, cp, strlen(cp)) == 0) { 154 char *s; 155 struct envstring *new_envstring; 156 157 opts += strlen(cp); 158 s = xmalloc(strlen(opts) + 1); 159 i = 0; 160 while (*opts) { 161 if (*opts == '"') 162 break; 163 if (*opts == '\\' && opts[1] == '"') { 164 opts += 2; 165 s[i++] = '"'; 166 continue; 167 } 168 s[i++] = *opts++; 169 } 170 if (!*opts) { 171 debug("%.100s, line %lu: missing end quote", 172 file, linenum); 173 auth_debug_add("%.100s, line %lu: missing end quote", 174 file, linenum); 175 xfree(s); 176 goto bad_option; 177 } 178 s[i] = '\0'; 179 auth_debug_add("Adding to environment: %.900s", s); 180 debug("Adding to environment: %.900s", s); 181 opts++; 182 new_envstring = xmalloc(sizeof(struct envstring)); 183 new_envstring->s = s; 184 new_envstring->next = custom_environment; 185 custom_environment = new_envstring; 186 goto next_option; 187 } 188 cp = "from=\""; 189 if (strncasecmp(opts, cp, strlen(cp)) == 0) { 190 const char *remote_ip = get_remote_ipaddr(); 191 const char *remote_host = get_canonical_hostname( 192 options.use_dns); 193 char *patterns = xmalloc(strlen(opts) + 1); 194 195 opts += strlen(cp); 196 i = 0; 197 while (*opts) { 198 if (*opts == '"') 199 break; 200 if (*opts == '\\' && opts[1] == '"') { 201 opts += 2; 202 patterns[i++] = '"'; 203 continue; 204 } 205 patterns[i++] = *opts++; 206 } 207 if (!*opts) { 208 debug("%.100s, line %lu: missing end quote", 209 file, linenum); 210 auth_debug_add("%.100s, line %lu: missing end quote", 211 file, linenum); 212 xfree(patterns); 213 goto bad_option; 214 } 215 patterns[i] = '\0'; 216 opts++; 217 if (match_host_and_ip(remote_host, remote_ip, 218 patterns) != 1) { 219 xfree(patterns); 220 logit("Authentication tried for %.100s with " 221 "correct key but not from a permitted " 222 "host (host=%.200s, ip=%.200s).", 223 pw->pw_name, remote_host, remote_ip); 224 auth_debug_add("Your host '%.200s' is not " 225 "permitted to use this key for login.", 226 remote_host); 227 /* deny access */ 228 return 0; 229 } 230 xfree(patterns); 231 /* Host name matches. */ 232 goto next_option; 233 } 234 cp = "permitopen=\""; 235 if (strncasecmp(opts, cp, strlen(cp)) == 0) { 236 char *host, *p; 237 u_short port; 238 char *patterns = xmalloc(strlen(opts) + 1); 239 240 opts += strlen(cp); 241 i = 0; 242 while (*opts) { 243 if (*opts == '"') 244 break; 245 if (*opts == '\\' && opts[1] == '"') { 246 opts += 2; 247 patterns[i++] = '"'; 248 continue; 249 } 250 patterns[i++] = *opts++; 251 } 252 if (!*opts) { 253 debug("%.100s, line %lu: missing end quote", 254 file, linenum); 255 auth_debug_add("%.100s, line %lu: missing " 256 "end quote", file, linenum); 257 xfree(patterns); 258 goto bad_option; 259 } 260 patterns[i] = '\0'; 261 opts++; 262 p = patterns; 263 host = hpdelim(&p); 264 if (host == NULL || strlen(host) >= NI_MAXHOST) { 265 debug("%.100s, line %lu: Bad permitopen " 266 "specification <%.100s>", file, linenum, 267 patterns); 268 auth_debug_add("%.100s, line %lu: " 269 "Bad permitopen specification", file, 270 linenum); 271 xfree(patterns); 272 goto bad_option; 273 } 274 host = cleanhostname(host); 275 if (p == NULL || (port = a2port(p)) == 0) { 276 debug("%.100s, line %lu: Bad permitopen port " 277 "<%.100s>", file, linenum, p ? p : ""); 278 auth_debug_add("%.100s, line %lu: " 279 "Bad permitopen port", file, linenum); 280 xfree(patterns); 281 goto bad_option; 282 } 283 if (options.allow_tcp_forwarding) 284 channel_add_permitted_opens(host, port); 285 xfree(patterns); 286 goto next_option; 287 } 288 cp = "tunnel=\""; 289 if (strncasecmp(opts, cp, strlen(cp)) == 0) { 290 char *tun = NULL; 291 opts += strlen(cp); 292 tun = xmalloc(strlen(opts) + 1); 293 i = 0; 294 while (*opts) { 295 if (*opts == '"') 296 break; 297 tun[i++] = *opts++; 298 } 299 if (!*opts) { 300 debug("%.100s, line %lu: missing end quote", 301 file, linenum); 302 auth_debug_add("%.100s, line %lu: missing end quote", 303 file, linenum); 304 xfree(tun); 305 forced_tun_device = -1; 306 goto bad_option; 307 } 308 tun[i] = '\0'; 309 forced_tun_device = a2tun(tun, NULL); 310 xfree(tun); 311 if (forced_tun_device == SSH_TUNID_ERR) { 312 debug("%.100s, line %lu: invalid tun device", 313 file, linenum); 314 auth_debug_add("%.100s, line %lu: invalid tun device", 315 file, linenum); 316 forced_tun_device = -1; 317 goto bad_option; 318 } 319 auth_debug_add("Forced tun device: %d", forced_tun_device); 320 opts++; 321 goto next_option; 322 } 323 next_option: 324 /* 325 * Skip the comma, and move to the next option 326 * (or break out if there are no more). 327 */ 328 if (!*opts) 329 fatal("Bugs in auth-options.c option processing."); 330 if (*opts == ' ' || *opts == '\t') 331 break; /* End of options. */ 332 if (*opts != ',') 333 goto bad_option; 334 opts++; 335 /* Process the next option. */ 336 } 337 338 if (!use_privsep) 339 auth_debug_send(); 340 341 /* grant access */ 342 return 1; 343 344 bad_option: 345 logit("Bad options in %.100s file, line %lu: %.50s", 346 file, linenum, opts); 347 auth_debug_add("Bad options in %.100s file, line %lu: %.50s", 348 file, linenum, opts); 349 350 if (!use_privsep) 351 auth_debug_send(); 352 353 /* deny access */ 354 return 0; 355 } 356