-
Notifications
You must be signed in to change notification settings - Fork 0
/
csclient.cxx
3843 lines (3436 loc) · 109 KB
/
csclient.cxx
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Compile server client functions
Copyright (C) 2010-2014 Red Hat Inc.
This file is part of systemtap, and is free software. You can
redistribute it and/or modify it under the terms of the GNU General
Public License (GPL); either version 2, or (at your option) any
later version.
*/
// Completely disable the client if NSS is not available.
#include "config.h"
#if HAVE_NSS
#include "session.h"
#include "cscommon.h"
#include "csclient.h"
#include "util.h"
#include "stap-probe.h"
#include <sys/times.h>
#include <vector>
#include <fstream>
#include <sstream>
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
extern "C" {
#include <unistd.h>
#include <linux/limits.h>
#include <sys/time.h>
#include <glob.h>
#include <limits.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <net/if.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <pwd.h>
}
#if HAVE_AVAHI
extern "C" {
#include <avahi-client/client.h>
#include <avahi-client/lookup.h>
#include <avahi-common/simple-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>
#include <avahi-common/timeval.h>
}
#endif // HAVE_AVAHI
extern "C" {
#include <ssl.h>
#include <nspr.h>
#include <nss.h>
#include <certdb.h>
#include <pk11pub.h>
#include <prerror.h>
#include <secerr.h>
#include <sslerr.h>
}
#include "nsscommon.h"
using namespace std;
#define STAP_CSC_01 _("WARNING: The domain name, %s, does not match the DNS name(s) on the server certificate:\n")
#define STAP_CSC_02 _("could not find input file %s\n")
#define STAP_CSC_03 _("could not open input file %s\n")
#define STAP_CSC_04 _("Unable to open output file %s\n")
#define STAP_CSC_05 _("could not write to %s\n")
#define MOK_PUBLIC_CERT_NAME "signing_key.x509"
static PRIPv6Addr ©Address (PRIPv6Addr &PRin6, const in6_addr &in6);
static PRNetAddr ©NetAddr (PRNetAddr &x, const PRNetAddr &y);
bool operator!= (const PRNetAddr &x, const PRNetAddr &y);
bool operator== (const PRNetAddr &x, const PRNetAddr &y);
extern "C"
void
nsscommon_error (const char *msg, int logit __attribute ((unused)))
{
clog << msg << endl << flush;
}
// Information about compile servers.
struct compile_server_info
{
compile_server_info () : port(0), fully_specified(false)
{
memset (& address, 0, sizeof (address));
}
string host_name;
PRNetAddr address;
unsigned short port;
bool fully_specified;
string version;
string sysinfo;
string certinfo;
vector<string> mok_fingerprints;
bool empty () const
{
return this->host_name.empty () && ! this->hasAddress () && certinfo.empty ();
}
bool hasAddress () const
{
return this->address.raw.family != 0;
}
unsigned short setAddressPort (unsigned short port)
{
if (this->address.raw.family == PR_AF_INET)
return this->address.inet.port = htons (port);
if (this->address.raw.family == PR_AF_INET6)
return this->address.ipv6.port = htons (port);
assert (0);
return 0;
}
bool isComplete () const
{
return this->hasAddress () && port != 0;
}
bool operator== (const compile_server_info &that) const
{
// If one item or the other has only a name, and possibly a port specified,
// then allow a match by name and port only. This is so that the user can specify
// names which are returned by avahi, but are not dns resolvable.
// Otherwise, we will ignore the host_name.
if ((! this->hasAddress() && this->version.empty () &&
this->sysinfo.empty () && this->certinfo.empty ()) ||
(! that.hasAddress() && that.version.empty () &&
that.sysinfo.empty () && that.certinfo.empty ()))
{
if (this->host_name != that.host_name)
return false;
}
// Compare the other fields only if they have both been set.
if (this->hasAddress() && that.hasAddress() &&
this->address != that.address)
return false;
if (this->port != 0 && that.port != 0 &&
this->port != that.port)
return false;
if (! this->version.empty () && ! that.version.empty () &&
this->version != that.version)
return false;
if (! this->sysinfo.empty () && ! that.sysinfo.empty () &&
this->sysinfo != that.sysinfo)
return false;
if (! this->certinfo.empty () && ! that.certinfo.empty () &&
this->certinfo != that.certinfo)
return false;
if (! this->mok_fingerprints.empty () && ! that.mok_fingerprints.empty ()
&& this->mok_fingerprints != that.mok_fingerprints)
return false;
return true; // They are equal
}
// Used to sort servers by preference for order of contact. The preferred server is
// "less" than the other one.
bool operator< (const compile_server_info &that) const
{
// Prefer servers with a later (higher) version number.
cs_protocol_version this_version (this->version.c_str ());
cs_protocol_version that_version (that.version.c_str ());
return that_version < this_version;
}
};
ostream &operator<< (ostream &s, const compile_server_info &i);
ostream &operator<< (ostream &s, const vector<compile_server_info> &v);
static void
preferred_order (vector<compile_server_info> &servers)
{
// Sort the given list of servers into the preferred order for contacting.
// Don't bother if there are less than 2 servers in the list.
if (servers.size () < 2)
return;
// Sort the list using compile_server_info::operator<
sort (servers.begin (), servers.end ());
}
struct resolved_host // see also PR16326, PR16342
{
string host_name;
PRNetAddr address;
resolved_host(string chost_name, PRNetAddr caddress):
host_name(chost_name), address(caddress) {}
};
struct compile_server_cache
{
vector<compile_server_info> default_servers;
vector<compile_server_info> specified_servers;
vector<compile_server_info> trusted_servers;
vector<compile_server_info> signing_servers;
vector<compile_server_info> online_servers;
vector<compile_server_info> all_servers;
map<string,vector<resolved_host> > resolved_hosts;
};
// For filtering queries.
enum compile_server_properties {
compile_server_all = 0x1,
compile_server_trusted = 0x2,
compile_server_online = 0x4,
compile_server_compatible = 0x8,
compile_server_signer = 0x10,
compile_server_specified = 0x20
};
// Static functions.
static compile_server_cache* cscache(systemtap_session& s);
static void query_server_status (systemtap_session &s, const string &status_string);
static void get_server_info (systemtap_session &s, int pmask, vector<compile_server_info> &servers);
static void get_all_server_info (systemtap_session &s, vector<compile_server_info> &servers);
static void get_default_server_info (systemtap_session &s, vector<compile_server_info> &servers);
static void get_specified_server_info (systemtap_session &s, vector<compile_server_info> &servers, bool no_default = false);
static void get_or_keep_online_server_info (systemtap_session &s, vector<compile_server_info> &servers, bool keep);
static void get_or_keep_trusted_server_info (systemtap_session &s, vector<compile_server_info> &servers, bool keep);
static void get_or_keep_signing_server_info (systemtap_session &s, vector<compile_server_info> &servers, bool keep);
static void get_or_keep_compatible_server_info (systemtap_session &s, vector<compile_server_info> &servers, bool keep);
static void keep_common_server_info (const compile_server_info &info_to_keep, vector<compile_server_info> &filtered_info);
static void keep_common_server_info (const vector<compile_server_info> &info_to_keep, vector<compile_server_info> &filtered_info);
static void keep_server_info_with_cert_and_port (systemtap_session &s, const compile_server_info &server, vector<compile_server_info> &servers);
static void add_server_info (const compile_server_info &info, vector<compile_server_info>& list);
static void add_server_info (const vector<compile_server_info> &source, vector<compile_server_info> &target);
static void merge_server_info (const compile_server_info &source, compile_server_info &target);
#if 0 // not used right now
static void merge_server_info (const compile_server_info &source, vector<compile_server_info> &target);
static void merge_server_info (const vector<compile_server_info> &source, vector <compile_server_info> &target);
#endif
static void resolve_host (systemtap_session& s, compile_server_info &server, vector<compile_server_info> &servers);
/* Exit error codes */
#define SUCCESS 0
#define GENERAL_ERROR 1
#define CA_CERT_INVALID_ERROR 2
#define SERVER_CERT_EXPIRED_ERROR 3
// -----------------------------------------------------
// NSS related code used by the compile server client
// -----------------------------------------------------
static void add_server_trust (systemtap_session &s, const string &cert_db_path, vector<compile_server_info> &server_list);
static void revoke_server_trust (systemtap_session &s, const string &cert_db_path, const vector<compile_server_info> &server_list);
static void get_server_info_from_db (systemtap_session &s, vector<compile_server_info> &servers, const string &cert_db_path);
static string global_client_cert_db_path () {
return SYSCONFDIR "/systemtap/ssl/client";
}
static string
private_ssl_cert_db_path ()
{
return local_client_cert_db_path ();
}
static string
global_ssl_cert_db_path ()
{
return global_client_cert_db_path ();
}
static string
signing_cert_db_path ()
{
return SYSCONFDIR "/systemtap/staprun";
}
/* Connection state. */
typedef struct connectionState_t
{
const char *hostName;
PRNetAddr addr;
const char *infileName;
const char *outfileName;
const char *trustNewServerMode;
} connectionState_t;
#if 0 /* No client authorization */
static char *
myPasswd(PK11SlotInfo *info, PRBool retry, void *arg)
{
char * passwd = NULL;
if ( (!retry) && arg )
passwd = PORT_Strdup((char *)arg);
return passwd;
}
#endif
/* Add the server's certificate to our database of trusted servers. */
static SECStatus
trustNewServer (CERTCertificate *serverCert)
{
SECStatus secStatus;
CERTCertTrust *trust = NULL;
PK11SlotInfo *slot = NULL;
/* Import the certificate. */
slot = PK11_GetInternalKeySlot();
const char *nickname = server_cert_nickname ();
secStatus = PK11_ImportCert(slot, serverCert, CK_INVALID_HANDLE, nickname, PR_FALSE);
if (secStatus != SECSuccess)
goto done;
/* Make it a trusted peer. */
trust = (CERTCertTrust *)PORT_ZAlloc(sizeof(CERTCertTrust));
if (! trust)
{
secStatus = SECFailure;
goto done;
}
secStatus = CERT_DecodeTrustString(trust, "P,P,P");
if (secStatus != SECSuccess)
goto done;
secStatus = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), serverCert, trust);
done:
if (slot)
PK11_FreeSlot (slot);
if (trust)
PORT_Free(trust);
return secStatus;
}
/* Called when the server certificate verification fails. This gives us
the chance to trust the server anyway and add the certificate to the
local database. */
static SECStatus
badCertHandler(void *arg, PRFileDesc *sslSocket)
{
SECStatus secStatus;
PRErrorCode errorNumber;
CERTCertificate *serverCert = NULL;
SECItem subAltName;
PRArenaPool *tmpArena = NULL;
CERTGeneralName *nameList, *current;
char *expected = NULL;
const connectionState_t *connectionState = (connectionState_t *)arg;
errorNumber = PR_GetError ();
switch (errorNumber)
{
case SSL_ERROR_BAD_CERT_DOMAIN:
/* Since we administer our own client-side databases of trustworthy
certificates, we don't need the domain name(s) on the certificate to
match. If the cert is in our database, then we can trust it.
If we know the expected domain name, then issue a warning but,
in any case, accept the certificate. */
secStatus = SECSuccess;
expected = SSL_RevealURL (sslSocket);
if (expected == NULL || *expected == '\0')
break;
fprintf (stderr, STAP_CSC_01, expected);
/* List the DNS names from the server cert as part of the warning.
First, find the alt-name extension on the certificate. */
subAltName.data = NULL;
serverCert = SSL_PeerCertificate (sslSocket);
secStatus = CERT_FindCertExtension (serverCert,
SEC_OID_X509_SUBJECT_ALT_NAME,
& subAltName);
if (secStatus != SECSuccess || ! subAltName.data)
{
fprintf (stderr, _("Unable to find alt name extension on the server certificate\n"));
secStatus = SECSuccess; /* Not a fatal error */
break;
}
// Now, decode the extension.
tmpArena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (! tmpArena)
{
fprintf (stderr, _("Out of memory\n"));
SECITEM_FreeItem(& subAltName, PR_FALSE);
secStatus = SECSuccess; /* Not a fatal error here */
break;
}
nameList = CERT_DecodeAltNameExtension (tmpArena, & subAltName);
SECITEM_FreeItem(& subAltName, PR_FALSE);
if (! nameList)
{
fprintf (stderr, _("Unable to decode alt name extension on server certificate\n"));
secStatus = SECSuccess; /* Not a fatal error */
break;
}
/* List the DNS names from the server cert as part of the warning.
The names are in a circular list. */
current = nameList;
do
{
/* Make sure this is a DNS name. */
if (current->type == certDNSName)
{
fprintf (stderr, " %.*s\n",
(int)current->name.other.len, current->name.other.data);
}
current = CERT_GetNextGeneralName (current);
}
while (current != nameList);
break;
case SEC_ERROR_CA_CERT_INVALID:
/* The server's certificate is not trusted. Should we trust it? */
secStatus = SECFailure; /* Do not trust by default. */
if (! connectionState->trustNewServerMode)
break;
/* Trust it for this session only? */
if (strcmp (connectionState->trustNewServerMode, "session") == 0)
{
secStatus = SECSuccess;
break;
}
/* Trust it permanently? */
if (strcmp (connectionState->trustNewServerMode, "permanent") == 0)
{
/* The user wants to trust this server. Get the server's certificate so
and add it to our database. */
serverCert = SSL_PeerCertificate (sslSocket);
if (serverCert != NULL)
{
secStatus = trustNewServer (serverCert);
}
}
break;
default:
secStatus = SECFailure; /* Do not trust this server */
break;
}
if (expected)
PORT_Free (expected);
if (tmpArena)
PORT_FreeArena (tmpArena, PR_FALSE);
if (serverCert != NULL)
{
CERT_DestroyCertificate (serverCert);
}
return secStatus;
}
static PRFileDesc *
setupSSLSocket (connectionState_t *connectionState)
{
PRFileDesc *tcpSocket;
PRFileDesc *sslSocket;
PRSocketOptionData socketOption;
PRStatus prStatus;
SECStatus secStatus;
tcpSocket = PR_OpenTCPSocket(connectionState->addr.raw.family);
if (tcpSocket == NULL)
goto loser;
/* Make the socket blocking. */
socketOption.option = PR_SockOpt_Nonblocking;
socketOption.value.non_blocking = PR_FALSE;
prStatus = PR_SetSocketOption(tcpSocket, &socketOption);
if (prStatus != PR_SUCCESS)
goto loser;
/* Import the socket into the SSL layer. */
sslSocket = SSL_ImportFD(NULL, tcpSocket);
if (!sslSocket)
goto loser;
/* Set configuration options. */
secStatus = SSL_OptionSet(sslSocket, SSL_SECURITY, PR_TRUE);
if (secStatus != SECSuccess)
goto loser;
secStatus = SSL_OptionSet(sslSocket, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE);
if (secStatus != SECSuccess)
goto loser;
/* Set SSL callback routines. */
#if 0 /* no client authentication */
secStatus = SSL_GetClientAuthDataHook(sslSocket,
(SSLGetClientAuthData)myGetClientAuthData,
(void *)certNickname);
if (secStatus != SECSuccess)
goto loser;
#endif
#if 0 /* Use the default */
secStatus = SSL_AuthCertificateHook(sslSocket,
(SSLAuthCertificate)myAuthCertificate,
(void *)CERT_GetDefaultCertDB());
if (secStatus != SECSuccess)
goto loser;
#endif
secStatus = SSL_BadCertHook(sslSocket, (SSLBadCertHandler)badCertHandler,
connectionState);
if (secStatus != SECSuccess)
goto loser;
#if 0 /* No handshake callback */
secStatus = SSL_HandshakeCallback(sslSocket, myHandshakeCallback, NULL);
if (secStatus != SECSuccess)
goto loser;
#endif
return sslSocket;
loser:
if (tcpSocket)
PR_Close(tcpSocket);
return NULL;
}
static SECStatus
handle_connection (PRFileDesc *sslSocket, connectionState_t *connectionState)
{
PRInt32 numBytes;
char *readBuffer;
PRFileInfo info;
PRFileDesc *local_file_fd;
PRStatus prStatus;
SECStatus secStatus = SECSuccess;
#define READ_BUFFER_SIZE (60 * 1024)
/* If we don't have both the input and output file names, then we're
contacting this server only in order to establish trust. In this case send
0 as the file size and exit. */
if (! connectionState->infileName || ! connectionState->outfileName)
{
numBytes = htonl ((PRInt32)0);
numBytes = PR_Write (sslSocket, & numBytes, sizeof (numBytes));
if (numBytes < 0)
return SECFailure;
return SECSuccess;
}
/* read and send the data. */
/* Try to open the local file named.
* If successful, then write it to the server
*/
prStatus = PR_GetFileInfo(connectionState->infileName, &info);
if (prStatus != PR_SUCCESS ||
info.type != PR_FILE_FILE ||
info.size < 0)
{
fprintf (stderr, STAP_CSC_02,
connectionState->infileName);
return SECFailure;
}
local_file_fd = PR_Open(connectionState->infileName, PR_RDONLY, 0);
if (local_file_fd == NULL)
{
fprintf (stderr, STAP_CSC_03, connectionState->infileName);
return SECFailure;
}
/* Send the file size first, so the server knows when it has the entire file. */
numBytes = htonl ((PRInt32)info.size);
numBytes = PR_Write(sslSocket, & numBytes, sizeof (numBytes));
if (numBytes < 0)
{
PR_Close(local_file_fd);
return SECFailure;
}
/* Transmit the local file across the socket. */
numBytes = PR_TransmitFile(sslSocket, local_file_fd,
NULL, 0,
PR_TRANSMITFILE_KEEP_OPEN,
PR_INTERVAL_NO_TIMEOUT);
if (numBytes < 0)
{
PR_Close(local_file_fd);
return SECFailure;
}
PR_Close(local_file_fd);
/* read until EOF */
readBuffer = (char *)PORT_Alloc(READ_BUFFER_SIZE);
if (! readBuffer) {
fprintf (stderr, _("Out of memory\n"));
return SECFailure;
}
local_file_fd = PR_Open(connectionState->outfileName, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE,
PR_IRUSR | PR_IWUSR | PR_IRGRP | PR_IWGRP | PR_IROTH);
if (local_file_fd == NULL)
{
fprintf (stderr, STAP_CSC_04, connectionState->outfileName);
return SECFailure;
}
while (PR_TRUE)
{
// No need for PR_Read_Complete here, since we're already managing multiple
// reads to a fixed size buffer.
numBytes = PR_Read (sslSocket, readBuffer, READ_BUFFER_SIZE);
if (numBytes == 0)
break; /* EOF */
if (numBytes < 0)
{
secStatus = SECFailure;
break;
}
/* Write to output file */
numBytes = PR_Write(local_file_fd, readBuffer, numBytes);
if (numBytes < 0)
{
fprintf (stderr, STAP_CSC_05, connectionState->outfileName);
secStatus = SECFailure;
break;
}
}
PR_Free(readBuffer);
PR_Close(local_file_fd);
/* Caller closes the socket. */
return secStatus;
}
/* make the connection.
*/
static SECStatus
do_connect (connectionState_t *connectionState)
{
PRFileDesc *sslSocket;
PRStatus prStatus;
SECStatus secStatus;
secStatus = SECSuccess;
/* Set up SSL secure socket. */
sslSocket = setupSSLSocket (connectionState);
if (sslSocket == NULL)
return SECFailure;
#if 0 /* no client authentication */
secStatus = SSL_SetPKCS11PinArg(sslSocket, password);
if (secStatus != SECSuccess)
goto done;
#endif
secStatus = SSL_SetURL(sslSocket, connectionState->hostName);
if (secStatus != SECSuccess)
goto done;
prStatus = PR_Connect(sslSocket, & connectionState->addr, PR_INTERVAL_NO_TIMEOUT);
if (prStatus != PR_SUCCESS)
{
secStatus = SECFailure;
goto done;
}
/* Established SSL connection, ready to send data. */
secStatus = SSL_ResetHandshake(sslSocket, /* asServer */ PR_FALSE);
if (secStatus != SECSuccess)
goto done;
/* This is normally done automatically on the first I/O operation,
but doing it here catches any authentication problems early. */
secStatus = SSL_ForceHandshake(sslSocket);
if (secStatus != SECSuccess)
goto done;
// Connect to the server and make the request.
secStatus = handle_connection(sslSocket, connectionState);
done:
prStatus = PR_Close(sslSocket);
return secStatus;
}
static bool
isIPv6LinkLocal (const PRNetAddr &address)
{
// Link-local addresses are members of the address block fe80::
if (address.raw.family == PR_AF_INET6 &&
address.ipv6.ip.pr_s6_addr[0] == 0xfe && address.ipv6.ip.pr_s6_addr[1] == 0x80)
return true;
return false;
}
int
client_connect (const compile_server_info &server,
const char* infileName, const char* outfileName,
const char* trustNewServer)
{
SECStatus secStatus;
PRErrorCode errorNumber;
int attempt;
int errCode = GENERAL_ERROR;
struct connectionState_t connectionState;
// Set up a connection state for use by NSS error callbacks.
memset (& connectionState, 0, sizeof (connectionState));
connectionState.hostName = server.host_name.c_str ();
connectionState.addr = server.address;
connectionState.infileName = infileName;
connectionState.outfileName = outfileName;
connectionState.trustNewServerMode = trustNewServer;
/* Some errors (see below) represent a situation in which trying again
should succeed. However, don't try forever. */
for (attempt = 0; attempt < 5; ++attempt)
{
secStatus = do_connect (& connectionState);
if (secStatus == SECSuccess)
return SUCCESS;
errorNumber = PR_GetError ();
switch (errorNumber)
{
case PR_CONNECT_RESET_ERROR:
/* Server was not ready. */
sleep (1);
break; /* Try again */
case SEC_ERROR_EXPIRED_CERTIFICATE:
/* The server's certificate has expired. It should
generate a new certificate. Return now and we'll try again. */
errCode = SERVER_CERT_EXPIRED_ERROR;
return errCode;
case SEC_ERROR_CA_CERT_INVALID:
/* The server's certificate is not trusted. The exit code must
reflect this. */
errCode = CA_CERT_INVALID_ERROR;
return errCode;
default:
/* This error is fatal. */
return errCode;
}
}
return errCode;
}
int
compile_server_client::passes_0_4 ()
{
PROBE1(stap, client__start, &s);
// arguments parsed; get down to business
if (s.verbose || ! s.auto_server_msgs.empty ())
clog << _("Using a compile server.") << endl;
struct tms tms_before;
times (& tms_before);
struct timeval tv_before;
gettimeofday (&tv_before, NULL);
// Create the request package.
int rc = initialize ();
assert_no_interrupts();
if (rc != 0) goto done;
rc = create_request ();
assert_no_interrupts();
if (rc != 0) goto done;
rc = package_request ();
assert_no_interrupts();
if (rc != 0) goto done;
// Submit it to the server.
rc = find_and_connect_to_server ();
assert_no_interrupts();
if (rc != 0) goto done;
// Unpack and process the response.
rc = unpack_response ();
assert_no_interrupts();
if (rc != 0) goto done;
rc = process_response ();
done:
struct tms tms_after;
times (& tms_after);
unsigned _sc_clk_tck = sysconf (_SC_CLK_TCK);
struct timeval tv_after;
gettimeofday (&tv_after, NULL);
#define TIMESPRINT "in " << \
(tms_after.tms_cutime + tms_after.tms_utime \
- tms_before.tms_cutime - tms_before.tms_utime) * 1000 / (_sc_clk_tck) << "usr/" \
<< (tms_after.tms_cstime + tms_after.tms_stime \
- tms_before.tms_cstime - tms_before.tms_stime) * 1000 / (_sc_clk_tck) << "sys/" \
<< ((tv_after.tv_sec - tv_before.tv_sec) * 1000 + \
((long)tv_after.tv_usec - (long)tv_before.tv_usec) / 1000) << "real ms."
if (rc == 0)
{
// Save the module, if necessary.
if (s.last_pass == 4)
s.save_module = true;
// Copy module to the current directory.
if (! pending_interrupts)
{
if (s.save_module)
{
string module_src_path = s.tmpdir + "/" + s.module_filename();
string module_dest_path = s.module_filename();
copy_file (module_src_path, module_dest_path, s.verbose >= 3);
// Also copy the module signature, it it exists.
module_src_path += ".sgn";
if (file_exists (module_src_path))
{
module_dest_path += ".sgn";
copy_file(module_src_path, module_dest_path, s.verbose >= 3);
}
}
// Print the name of the module
if (s.last_pass == 4)
{
cout << s.module_filename() << endl;
}
}
}
// syntax errors, if any, are already printed
if (s.verbose)
{
string ws = s.winning_server;
if (ws == "") ws = "?";
clog << _("Passes: via server ") << ws << " "
<< getmemusage()
<< TIMESPRINT
<< endl;
}
if (rc && !s.dump_mode)
{
clog << _("Passes: via server failed. Try again with another '-v' option.") << endl;
}
PROBE1(stap, client__end, &s);
return rc;
}
// Initialize a client/server session.
int
compile_server_client::initialize ()
{
int rc = 0;
// Initialize session state
argc = 0;
// Private location for server certificates.
private_ssl_dbs.push_back (private_ssl_cert_db_path ());
// Additional public location.
public_ssl_dbs.push_back (global_ssl_cert_db_path ());
// Create a temporary directory to package things in.
client_tmpdir = s.tmpdir + "/client";
rc = create_dir (client_tmpdir.c_str ());
if (rc != 0)
{
const char* e = strerror (errno);
clog << _("ERROR: cannot create temporary directory (\"")
<< client_tmpdir << "\"): " << e
<< endl;
}
return rc;
}
// Create the request package.
int
compile_server_client::create_request ()
{
// Add the current protocol version.
int rc = write_to_file (client_tmpdir + "/version", CURRENT_CS_PROTOCOL_VERSION);
if (rc != 0)
return rc;
// Add the script file or script option
if (s.script_file != "")
{
if (s.script_file == "-")
{
// Copy the script from stdin
string packaged_script_dir = client_tmpdir + "/script";
rc = create_dir (packaged_script_dir.c_str ());
if (rc != 0)
{
const char* e = strerror (errno);
clog << _("ERROR: cannot create temporary directory ")
<< packaged_script_dir << ": " << e
<< endl;
return rc;
}
rc = ! copy_file("/dev/stdin", packaged_script_dir + "/-");
if (rc != 0)
return rc;
// Name the script in the packaged arguments.
rc = add_package_arg ("script/-");
if (rc != 0)
return rc;
}
else
{
// Add the script to our package. This will also name the script
// in the packaged arguments.
rc = include_file_or_directory ("script", s.script_file);
if (rc != 0)
return rc;
}
}
// Add -I paths. Skip the default directory.
if (s.include_arg_start != -1)
{
unsigned limit = s.include_path.size ();
for (unsigned i = s.include_arg_start; i < limit; ++i)
{
rc = add_package_arg ("-I");
if (rc != 0)
return rc;
rc = include_file_or_directory ("tapset", s.include_path[i]);
if (rc != 0)
return rc;
}
}
// Add other options.
rc = add_package_args ();
if (rc != 0)
return rc;
// Add the sysinfo file
string sysinfo = "sysinfo: " + s.kernel_release + " " + s.architecture;
rc = write_to_file (client_tmpdir + "/sysinfo", sysinfo);
if (rc != 0)
return rc;
// Add localization data
rc = add_localization_variables();
// Add the machine owner key (MOK) fingerprints file, if needed.
if (! s.mok_fingerprints.empty())
{
ostringstream fingerprints;
vector<string>::const_iterator it;
for (it = s.mok_fingerprints.begin(); it != s.mok_fingerprints.end();
it++)
fingerprints << *it << endl;
rc = write_to_file(client_tmpdir + "/mok_fingerprints",
fingerprints.str());
if (rc != 0)
return rc;
}
return rc;
}
// Add the arguments specified on the command line to the server request
// package, as appropriate.
int
compile_server_client::add_package_args ()
{
// stap arguments to be passed to the server.
int rc = 0;
unsigned limit = s.server_args.size();
for (unsigned i = 0; i < limit; ++i)
{
rc = add_package_arg (s.server_args[i]);
if (rc != 0)
return rc;
}
// Script arguments.