1 /* 2 * 3 * ssh.h 4 * 5 * Author: Tatu Ylonen <ylo@cs.hut.fi> 6 * 7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 8 * All rights reserved 9 * 10 * Created: Fri Mar 17 17:09:37 1995 ylo 11 * 12 * Generic header file for ssh. 13 * 14 * $FreeBSD$ 15 */ 16 17 /* RCSID("$Id: ssh.h,v 1.46 2000/05/17 08:20:15 markus Exp $"); */ 18 19 #ifndef SSH_H 20 #define SSH_H 21 22 #include "rsa.h" 23 #include "cipher.h" 24 25 /* 26 * XXX 27 * The default cipher used if IDEA is not supported by the remote host. It is 28 * recommended that this be one of the mandatory ciphers (DES, 3DES), though 29 * that is not required. 30 */ 31 #define SSH_FALLBACK_CIPHER SSH_CIPHER_3DES 32 33 /* Cipher used for encrypting authentication files. */ 34 #define SSH_AUTHFILE_CIPHER SSH_CIPHER_3DES 35 36 /* Default port number. */ 37 #define SSH_DEFAULT_PORT 22 38 39 /* Maximum number of TCP/IP ports forwarded per direction. */ 40 #define SSH_MAX_FORWARDS_PER_DIRECTION 100 41 42 /* 43 * Maximum number of RSA authentication identity files that can be specified 44 * in configuration files or on the command line. 45 */ 46 #define SSH_MAX_IDENTITY_FILES 100 47 48 /* 49 * Major protocol version. Different version indicates major incompatiblity 50 * that prevents communication. 51 * 52 * Minor protocol version. Different version indicates minor incompatibility 53 * that does not prevent interoperation. 54 */ 55 #define PROTOCOL_MAJOR_1 1 56 #define PROTOCOL_MINOR_1 5 57 58 /* We support both SSH1 and SSH2 */ 59 #define PROTOCOL_MAJOR_2 2 60 #define PROTOCOL_MINOR_2 0 61 62 /* 63 * Name for the service. The port named by this service overrides the 64 * default port if present. 65 */ 66 #define SSH_SERVICE_NAME "ssh" 67 68 #define ETCDIR "/etc/ssh" 69 #define PIDDIR "/var/run" 70 71 /* 72 * System-wide file containing host keys of known hosts. This file should be 73 * world-readable. 74 */ 75 #define SSH_SYSTEM_HOSTFILE ETCDIR "/ssh_known_hosts" 76 #define SSH_SYSTEM_HOSTFILE2 ETCDIR "/ssh_known_hosts2" 77 78 /* 79 * Of these, ssh_host_key must be readable only by root, whereas ssh_config 80 * should be world-readable. 81 */ 82 #define HOST_KEY_FILE ETCDIR "/ssh_host_key" 83 #define SERVER_CONFIG_FILE ETCDIR "/sshd_config" 84 #define HOST_CONFIG_FILE ETCDIR "/ssh_config" 85 #define HOST_DSA_KEY_FILE ETCDIR "/ssh_host_dsa_key" 86 87 #define SSH_PROGRAM "/usr/bin/ssh" 88 89 /* 90 * The process id of the daemon listening for connections is saved here to 91 * make it easier to kill the correct daemon when necessary. 92 */ 93 #define SSH_DAEMON_PID_FILE PIDDIR "/sshd.pid" 94 95 /* 96 * The directory in user\'s home directory in which the files reside. The 97 * directory should be world-readable (though not all files are). 98 */ 99 #define SSH_USER_DIR ".ssh" 100 101 /* 102 * Per-user file containing host keys of known hosts. This file need not be 103 * readable by anyone except the user him/herself, though this does not 104 * contain anything particularly secret. 105 */ 106 #define SSH_USER_HOSTFILE "~/.ssh/known_hosts" 107 #define SSH_USER_HOSTFILE2 "~/.ssh/known_hosts2" 108 109 /* 110 * Name of the default file containing client-side authentication key. This 111 * file should only be readable by the user him/herself. 112 */ 113 #define SSH_CLIENT_IDENTITY ".ssh/identity" 114 #define SSH_CLIENT_ID_DSA ".ssh/id_dsa" 115 116 /* 117 * Configuration file in user\'s home directory. This file need not be 118 * readable by anyone but the user him/herself, but does not contain anything 119 * particularly secret. If the user\'s home directory resides on an NFS 120 * volume where root is mapped to nobody, this may need to be world-readable. 121 */ 122 #define SSH_USER_CONFFILE ".ssh/config" 123 124 /* 125 * File containing a list of those rsa keys that permit logging in as this 126 * user. This file need not be readable by anyone but the user him/herself, 127 * but does not contain anything particularly secret. If the user\'s home 128 * directory resides on an NFS volume where root is mapped to nobody, this 129 * may need to be world-readable. (This file is read by the daemon which is 130 * running as root.) 131 */ 132 #define SSH_USER_PERMITTED_KEYS ".ssh/authorized_keys" 133 #define SSH_USER_PERMITTED_KEYS2 ".ssh/authorized_keys2" 134 135 /* 136 * Per-user and system-wide ssh "rc" files. These files are executed with 137 * /bin/sh before starting the shell or command if they exist. They will be 138 * passed "proto cookie" as arguments if X11 forwarding with spoofing is in 139 * use. xauth will be run if neither of these exists. 140 */ 141 #define SSH_USER_RC ".ssh/rc" 142 #define SSH_SYSTEM_RC ETCDIR "/sshrc" 143 144 /* 145 * Ssh-only version of /etc/hosts.equiv. Additionally, the daemon may use 146 * ~/.rhosts and /etc/hosts.equiv if rhosts authentication is enabled. 147 */ 148 #define SSH_HOSTS_EQUIV ETCDIR "/shosts.equiv" 149 150 /* 151 * Name of the environment variable containing the pathname of the 152 * authentication socket. 153 */ 154 #define SSH_AUTHSOCKET_ENV_NAME "SSH_AUTH_SOCK" 155 156 /* 157 * Name of the environment variable containing the pathname of the 158 * authentication socket. 159 */ 160 #define SSH_AGENTPID_ENV_NAME "SSH_AGENT_PID" 161 162 /* 163 * Default path to ssh-askpass used by ssh-add, 164 * environment variable for overwriting the default location 165 */ 166 #define SSH_ASKPASS_DEFAULT "/usr/X11R6/bin/ssh-askpass" 167 #define SSH_ASKPASS_ENV "SSH_ASKPASS" 168 169 /* 170 * Force host key length and server key length to differ by at least this 171 * many bits. This is to make double encryption with rsaref work. 172 */ 173 #define SSH_KEY_BITS_RESERVED 128 174 175 /* 176 * Length of the session key in bytes. (Specified as 256 bits in the 177 * protocol.) 178 */ 179 #define SSH_SESSION_KEY_LENGTH 32 180 181 /* Name of Kerberos service for SSH to use. */ 182 #define KRB4_SERVICE_NAME "rcmd" 183 184 /* 185 * Authentication methods. New types can be added, but old types should not 186 * be removed for compatibility. The maximum allowed value is 31. 187 */ 188 #define SSH_AUTH_RHOSTS 1 189 #define SSH_AUTH_RSA 2 190 #define SSH_AUTH_PASSWORD 3 191 #define SSH_AUTH_RHOSTS_RSA 4 192 #define SSH_AUTH_TIS 5 193 #define SSH_AUTH_KRB4 6 194 #define SSH_PASS_KRB4_TGT 7 195 /* 8 to 15 are reserved */ 196 #define SSH_PASS_AFS_TOKEN 21 197 198 #define SSH_AUTH_KRB5 29 199 #define SSH_PASS_KRB5_TGT 30 200 201 /* Protocol flags. These are bit masks. */ 202 #define SSH_PROTOFLAG_SCREEN_NUMBER 1 /* X11 forwarding includes screen */ 203 #define SSH_PROTOFLAG_HOST_IN_FWD_OPEN 2 /* forwarding opens contain host */ 204 205 /* 206 * Definition of message types. New values can be added, but old values 207 * should not be removed or without careful consideration of the consequences 208 * for compatibility. The maximum value is 254; value 255 is reserved for 209 * future extension. 210 */ 211 /* Message name */ /* msg code */ /* arguments */ 212 #define SSH_MSG_NONE 0 /* no message */ 213 #define SSH_MSG_DISCONNECT 1 /* cause (string) */ 214 #define SSH_SMSG_PUBLIC_KEY 2 /* ck,msk,srvk,hostk */ 215 #define SSH_CMSG_SESSION_KEY 3 /* key (BIGNUM) */ 216 #define SSH_CMSG_USER 4 /* user (string) */ 217 #define SSH_CMSG_AUTH_RHOSTS 5 /* user (string) */ 218 #define SSH_CMSG_AUTH_RSA 6 /* modulus (BIGNUM) */ 219 #define SSH_SMSG_AUTH_RSA_CHALLENGE 7 /* int (BIGNUM) */ 220 #define SSH_CMSG_AUTH_RSA_RESPONSE 8 /* int (BIGNUM) */ 221 #define SSH_CMSG_AUTH_PASSWORD 9 /* pass (string) */ 222 #define SSH_CMSG_REQUEST_PTY 10 /* TERM, tty modes */ 223 #define SSH_CMSG_WINDOW_SIZE 11 /* row,col,xpix,ypix */ 224 #define SSH_CMSG_EXEC_SHELL 12 /* */ 225 #define SSH_CMSG_EXEC_CMD 13 /* cmd (string) */ 226 #define SSH_SMSG_SUCCESS 14 /* */ 227 #define SSH_SMSG_FAILURE 15 /* */ 228 #define SSH_CMSG_STDIN_DATA 16 /* data (string) */ 229 #define SSH_SMSG_STDOUT_DATA 17 /* data (string) */ 230 #define SSH_SMSG_STDERR_DATA 18 /* data (string) */ 231 #define SSH_CMSG_EOF 19 /* */ 232 #define SSH_SMSG_EXITSTATUS 20 /* status (int) */ 233 #define SSH_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* channel (int) */ 234 #define SSH_MSG_CHANNEL_OPEN_FAILURE 22 /* channel (int) */ 235 #define SSH_MSG_CHANNEL_DATA 23 /* ch,data (int,str) */ 236 #define SSH_MSG_CHANNEL_CLOSE 24 /* channel (int) */ 237 #define SSH_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* channel (int) */ 238 /* SSH_CMSG_X11_REQUEST_FORWARDING 26 OBSOLETE */ 239 #define SSH_SMSG_X11_OPEN 27 /* channel (int) */ 240 #define SSH_CMSG_PORT_FORWARD_REQUEST 28 /* p,host,hp (i,s,i) */ 241 #define SSH_MSG_PORT_OPEN 29 /* ch,h,p (i,s,i) */ 242 #define SSH_CMSG_AGENT_REQUEST_FORWARDING 30 /* */ 243 #define SSH_SMSG_AGENT_OPEN 31 /* port (int) */ 244 #define SSH_MSG_IGNORE 32 /* string */ 245 #define SSH_CMSG_EXIT_CONFIRMATION 33 /* */ 246 #define SSH_CMSG_X11_REQUEST_FORWARDING 34 /* proto,data (s,s) */ 247 #define SSH_CMSG_AUTH_RHOSTS_RSA 35 /* user,mod (s,mpi) */ 248 #define SSH_MSG_DEBUG 36 /* string */ 249 #define SSH_CMSG_REQUEST_COMPRESSION 37 /* level 1-9 (int) */ 250 #define SSH_CMSG_MAX_PACKET_SIZE 38 /* size 4k-1024k (int) */ 251 #define SSH_CMSG_AUTH_TIS 39 /* we use this for s/key */ 252 #define SSH_SMSG_AUTH_TIS_CHALLENGE 40 /* challenge (string) */ 253 #define SSH_CMSG_AUTH_TIS_RESPONSE 41 /* response (string) */ 254 #define SSH_CMSG_AUTH_KRB4 42 /* (KTEXT) */ 255 #define SSH_SMSG_AUTH_KRB4_RESPONSE 43 /* (KTEXT) */ 256 #define SSH_CMSG_HAVE_KRB4_TGT 44 /* credentials (s) */ 257 #define SSH_CMSG_HAVE_AFS_TOKEN 65 /* token (s) */ 258 259 #define SSH_CMSG_AUTH_KRB5 110 260 #define SSH_SMSG_AUTH_KRB5_RESPONSE 111 261 #define SSH_CMSG_HAVE_KRB5_TGT 112 262 263 /*------------ definitions for login.c -------------*/ 264 265 /* 266 * Returns the time when the user last logged in. Returns 0 if the 267 * information is not available. This must be called before record_login. 268 * The host from which the user logged in is stored in buf. 269 */ 270 unsigned long 271 get_last_login_time(uid_t uid, const char *logname, 272 char *buf, unsigned int bufsize); 273 274 /* 275 * Records that the user has logged in. This does many things normally done 276 * by login(1). 277 */ 278 void 279 record_login(pid_t pid, const char *ttyname, const char *user, uid_t uid, 280 const char *host, struct sockaddr *addr); 281 282 /* 283 * Records that the user has logged out. This does many thigs normally done 284 * by login(1) or init. 285 */ 286 void record_logout(pid_t pid, const char *ttyname); 287 288 /*------------ definitions for sshconnect.c ----------*/ 289 290 /* 291 * Opens a TCP/IP connection to the remote server on the given host. If port 292 * is 0, the default port will be used. If anonymous is zero, a privileged 293 * port will be allocated to make the connection. This requires super-user 294 * privileges if anonymous is false. Connection_attempts specifies the 295 * maximum number of tries, one per second. This returns true on success, 296 * and zero on failure. If the connection is successful, this calls 297 * packet_set_connection for the connection. 298 */ 299 int 300 ssh_connect(const char *host, struct sockaddr_storage * hostaddr, 301 u_short port, int connection_attempts, 302 int anonymous, uid_t original_real_uid, 303 const char *proxy_command); 304 305 /* 306 * Starts a dialog with the server, and authenticates the current user on the 307 * server. This does not need any extra privileges. The basic connection to 308 * the server must already have been established before this is called. If 309 * login fails, this function prints an error and never returns. This 310 * initializes the random state, and leaves it initialized (it will also have 311 * references from the packet module). 312 */ 313 314 void 315 ssh_login(int host_key_valid, RSA * host_key, const char *host, 316 struct sockaddr * hostaddr, uid_t original_real_uid); 317 318 /*------------ Definitions for various authentication methods. -------*/ 319 320 /* 321 * Tries to authenticate the user using the .rhosts file. Returns true if 322 * authentication succeeds. If ignore_rhosts is non-zero, this will not 323 * consider .rhosts and .shosts (/etc/hosts.equiv will still be used). 324 */ 325 int auth_rhosts(struct passwd * pw, const char *client_user); 326 327 /* 328 * Tries to authenticate the user using the .rhosts file and the host using 329 * its host key. Returns true if authentication succeeds. 330 */ 331 int 332 auth_rhosts_rsa(struct passwd * pw, const char *client_user, RSA* client_host_key); 333 334 /* 335 * Tries to authenticate the user using password. Returns true if 336 * authentication succeeds. 337 */ 338 int auth_password(struct passwd * pw, const char *password); 339 340 /* 341 * Performs the RSA authentication dialog with the client. This returns 0 if 342 * the client could not be authenticated, and 1 if authentication was 343 * successful. This may exit if there is a serious protocol violation. 344 */ 345 int auth_rsa(struct passwd * pw, BIGNUM * client_n); 346 347 /* 348 * Parses an RSA key (number of bits, e, n) from a string. Moves the pointer 349 * over the key. Skips any whitespace at the beginning and at end. 350 */ 351 int auth_rsa_read_key(char **cpp, unsigned int *bitsp, BIGNUM * e, BIGNUM * n); 352 353 /* 354 * Returns the name of the machine at the other end of the socket. The 355 * returned string should be freed by the caller. 356 */ 357 char *get_remote_hostname(int socket); 358 359 /* 360 * Return the canonical name of the host in the other side of the current 361 * connection (as returned by packet_get_connection). The host name is 362 * cached, so it is efficient to call this several times. 363 */ 364 const char *get_canonical_hostname(void); 365 366 /* 367 * Returns the local IP address as an ascii string. 368 */ 369 const char *get_ipaddr(int socket); 370 371 /* 372 * Returns the remote IP address as an ascii string. The value need not be 373 * freed by the caller. 374 */ 375 const char *get_remote_ipaddr(void); 376 377 /* Returns the port number of the peer of the socket. */ 378 int get_peer_port(int sock); 379 380 /* Returns the port number of the remote/local host. */ 381 int get_remote_port(void); 382 int get_local_port(void); 383 384 385 /* 386 * Performs the RSA authentication challenge-response dialog with the client, 387 * and returns true (non-zero) if the client gave the correct answer to our 388 * challenge; returns zero if the client gives a wrong answer. 389 */ 390 int auth_rsa_challenge_dialog(RSA *pk); 391 392 /* 393 * Reads a passphrase from /dev/tty with echo turned off. Returns the 394 * passphrase (allocated with xmalloc). Exits if EOF is encountered. If 395 * from_stdin is true, the passphrase will be read from stdin instead. 396 */ 397 char *read_passphrase(const char *prompt, int from_stdin); 398 399 400 /*------------ Definitions for logging. -----------------------*/ 401 402 /* Supported syslog facilities and levels. */ 403 typedef enum { 404 SYSLOG_FACILITY_DAEMON, 405 SYSLOG_FACILITY_USER, 406 SYSLOG_FACILITY_AUTH, 407 SYSLOG_FACILITY_LOCAL0, 408 SYSLOG_FACILITY_LOCAL1, 409 SYSLOG_FACILITY_LOCAL2, 410 SYSLOG_FACILITY_LOCAL3, 411 SYSLOG_FACILITY_LOCAL4, 412 SYSLOG_FACILITY_LOCAL5, 413 SYSLOG_FACILITY_LOCAL6, 414 SYSLOG_FACILITY_LOCAL7 415 } SyslogFacility; 416 417 typedef enum { 418 SYSLOG_LEVEL_QUIET, 419 SYSLOG_LEVEL_FATAL, 420 SYSLOG_LEVEL_ERROR, 421 SYSLOG_LEVEL_INFO, 422 SYSLOG_LEVEL_VERBOSE, 423 SYSLOG_LEVEL_DEBUG 424 } LogLevel; 425 /* Initializes logging. */ 426 void log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr); 427 428 /* Logging implementation, depending on server or client */ 429 void do_log(LogLevel level, const char *fmt, va_list args); 430 431 /* name to facility/level */ 432 SyslogFacility log_facility_number(char *name); 433 LogLevel log_level_number(char *name); 434 435 /* Output a message to syslog or stderr */ 436 void fatal(const char *fmt,...) __attribute__((format(printf, 1, 2))); 437 void error(const char *fmt,...) __attribute__((format(printf, 1, 2))); 438 void log(const char *fmt,...) __attribute__((format(printf, 1, 2))); 439 void verbose(const char *fmt,...) __attribute__((format(printf, 1, 2))); 440 void debug(const char *fmt,...) __attribute__((format(printf, 1, 2))); 441 442 /* same as fatal() but w/o logging */ 443 void fatal_cleanup(void); 444 445 /* 446 * Registers a cleanup function to be called by fatal()/fatal_cleanup() 447 * before exiting. It is permissible to call fatal_remove_cleanup for the 448 * function itself from the function. 449 */ 450 void fatal_add_cleanup(void (*proc) (void *context), void *context); 451 452 /* Removes a cleanup function to be called at fatal(). */ 453 void fatal_remove_cleanup(void (*proc) (void *context), void *context); 454 455 /* ---- misc */ 456 457 /* 458 * Expands tildes in the file name. Returns data allocated by xmalloc. 459 * Warning: this calls getpw*. 460 */ 461 char *tilde_expand_filename(const char *filename, uid_t my_uid); 462 463 /* remove newline at end of string */ 464 char *chop(char *s); 465 466 /* set filedescriptor to non-blocking */ 467 void set_nonblock(int fd); 468 469 /* 470 * Performs the interactive session. This handles data transmission between 471 * the client and the program. Note that the notion of stdin, stdout, and 472 * stderr in this function is sort of reversed: this function writes to stdin 473 * (of the child program), and reads from stdout and stderr (of the child 474 * program). 475 */ 476 void server_loop(pid_t pid, int fdin, int fdout, int fderr); 477 void server_loop2(void); 478 479 /* Client side main loop for the interactive session. */ 480 int client_loop(int have_pty, int escape_char); 481 482 /* Linked list of custom environment strings (see auth-rsa.c). */ 483 struct envstring { 484 struct envstring *next; 485 char *s; 486 }; 487 488 /* 489 * Ensure all of data on socket comes through. f==read || f==write 490 */ 491 ssize_t atomicio(ssize_t (*f)(), int fd, void *s, size_t n); 492 493 #ifdef KRB5 494 #include <krb5.h> 495 int auth_krb5(); /* XXX Doplnit prototypy */ 496 int auth_krb5_tgt(); 497 int krb5_init(); 498 void krb5_cleanup_proc(void *ignore); 499 int auth_krb5_password(struct passwd *pw, const char *password); 500 #endif /* KRB5 */ 501 502 #ifdef KRB4 503 #include <krb.h> 504 /* 505 * Performs Kerberos v4 mutual authentication with the client. This returns 0 506 * if the client could not be authenticated, and 1 if authentication was 507 * successful. This may exit if there is a serious protocol violation. 508 */ 509 int auth_krb4(const char *server_user, KTEXT auth, char **client); 510 int krb4_init(uid_t uid); 511 void krb4_cleanup_proc(void *ignore); 512 int auth_krb4_password(struct passwd * pw, const char *password); 513 514 #ifdef AFS 515 #include <kafs.h> 516 517 /* Accept passed Kerberos v4 ticket-granting ticket and AFS tokens. */ 518 int auth_krb4_tgt(struct passwd * pw, const char *string); 519 int auth_afs_token(struct passwd * pw, const char *token_string); 520 521 int creds_to_radix(CREDENTIALS * creds, unsigned char *buf, size_t buflen); 522 int radix_to_creds(const char *buf, CREDENTIALS * creds); 523 #endif /* AFS */ 524 525 #endif /* KRB4 */ 526 527 #ifdef SKEY 528 #include <opie.h> 529 char *skey_fake_keyinfo(char *username); 530 int auth_skey_password(struct passwd * pw, const char *password); 531 #endif /* SKEY */ 532 533 /* AF_UNSPEC or AF_INET or AF_INET6 */ 534 extern int IPv4or6; 535 536 #endif /* SSH_H */ 537