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