1ca32bd8dSchristos[Note: This file has not been updated for OpenSSH versions after 2ca32bd8dSchristosOpenSSH-1.2 and should be considered OBSOLETE. It has been left in 3ca32bd8dSchristosthe distribution because some of its information may still be useful 4ca32bd8dSchristosto developers.] 5ca32bd8dSchristos 6ca32bd8dSchristosThis document is intended for those who wish to read the ssh source 7ca32bd8dSchristoscode. This tries to give an overview of the structure of the code. 8ca32bd8dSchristos 9ca32bd8dSchristosCopyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi> 10ca32bd8dSchristosUpdated 17 Nov 1995. 11ca32bd8dSchristosUpdated 19 Oct 1999 for OpenSSH-1.2 12ca32bd8dSchristosUpdated 20 May 2001 note obsolete for > OpenSSH-1.2 13ca32bd8dSchristos 14ca32bd8dSchristosThe software consists of ssh (client), sshd (server), scp, sdist, and 15ca32bd8dSchristosthe auxiliary programs ssh-keygen, ssh-agent, ssh-add, and 16ca32bd8dSchristosmake-ssh-known-hosts. The main program for each of these is in a .c 17ca32bd8dSchristosfile with the same name. 18ca32bd8dSchristos 19ca32bd8dSchristosThere are some subsystems/abstractions that are used by a number of 20ca32bd8dSchristosthese programs. 21ca32bd8dSchristos 22ca32bd8dSchristos Buffer manipulation routines 23ca32bd8dSchristos 24ca32bd8dSchristos - These provide an arbitrary size buffer, where data can be appended. 25ca32bd8dSchristos Data can be consumed from either end. The code is used heavily 2655a4608bSchristos throughout ssh. The buffer manipulation functions are in 2755a4608bSchristos sshbuf*.c (header sshbuf.h). 28ca32bd8dSchristos 29ca32bd8dSchristos Compression Library 30ca32bd8dSchristos 31ca32bd8dSchristos - Ssh uses the GNU GZIP compression library (ZLIB). 32ca32bd8dSchristos 33ca32bd8dSchristos Encryption/Decryption 34ca32bd8dSchristos 35ca32bd8dSchristos - Ssh contains several encryption algorithms. These are all 36ca32bd8dSchristos accessed through the cipher.h interface. The interface code is 37*aa36fcacSchristos in cipher.c, and the implementations are either in libc or 38*aa36fcacSchristos LibreSSL. 39ca32bd8dSchristos 40ca32bd8dSchristos Multiple Precision Integer Library 41ca32bd8dSchristos 42*aa36fcacSchristos - Uses the LibreSSL BIGNUM sublibrary. 43ca32bd8dSchristos 44ca32bd8dSchristos Random Numbers 45ca32bd8dSchristos 46ca32bd8dSchristos - Uses arc4random() and such. 47ca32bd8dSchristos 48ca32bd8dSchristos RSA key generation, encryption, decryption 49ca32bd8dSchristos 50ca32bd8dSchristos - Ssh uses the RSA routines in libssl. 51ca32bd8dSchristos 52ca32bd8dSchristos RSA key files 53ca32bd8dSchristos 54ca32bd8dSchristos - RSA keys are stored in files with a special format. The code to 55ca32bd8dSchristos read/write these files is in authfile.c. The files are normally 56ca32bd8dSchristos encrypted with a passphrase. The functions to read passphrases 57ca32bd8dSchristos are in readpass.c (the same code is used to read passwords). 58ca32bd8dSchristos 59ca32bd8dSchristos Binary packet protocol 60ca32bd8dSchristos 61ca32bd8dSchristos - The ssh binary packet protocol is implemented in packet.c. The 62ca32bd8dSchristos code in packet.c does not concern itself with packet types or their 63ca32bd8dSchristos execution; it contains code to build packets, to receive them and 64ca32bd8dSchristos extract data from them, and the code to compress and/or encrypt 6555a4608bSchristos packets. 66ca32bd8dSchristos 67ca32bd8dSchristos - The code in packet.c calls the buffer manipulation routines 688395c133Schristos (buffer.c, bufaux.c), compression routines (zlib), and the 698395c133Schristos encryption routines. 70ca32bd8dSchristos 71ca32bd8dSchristos X11, TCP/IP, and Agent forwarding 72ca32bd8dSchristos 73ca32bd8dSchristos - Code for various types of channel forwarding is in channels.c. 74ca32bd8dSchristos The file defines a generic framework for arbitrary communication 75ca32bd8dSchristos channels inside the secure channel, and uses this framework to 76ca32bd8dSchristos implement X11 forwarding, TCP/IP forwarding, and authentication 77ca32bd8dSchristos agent forwarding. 78ca32bd8dSchristos The new, Protocol 1.5, channel close implementation is in nchan.c 79ca32bd8dSchristos 80ca32bd8dSchristos Authentication agent 81ca32bd8dSchristos 82ca32bd8dSchristos - Code to communicate with the authentication agent is in authfd.c. 83ca32bd8dSchristos 84ca32bd8dSchristos Authentication methods 85ca32bd8dSchristos 86ca32bd8dSchristos - Code for various authentication methods resides in auth-*.c 87ca32bd8dSchristos (auth-passwd.c, auth-rh-rsa.c, auth-rhosts.c, auth-rsa.c). This 88ca32bd8dSchristos code is linked into the server. The routines also manipulate 89ca32bd8dSchristos known hosts files using code in hostfile.c. Code in canohost.c 90ca32bd8dSchristos is used to retrieve the canonical host name of the remote host. 91ca32bd8dSchristos Code in match.c is used to match host names. 92ca32bd8dSchristos 93ca32bd8dSchristos - In the client end, authentication code is in sshconnect.c. It 94ca32bd8dSchristos reads Passwords/passphrases using code in readpass.c. It reads 95ca32bd8dSchristos RSA key files with authfile.c. It communicates the 96ca32bd8dSchristos authentication agent using authfd.c. 97ca32bd8dSchristos 98ca32bd8dSchristos The ssh client 99ca32bd8dSchristos 100ca32bd8dSchristos - The client main program is in ssh.c. It first parses arguments 101ca32bd8dSchristos and reads configuration (readconf.c), then calls ssh_connect (in 102ca32bd8dSchristos sshconnect.c) to open a connection to the server (possibly via a 103ca32bd8dSchristos proxy), and performs authentication (ssh_login in sshconnect.c). 104ca32bd8dSchristos It then makes any pty, forwarding, etc. requests. It may call 105ca32bd8dSchristos code in ttymodes.c to encode current tty modes. Finally it 106ca32bd8dSchristos calls client_loop in clientloop.c. This does the real work for 107ca32bd8dSchristos the session. 108ca32bd8dSchristos 109ca32bd8dSchristos Pseudo-tty manipulation and tty modes 110ca32bd8dSchristos 111ca32bd8dSchristos - Code to allocate and use a pseudo tty is in pty.c. Code to 112ca32bd8dSchristos encode and set terminal modes is in ttymodes.c. 113ca32bd8dSchristos 114ca32bd8dSchristos Logging in (updating utmp, lastlog, etc.) 115ca32bd8dSchristos 116ca32bd8dSchristos - The code to do things that are done when a user logs in are in 117ca32bd8dSchristos login.c. This includes things such as updating the utmp, wtmp, 118ca32bd8dSchristos and lastlog files. Some of the code is in sshd.c. 119ca32bd8dSchristos 120ca32bd8dSchristos Writing to the system log and terminal 121ca32bd8dSchristos 122ca32bd8dSchristos - The programs use the functions fatal(), log(), debug(), error() 123ca32bd8dSchristos in many places to write messages to system log or user's 124ca32bd8dSchristos terminal. The implementation that logs to system log is in 125ca32bd8dSchristos log-server.c; it is used in the server program. The other 126ca32bd8dSchristos programs use an implementation that sends output to stderr; it 127ca32bd8dSchristos is in log-client.c. The definitions are in ssh.h. 128ca32bd8dSchristos 129ca32bd8dSchristos The sshd server (daemon) 130ca32bd8dSchristos 131ca32bd8dSchristos - The sshd daemon starts by processing arguments and reading the 132ca32bd8dSchristos configuration file (servconf.c). It then reads the host key, 133ca32bd8dSchristos starts listening for connections, and generates the server key. 134ca32bd8dSchristos The server key will be regenerated every hour by an alarm. 135ca32bd8dSchristos 136ca32bd8dSchristos - When the server receives a connection, it forks, disables the 137ca32bd8dSchristos regeneration alarm, and starts communicating with the client. 138ca32bd8dSchristos They first perform identification string exchange, then 139ca32bd8dSchristos negotiate encryption, then perform authentication, preparatory 140ca32bd8dSchristos operations, and finally the server enters the normal session 141ca32bd8dSchristos mode by calling server_loop in serverloop.c. This does the real 142ca32bd8dSchristos work, calling functions in other modules. 143ca32bd8dSchristos 144ca32bd8dSchristos - The code for the server is in sshd.c. It contains a lot of 145ca32bd8dSchristos stuff, including: 146ca32bd8dSchristos - server main program 147ca32bd8dSchristos - waiting for connections 148ca32bd8dSchristos - processing new connection 149ca32bd8dSchristos - authentication 150ca32bd8dSchristos - preparatory operations 151ca32bd8dSchristos - building up the execution environment for the user program 152ca32bd8dSchristos - starting the user program. 153ca32bd8dSchristos 154ca32bd8dSchristos Auxiliary files 155ca32bd8dSchristos 156ca32bd8dSchristos - There are several other files in the distribution that contain 157ca32bd8dSchristos various auxiliary routines: 158ca32bd8dSchristos ssh.h the main header file for ssh (various definitions) 159ca32bd8dSchristos uidswap.c uid-swapping 160ca32bd8dSchristos xmalloc.c "safe" malloc routines 161ca32bd8dSchristos 162*aa36fcacSchristos$OpenBSD: OVERVIEW,v 1.15 2018/10/23 05:56:35 djm Exp $ 163*aa36fcacSchristos$NetBSD: OVERVIEW,v 1.8 2019/04/20 17:16:40 christos Exp $ 164