-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefriend.cc
180 lines (142 loc) · 5.37 KB
/
prefriend.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
174
175
176
177
178
179
/*
* 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 "packet.h"
#include "string.h"
#include "error.h"
#include "keyagent.h"
#include <string.h>
void Server::notifyAccept( User &user, Identity &identity, const String &peerNotifyReqid )
{
/* Verify that there is a friend request. */
DbQuery checkSentRequest( mysql,
"SELECT id "
"FROM sent_friend_request "
"WHERE user_id = %L AND identity_id = %L AND peer_notify_reqid = %e ",
user.id(), identity.id(), peerNotifyReqid() );
if ( checkSentRequest.rows() != 1 )
throw FriendRequestInvalid();
DbQuery( mysql,
"UPDATE friend_claim "
"SET state = %l "
"WHERE user_id = %L AND identity_id = %L ",
FC_GRANTED_ACCEPT, user.id(), identity.id()
);
bioWrap.returnOkServer();
}
void Server::registered( User &user, Identity &identity,
const String &peerNotifyReqid, const String &friendClaimSigKey )
{
DbQuery reqids( mysql,
"SELECT user_notify_reqid "
"FROM sent_friend_request "
"WHERE user_id = %L AND identity_id = %L AND peer_notify_reqid = %e",
user.id(), identity.id(), peerNotifyReqid() );
MYSQL_ROW row = reqids.fetchRow();
String userNotifyReqid = Pointer( row[0] );
/* FIXME: use id. */
DbQuery( mysql,
"DELETE FROM sent_friend_request "
"WHERE user_id = %L AND identity_id = %L AND peer_notify_reqid = %e ",
user.id(), identity.id(), peerNotifyReqid() );
DbQuery( mysql,
"UPDATE friend_claim "
"SET state = %l "
"WHERE user_id = %L AND identity_id = %L ",
FC_ESTABLISHED, user.id(), identity.id()
);
/* Parse the public key. */
PacketPublicKey epp( friendClaimSigKey );
FriendClaim friendClaim( mysql, user, identity );
keyAgent.generateFriendClaimKeys( friendClaim.id );
keyAgent.storeFriendClaimSigKey( friendClaim.id, friendClaimSigKey );
String site = user.site();
notifAgent.notifSentFriendRequestAccepted(
site, user.iduri, identity.iduri(),
userNotifyReqid() );
addToPrimaryNetwork( user, identity );
String retPacket = keyAgent.signEncrypt( identity, user, keyAgent.pub1 );
bioWrap.returnOkServer( retPacket );
}
void Server::recieveMessagePrefriend( FriendClaim &friendClaim, const String &relid, const String &msg )
{
User user( mysql, friendClaim.userId );
Identity identity( mysql, friendClaim.identityId );
fetchPublicKey( identity );
String decrypted = keyAgent.decryptVerify( identity, user, msg );
PrefriendParser pfp;
pfp.parse( decrypted );
switch ( pfp.type ) {
case PrefriendParser::NotifyAccept:
notifyAccept( user, identity, pfp.peerNotifyReqid );
break;
case PrefriendParser::Registered:
registered( user, identity, pfp.peerNotifyReqid, pfp.friendClaimSigKey );
break;
default:
break;
}
}
void Server::acceptFriend( const String &loginToken, const String &acceptReqid )
{
User user( mysql, User::ByLoginToken(), loginToken );
if ( user.id() < 0 )
throw InvalidUser( loginToken );
/* Find the friend request. */
DbQuery friendRequest( mysql,
"SELECT identity_id, peer_notify_reqid "
"FROM friend_request "
"WHERE user_id = %L AND accept_reqid = %e",
user.id(), acceptReqid() );
/* Check for a result. */
if ( friendRequest.rows() == 0 )
throw FriendRequestInvalid();
MYSQL_ROW row = friendRequest.fetchRow();
long long identityId = parseId( row[0] );
String peerNotifyReqid = Pointer( row[1] );
Identity identity( mysql, identityId );
FriendClaim friendClaim( mysql, user, identity );
if ( friendClaim.state != FC_RECEIVED )
throw UnexpectedFriendClaimState( friendClaim.state );
/* Notify the requester. */
String notifyAccept = consNotifyAccept( peerNotifyReqid );
/* FIXME: try, catch. */
String result = sendMessageNow( user, identity.iduri,
friendClaim.putRelids[KEY_PRIV3], notifyAccept );
DbQuery( mysql,
"UPDATE friend_claim "
"SET state = %l "
"WHERE user_id = %L AND identity_id = %L ",
FC_ESTABLISHED, user.id(), identity.id()
);
keyAgent.generateFriendClaimKeys( friendClaim.id );
/* Notify the requester. */
String registered = consRegistered( peerNotifyReqid, keyAgent.pub1 );
String packet = sendMessageNow( user, identity.iduri(),
friendClaim.putRelids[KEY_PRIV3], registered );
String friendClaimSigKey = keyAgent.decryptVerify( identity, user, packet );
PacketPublicKey epp( friendClaimSigKey );
/* Result will contain the pub key. */
keyAgent.storeFriendClaimSigKey( friendClaim.id, friendClaimSigKey );
DbQuery( mysql,
"DELETE FROM friend_request WHERE user_id = %L AND accept_reqid = %e",
user.id(), acceptReqid() );
String site = user.site();
notifAgent.notifFriendRequestAccepted( site, user.iduri,
identity.iduri(), acceptReqid() );
addToPrimaryNetwork( user, identity );
bioWrap.returnOkLocal();
}