BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file PasswordListener.h
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Object used to listen on a socket, accept clients and identify them
32 * based on a number they send.
33 */
34  
35 #ifndef BADVPN_CLIENT_PASSWORDLISTENER_H
36 #define BADVPN_CLIENT_PASSWORDLISTENER_H
37  
38 #include <stdint.h>
39  
40 #include <prio.h>
41  
42 #include <nss/cert.h>
43 #include <nss/keyhi.h>
44  
45 #include <misc/debug.h>
46 #include <misc/sslsocket.h>
47 #include <structure/LinkedList1.h>
48 #include <structure/BAVL.h>
49 #include <base/DebugObject.h>
50 #include <flow/SingleStreamReceiver.h>
51 #include <system/BConnection.h>
52 #include <nspr_support/BSSLConnection.h>
53  
54 /**
55 * Handler function called when a client identifies itself with a password
56 * belonging to one of the password entries.
57 * The password entry is unregistered before the handler is called
58 * and must not be unregistered again.
59 *
60 * @param user as in {@link PasswordListener_AddEntry}
61 * @param sock structure containing a {@link BConnection} and, if TLS is enabled,
62 * the SSL socket with the bottom layer connected to the async interfaces
63 * of the {@link BConnection} object. The structure was allocated with
64 * malloc() and the user is responsible for freeing it.
65 */
66 typedef void (*PasswordListener_handler_client) (void *user, sslsocket *sock);
67  
68 struct PasswordListenerClient;
69  
70 /**
71 * Object used to listen on a socket, accept clients and identify them
72 * based on a number they send.
73 */
74 typedef struct {
75 BReactor *bsys;
76 BThreadWorkDispatcher *twd;
77 int ssl;
78 int ssl_flags;
79 PRFileDesc model_dprfd;
80 PRFileDesc *model_prfd;
81 struct PasswordListenerClient *clients_data;
82 LinkedList1 clients_free;
83 LinkedList1 clients_used;
84 BAVL passwords;
85 BListener listener;
86 DebugObject d_obj;
87 } PasswordListener;
88  
89 typedef struct {
90 uint64_t password;
91 BAVLNode tree_node;
92 PasswordListener_handler_client handler_client;
93 void *user;
94 } PasswordListener_pwentry;
95  
96 struct PasswordListenerClient {
97 PasswordListener *l;
98 LinkedList1Node list_node;
99 sslsocket *sock;
100 BSSLConnection sslcon;
101 SingleStreamReceiver receiver;
102 uint64_t recv_buffer;
103 };
104  
105 /**
106 * Initializes the object.
107 *
108 * @param l the object
109 * @param bsys reactor we live in
110 * @param twd thread work dispatcher. May be NULL if ssl_flags does not request performing SSL
111 * operations in threads.
112 * @param listen_addr address to listen on. Must be supported according to {@link BConnection_AddressSupported}.
113 * @param max_clients maximum number of client to hold until they are identified.
114 * Must be >0.
115 * @param ssl whether to use TLS. Must be 1 or 0.
116 * @param ssl_flags flags passed down to {@link BSSLConnection_MakeBackend}. May be used to
117 * request performing SSL operations in threads.
118 * @param cert if using TLS, the server certificate
119 * @param key if using TLS, the private key
120 * @return 1 on success, 0 on failure
121 */
122 int PasswordListener_Init (PasswordListener *l, BReactor *bsys, BThreadWorkDispatcher *twd, BAddr listen_addr, int max_clients, int ssl, int ssl_flags, CERTCertificate *cert, SECKEYPrivateKey *key) WARN_UNUSED;
123  
124 /**
125 * Frees the object.
126 *
127 * @param l the object
128 */
129 void PasswordListener_Free (PasswordListener *l);
130  
131 /**
132 * Registers a password entry.
133 *
134 * @param l the object
135 * @param entry uninitialized entry structure
136 * @param handler_client handler function to call when a client identifies
137 * with the password which this function returns
138 * @param user value to pass to handler function
139 * @return password which a client should send to be recognized and
140 * dispatched to the handler function. Should be treated as a numeric
141 * value, which a client should as a little-endian 64-bit unsigned integer
142 * when it connects.
143 */
144 uint64_t PasswordListener_AddEntry (PasswordListener *l, PasswordListener_pwentry *entry, PasswordListener_handler_client handler_client, void *user);
145  
146 /**
147 * Unregisters a password entry.
148 * Note that when a client is dispatched, its entry is unregistered
149 * automatically and must not be unregistered again here.
150 *
151 * @param l the object
152 * @param entry entry to unregister
153 */
154 void PasswordListener_RemoveEntry (PasswordListener *l, PasswordListener_pwentry *entry);
155  
156 #endif