1 
2 /***************************************************************************
3  * Connection.h -- The "Connection" class holds information specifically   *
4  * pertaining to connection probes. Objects of this class must always      *
5  * belong to a certain "Service" object.                                   *
6  *                                                                         *
7  ***********************IMPORTANT NMAP LICENSE TERMS************************
8  *                                                                         *
9  * The Nmap Security Scanner is (C) 1996-2019 Insecure.Com LLC ("The Nmap  *
10  * Project"). Nmap is also a registered trademark of the Nmap Project.     *
11  * This program is free software; you may redistribute and/or modify it    *
12  * under the terms of the GNU General Public License as published by the   *
13  * Free Software Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE   *
14  * CLARIFICATIONS AND EXCEPTIONS DESCRIBED HEREIN.  This guarantees your   *
15  * right to use, modify, and redistribute this software under certain      *
16  * conditions.  If you wish to embed Nmap technology into proprietary      *
17  * software, we sell alternative licenses (contact sales@nmap.com).        *
18  * Dozens of software vendors already license Nmap technology such as      *
19  * host discovery, port scanning, OS detection, version detection, and     *
20  * the Nmap Scripting Engine.                                              *
21  *                                                                         *
22  * Note that the GPL places important restrictions on "derivative works",  *
23  * yet it does not provide a detailed definition of that term.  To avoid   *
24  * misunderstandings, we interpret that term as broadly as copyright law   *
25  * allows.  For example, we consider an application to constitute a        *
26  * derivative work for the purpose of this license if it does any of the   *
27  * following with any software or content covered by this license          *
28  * ("Covered Software"):                                                   *
29  *                                                                         *
30  * o Integrates source code from Covered Software.                         *
31  *                                                                         *
32  * o Reads or includes copyrighted data files, such as Nmap's nmap-os-db   *
33  * or nmap-service-probes.                                                 *
34  *                                                                         *
35  * o Is designed specifically to execute Covered Software and parse the    *
36  * results (as opposed to typical shell or execution-menu apps, which will *
37  * execute anything you tell them to).                                     *
38  *                                                                         *
39  * o Includes Covered Software in a proprietary executable installer.  The *
40  * installers produced by InstallShield are an example of this.  Including *
41  * Nmap with other software in compressed or archival form does not        *
42  * trigger this provision, provided appropriate open source decompression  *
43  * or de-archiving software is widely available for no charge.  For the    *
44  * purposes of this license, an installer is considered to include Covered *
45  * Software even if it actually retrieves a copy of Covered Software from  *
46  * another source during runtime (such as by downloading it from the       *
47  * Internet).                                                              *
48  *                                                                         *
49  * o Links (statically or dynamically) to a library which does any of the  *
50  * above.                                                                  *
51  *                                                                         *
52  * o Executes a helper program, module, or script to do any of the above.  *
53  *                                                                         *
54  * This list is not exclusive, but is meant to clarify our interpretation  *
55  * of derived works with some common examples.  Other people may interpret *
56  * the plain GPL differently, so we consider this a special exception to   *
57  * the GPL that we apply to Covered Software.  Works which meet any of     *
58  * these conditions must conform to all of the terms of this license,      *
59  * particularly including the GPL Section 3 requirements of providing      *
60  * source code and allowing free redistribution of the work as a whole.    *
61  *                                                                         *
62  * As another special exception to the GPL terms, the Nmap Project grants  *
63  * permission to link the code of this program with any version of the     *
64  * OpenSSL library which is distributed under a license identical to that  *
65  * listed in the included docs/licenses/OpenSSL.txt file, and distribute   *
66  * linked combinations including the two.                                  *
67  *                                                                         *
68  * The Nmap Project has permission to redistribute Npcap, a packet         *
69  * capturing driver and library for the Microsoft Windows platform.        *
70  * Npcap is a separate work with it's own license rather than this Nmap    *
71  * license.  Since the Npcap license does not permit redistribution        *
72  * without special permission, our Nmap Windows binary packages which      *
73  * contain Npcap may not be redistributed without special permission.      *
74  *                                                                         *
75  * Any redistribution of Covered Software, including any derived works,    *
76  * must obey and carry forward all of the terms of this license, including *
77  * obeying all GPL rules and restrictions.  For example, source code of    *
78  * the whole work must be provided and free redistribution must be         *
79  * allowed.  All GPL references to "this License", are to be treated as    *
80  * including the terms and conditions of this license text as well.        *
81  *                                                                         *
82  * Because this license imposes special exceptions to the GPL, Covered     *
83  * Work may not be combined (even as part of a larger work) with plain GPL *
84  * software.  The terms, conditions, and exceptions of this license must   *
85  * be included as well.  This license is incompatible with some other open *
86  * source licenses as well.  In some cases we can relicense portions of    *
87  * Nmap or grant special permissions to use it in other open source        *
88  * software.  Please contact fyodor@nmap.org with any such requests.       *
89  * Similarly, we don't incorporate incompatible open source software into  *
90  * Covered Software without special permission from the copyright holders. *
91  *                                                                         *
92  * If you have any questions about the licensing restrictions on using     *
93  * Nmap in other works, we are happy to help.  As mentioned above, we also *
94  * offer an alternative license to integrate Nmap into proprietary         *
95  * applications and appliances.  These contracts have been sold to dozens  *
96  * of software vendors, and generally include a perpetual license as well  *
97  * as providing support and updates.  They also fund the continued         *
98  * development of Nmap.  Please email sales@nmap.com for further           *
99  * information.                                                            *
100  *                                                                         *
101  * If you have received a written license agreement or contract for        *
102  * Covered Software stating terms other than these, you may choose to use  *
103  * and redistribute Covered Software under those terms instead of these.   *
104  *                                                                         *
105  * Source is provided to this software because we believe users have a     *
106  * right to know exactly what a program is going to do before they run it. *
107  * This also allows you to audit the software for security holes.          *
108  *                                                                         *
109  * Source code also allows you to port Nmap to new platforms, fix bugs,    *
110  * and add new features.  You are highly encouraged to send your changes   *
111  * to the dev@nmap.org mailing list for possible incorporation into the    *
112  * main distribution.  By sending these changes to Fyodor or one of the    *
113  * Insecure.Org development mailing lists, or checking them into the Nmap  *
114  * source code repository, it is understood (unless you specify            *
115  * otherwise) that you are offering the Nmap Project the unlimited,        *
116  * non-exclusive right to reuse, modify, and relicense the code.  Nmap     *
117  * will always be available Open Source, but this is important because     *
118  * the inability to relicense code has caused devastating problems for     *
119  * other Free Software projects (such as KDE and NASM).  We also           *
120  * occasionally relicense the code to third parties as discussed above.    *
121  * If you wish to specify special license conditions of your               *
122  * contributions, just say so when you send them.                          *
123  *                                                                         *
124  * This program is distributed in the hope that it will be useful, but     *
125  * WITHOUT ANY WARRANTY; without even the implied warranty of              *
126  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Nmap      *
127  * license file for more details (it's in a COPYING file included with     *
128  * Nmap, and also available from https://svn.nmap.org/nmap/COPYING)        *
129  *                                                                         *
130  ***************************************************************************/
131 
132 
133 
134 class Service;
135 
136 /*
137  * Active connection taking place for authenticating service.
138  * Each connection may try to authenticatate more than once before closing,
139  * depending on the service. For UDP 1 connection = 1 authentication session.
140  */
141 class Connection
142 {
143 	public:
144 		Connection(Service *serv);
145 		~Connection();
146 
147 		int time_started;
148 		int time_elapsed;
149 
150     char *user;
151     char *pass;
152 
153     /*
154      * True when we peer might close connection at the near moment.
155      * Consider the case, when some services after reaching the maximum
156      * authentication limit per connecton, just drop the connection without
157      * specifically telling you that you failed at the last authentication
158      * attempt. Thus, we use this, to be able to count the correct number of
159      * maximum attempts the peer lets us do (stored in 'supported_attempts'
160      * inside the Service class). A module should probably set it to true
161      * after writing the password on the wire and before issuing the next
162      * read call. Also if you use it, don't forget to set it to false, in the
163      * first state of your module, because we might need it to differentiate
164      * between normal server FINs and FINs/RSTs sent in the middle of an
165      * authentication due to strange network conditions.
166      */
167     bool peer_might_close;
168 
169     /* True if we have received a server reply, that indicated that it didn't
170      * close the connection prematurely. This may used in cases, when the
171      * server may close the connection after the maximum allowed auth attempts
172      * are reached, but will also print a relative message saying we failed.
173      */
174     bool finished_normally; /* XXX not currently used anywhere */
175 
176     bool check_closed;  /* true -> check if peer closed connection on us */
177     bool peer_alive;    /* true -> if peer is certain to be alive currently */
178     bool auth_complete; /* true -> login pair tested */
179     bool from_pool;     /* true -> login pair was extracted from pair_pool */
180     bool closed;        /* true -> connection was closed */
181     bool auth_success;  /* true -> we found a valid pair!!! */
182     bool force_close;   /* true -> forcefully close the connection */
183 
184     void *misc_info;    /* additional state information that might be needed */
185 
186     /* function pointer to module-specific free operation that deallocates
187      * all internal struct members of misc_info
188      */
189     void (* ops_free)(Connection *);
190 
191     int close_reason;
192 
193 		int state;          /* module state-machine's current state */
194 
195     Buf *inbuf;         /* buffer for inbound data */
196     Buf *outbuf;        /* buffer for outbound data */
197 
198 		unsigned long login_attempts; /* login attempts up until now */
199 		nsock_iod niod;     /* I/O descriptor for this connection */
200 
201     /* This stores our SSL session id, which will help speed up subsequent
202      * SSL connections. It's overwritten each time. void* is used so we don't
203      * need to #ifdef HAVE_OPENSSL all over. We'll cast later as needed.
204      */
205      void *ssl_session;
206 
207 		Service *service;   /* service it belongs to */
208 };
209 
210 enum close_reasons { READ_EOF, READ_TIMEOUT, CON_ERR, CON_TIMEOUT, MODULE_ERR };
211 
212