-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cc
173 lines (142 loc) · 4.21 KB
/
server.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright (c) 2008-2011, Adrian Thurston <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "dsnp.h"
#include "error.h"
#include "keyagent.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
void checkSockLocal( int sock )
{
in_addr expected;
inet_aton( "127.0.0.1", &expected );
socklen_t len = sizeof( sockaddr_in );
sockaddr_in addr_in;
int res = getsockname( sock, (sockaddr*)&addr_in, &len );
if ( res < 0 )
throw SockAddrError( errno );
if ( len != sizeof( sockaddr_in ) )
throw SockAddrError( EBADMSG );
if ( addr_in.sin_addr.s_addr != expected.s_addr )
throw SockNotLocal( sock );
}
void Server::negotiation( long versions, bool tls, const String &host, const String &key )
{
/* Decide on a protocol version. --VERSION-- */
if ( ! ( versions | VERSION_MASK_0_6 ) )
throw NoCommonVersion();
/* --VERSION-- */
String replyVersion = Pointer( "0.6" );
if ( tls ) {
/* Nonlocal, find the specified host by the hostname. */
c->selectHostByHostName( host );
}
else {
/* If a local connection is requested, verify that we are indeed connected
* to something on this machine, then check the communication key. */
c->selectSiteByCommKey( key );
/* Ensure we got a connection from localhost. */
checkSockLocal( bioWrap.sockFd );
/* Convert the BioWrap to a local connection, which indicates to the
* generic error printing code that we should be using text, not binary. */
bioWrap.type = BioWrap::Local;
}
bioWrap.type = ! tls ? BioWrap::Local : BioWrap::Server;
if ( ! tls )
bioWrap.returnOkLocal( replyVersion );
else
bioWrap.returnOkServer( replyVersion );
message( "negotiation: version %s %s\n",
replyVersion(), ( tls ? "tls" : "local" ) );
if ( tls ) {
bioWrap.startTls();
message("negotiation: TLS started\n");
}
/* Connect to the database. Either have a valid local key, or we have
* established SSL. */
mysql = dbConnect();
if ( mysql == 0 )
throw DatabaseConnectionFailed();
}
void Server::runReceivedQueueBare()
{
/* Connect to the database. Either have a valid local key, or we have
* established SSL. */
mysql = dbConnect();
if ( mysql == 0 )
throw DatabaseConnectionFailed();
while ( true ) {
bool itemsLeft = deliverBroadcast();
if ( !itemsLeft )
break;
}
}
void Server::runReceivedQueue()
{
try {
runReceivedQueueBare();
}
catch ( UserError &e ) {
/* We have no more output. */
BioWrap bioWrap( BioWrap::Null );
e.print( bioWrap );
}
if ( mysql != 0 )
mysql_close( mysql );
}
void Server::runCommandBare()
{
/* Connect to the database. Either have a valid local key, or we have
* established SSL. */
mysql = dbConnect();
if ( mysql == 0 )
throw DatabaseConnectionFailed();
CommandParser parser( this );
if ( gbl.numCommandArgs < 2 )
throw ParseError(__FILE__, __LINE__);
c->selectSiteByName( Pointer( gbl.commandArgs[0] ) );
for ( int i = 1; gbl.commandArgs[i]; i++ ) {
/* Note we are sending the null of each arg. */
const char *arg = gbl.commandArgs[i];
int len = strlen( arg ) + 1;
parser.data( arg, len );
}
/* Send the last null. */
char null = 0;
parser.data( &null, 1 );
}
int Server::runCommand()
{
int exitStatus = 0;
try {
runCommandBare();
}
catch ( UserError &e ) {
/* We have no more output. */
BioWrap bioWrap( BioWrap::Null );
e.print( bioWrap );
exitStatus = 1;
}
if ( mysql != 0 )
mysql_close( mysql );
return exitStatus;
}