-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnss-server-info.cxx
2512 lines (2264 loc) · 70.9 KB
/
nss-server-info.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-2017 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 "client-nss.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;
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);
// Information about compile servers.
compile_server_info::compile_server_info ()
: port(0), fully_specified(false)
{
memset (& address, 0, sizeof (address));
}
bool
compile_server_info::empty () const
{
return (this->host_name.empty () && ! this->hasAddress ()
&& certinfo.empty ());
}
bool
compile_server_info::hasAddress () const
{
return this->address.raw.family != 0;
}
unsigned short
compile_server_info::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
compile_server_info::isComplete () const
{
return this->hasAddress () && port != 0;
}
string
compile_server_info::host_specification () const
{
ostringstream host_spec;
if (!this->isComplete())
return host_spec.str();
if (! this->host_name.empty ())
host_spec << this->host_name;
else if (this->hasAddress())
{
PRStatus prStatus;
switch (this->address.raw.family)
{
case PR_AF_INET:
case PR_AF_INET6:
{
#define MAX_NETADDR_SIZE 46 // from the NSPR API reference.
char buf[MAX_NETADDR_SIZE];
prStatus = PR_NetAddrToString(&this->address, buf, sizeof (buf));
if (prStatus == PR_SUCCESS) {
host_spec << buf;
break;
}
}
break;
default:
// Do nothing.
break;
}
}
if (! host_spec.str().empty())
host_spec << ":" << this->port;
return host_spec.str();
}
bool
compile_server_info::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 compile_server_info::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_default_server_info (systemtap_session &s,
vector<compile_server_info> &servers);
static void
merge_server_info (const compile_server_info &source,
compile_server_info &target);
static void
resolve_host (systemtap_session& s, compile_server_info &server,
vector<compile_server_info> &servers);
// -----------------------------------------------------
// 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);
void
get_server_info_from_db (systemtap_session &s,
vector<compile_server_info> &servers,
const string &cert_db_path);
string
global_client_cert_db_path ()
{
return SYSCONFDIR "/systemtap/ssl/client";
}
static string
private_ssl_cert_db_path ()
{
return local_client_cert_db_path ();
}
string
global_ssl_cert_db_path ()
{
return global_client_cert_db_path ();
}
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;
/* Add the server's certificate to our database of trusted servers. */
SECStatus
nss_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;
}
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;
}
// Issue a status message for when a server's trust is already in place.
static void
trust_already_in_place (
const compile_server_info &server,
const vector<compile_server_info> &server_list,
const string cert_db_path,
bool revoking
)
{
// What level of trust?
string purpose;
if (cert_db_path == signing_cert_db_path ())
purpose = _("as a module signer for all users");
else
{
purpose = _("as an SSL peer");
if (cert_db_path == global_ssl_cert_db_path ())
purpose += _(" for all users");
else
purpose += _(" for the current user");
}
// Issue a message for each server in the list with the same certificate.
unsigned limit = server_list.size ();
for (unsigned i = 0; i < limit; ++i)
{
if (server.certinfo != server_list[i].certinfo)
continue;
clog << server_list[i] << _(" is already ");
if (revoking)
clog << _("untrusted ") << purpose << endl;
else
clog << _("trusted ") << purpose << endl;
}
}
// Add the given servers to the given database of trusted servers.
static void
add_server_trust (
systemtap_session &s,
const string &cert_db_path,
vector<compile_server_info> &server_list
)
{
// Get a list of servers already trusted. This opens the database, so do it
// before we open it for our own purposes.
vector<compile_server_info> already_trusted;
get_server_info_from_db (s, already_trusted, cert_db_path);
// Make sure the given path exists.
if (create_dir (cert_db_path.c_str (), 0755) != 0)
{
clog << _F("Unable to find or create the client certificate database directory %s: ", cert_db_path.c_str());
perror ("");
return;
}
// Must predeclare this because of jumps to cleanup: below.
vector<string> processed_certs;
client_backend *backend = nss_get_client_backend (s);
if (backend->initialize () != 0)
return;
// Make sure NSPR is initialized. Must be done before NSS is initialized
s.NSPR_init ();
// Initialize the NSS libraries -- read/write
SECStatus secStatus = nssInit (cert_db_path.c_str (), 1/*readwrite*/);
if (secStatus != SECSuccess)
{
// Message already issued.
goto cleanup;
}
// Enable all cipher suites.
// SSL_ClearSessionCache is required for the new settings to take effect.
/* Some NSS versions don't do this correctly in NSS_SetDomesticPolicy. */
do {
const PRUint16 *cipher;
for (cipher = SSL_GetImplementedCiphers(); *cipher != 0; ++cipher)
SSL_CipherPolicySet(*cipher, SSL_ALLOWED);
} while (0);
SSL_ClearSessionCache ();
// Iterate over the servers to become trusted. Contact each one and
// add it to the list of trusted servers if it is not already trusted.
// trust_server_info() will issue any error messages.
for (vector<compile_server_info>::iterator server = server_list.begin();
server != server_list.end ();
++server)
{
// Trust is based on certificates. We need only add trust in the
// same certificate once.
//
// RHBZ 1075685: If the new server to be trusted is selected by address + port,
// and there is no avahi assistance available, or the server is not known
// to avahi, then its certificate serial number field will be empty. We
// therefore have no basis for comparing it to the serial numbers on already-trusted
// certificates. In this case, unconditionally contact the new server to obtain
// its certificate.
if (! server->certinfo.empty ())
{
// We need not contact the server if it has already been processed.
if (find (processed_certs.begin (), processed_certs.end (),
server->certinfo) != processed_certs.end ())
continue;
processed_certs.push_back (server->certinfo);
// We need not contact the server if it is already trusted.
if (find (already_trusted.begin (), already_trusted.end (), *server) !=
already_trusted.end ())
{
if (s.verbose >= 2)
trust_already_in_place (*server, server_list, cert_db_path, false/*revoking*/);
continue;
}
}
// At a minimum we need an ip_address along with a port
// number in order to contact an nss server.
if (s.http_servers.empty ())
{
if (! server->hasAddress() || server->port == 0)
continue;
// Set the port within the address.
server->setAddressPort (server->port);
}
int rc = backend->trust_server_info (*server);
if (rc != NSS_SUCCESS)
{
// Notice no space before the '%s'? The compile_server_info
// '<<' operator always outputs a space.
clog << _F("Unable to connect to%s", lex_cast(*server).c_str()) << endl;
nssError ();
// Additional information: if the address is IPv6 and is link-local, then it must
// have a scope_id.
if (isIPv6LinkLocal (server->address) && server->address.ipv6.scope_id == 0)
{
clog << _(" The address is an IPv6 link-local address with no scope specifier.")
<< endl;
}
}
}
cleanup:
// Shutdown NSS.
// SSL_ClearSessionCache is required before shutdown for client applications.
SSL_ClearSessionCache ();
nssCleanup (cert_db_path.c_str ());
// Make sure the database files are readable.
glob_t globbuf;
string filespec = cert_db_path + "/*.db";
if (s.verbose >= 3)
clog << _F("Searching \"%s\"\n", filespec.c_str());
int r = glob (filespec.c_str (), 0, NULL, & globbuf);
if (r != GLOB_NOSPACE && r != GLOB_ABORTED && r != GLOB_NOMATCH)
{
for (unsigned i = 0; i < globbuf.gl_pathc; ++i)
{
string filename = globbuf.gl_pathv[i];
if (s.verbose >= 3)
clog << _(" found ") << filename << endl;
if (chmod (filename.c_str (), 0644) != 0)
{
s.print_warning("Unable to change permissions on " + filename + ": ");
perror ("");
}
}
globfree(& globbuf);
}
}
// Remove the given servers from the given database of trusted servers.
static void
revoke_server_trust (
systemtap_session &s,
const string &cert_db_path,
const vector<compile_server_info> &server_list
)
{
// Make sure the given path exists.
if (! file_exists (cert_db_path))
{
if (s.verbose >= 5)
{
clog << _F("Certificate database '%s' does not exist",
cert_db_path.c_str()) << endl;
for (vector<compile_server_info>::const_iterator server = server_list.begin();
server != server_list.end ();
++server)
trust_already_in_place (*server, server_list, cert_db_path, true/*revoking*/);
}
return;
}
// Must predeclare these because of jumps to cleanup: below.
CERTCertDBHandle *handle;
PRArenaPool *tmpArena = NULL;
CERTCertList *certs = NULL;
CERTCertificate *db_cert;
vector<string> processed_certs;
const char *nickname;
// Make sure NSPR is initialized. Must be done before NSS is initialized
s.NSPR_init ();
// Initialize the NSS libraries -- read/write
SECStatus secStatus = nssInit (cert_db_path.c_str (), 1/*readwrite*/);
if (secStatus != SECSuccess)
{
// Message already issued
goto cleanup;
}
handle = CERT_GetDefaultCertDB();
// A memory pool to work in
tmpArena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (! tmpArena)
{
clog << _("Out of memory:");
nssError ();
goto cleanup;
}
// Iterate over the servers to become untrusted.
nickname = server_cert_nickname ();
for (vector<compile_server_info>::const_iterator server = server_list.begin();
server != server_list.end ();
++server)
{
// If the server's certificate serial number is unknown, then we can't
// match it with one in the database.
if (server->certinfo.empty ())
continue;
// Trust is based on certificates. We need only revoke trust in the same
// certificate once.
if (find (processed_certs.begin (), processed_certs.end (),
server->certinfo) != processed_certs.end ())
continue;
processed_certs.push_back (server->certinfo);
// Search the client-side database of trusted servers.
db_cert = PK11_FindCertFromNickname (nickname, NULL);
if (! db_cert)
{
// No trusted servers. Not an error, but issue a status message.
if (s.verbose >= 2)
trust_already_in_place (*server, server_list, cert_db_path, true/*revoking*/);
continue;
}
// Here, we have one cert with the desired nickname.
// Now, we will attempt to get a list of ALL certs
// with the same subject name as the cert we have. That list
// should contain, at a minimum, the one cert we have already found.
// If the list of certs is empty (NULL), the libraries have failed.
certs = CERT_CreateSubjectCertList (NULL, handle, & db_cert->derSubject,
PR_Now (), PR_FALSE);
CERT_DestroyCertificate (db_cert);
if (! certs)
{
clog << _F("Unable to query certificate database %s: ",
cert_db_path.c_str()) << endl;
PORT_SetError (SEC_ERROR_LIBRARY_FAILURE);
nssError ();
goto cleanup;
}
// Find the certificate matching the one belonging to our server.
CERTCertListNode *node;
for (node = CERT_LIST_HEAD (certs);
! CERT_LIST_END (node, certs);
node = CERT_LIST_NEXT (node))
{
// The certificate we're working with.
db_cert = node->cert;
// Get the serial number.
string serialNumber = get_cert_serial_number (db_cert);
// Does the serial number match that of the current server?
if (serialNumber != server->certinfo)
continue; // goto next certificate
// All is ok! Remove the certificate from the database.
break;
} // Loop over certificates in the database
// Was a certificate matching the server found? */
if (CERT_LIST_END (node, certs))
{
// Not found. Server is already untrusted.
if (s.verbose >= 2)
trust_already_in_place (*server, server_list, cert_db_path, true/*revoking*/);
}
else
{
secStatus = SEC_DeletePermCertificate (db_cert);
if (secStatus != SECSuccess)
{
clog << _F("Unable to remove certificate from %s: ",
cert_db_path.c_str()) << endl;
nssError ();
}
}
CERT_DestroyCertList (certs);
certs = NULL;
} // Loop over servers
cleanup:
assert(!certs);
if (tmpArena)
PORT_FreeArena (tmpArena, PR_FALSE);
nssCleanup (cert_db_path.c_str ());
}
// Obtain information about servers from the certificates in the given database.
void
get_server_info_from_db (
systemtap_session &s,
vector<compile_server_info> &servers,
const string &cert_db_path
)
{
// Make sure the given path exists.
if (! file_exists (cert_db_path))
{
if (s.verbose >= 5)
clog << _F("Certificate database '%s' does not exist.",
cert_db_path.c_str()) << endl;
return;
}
// Make sure NSPR is initialized. Must be done before NSS is initialized
s.NSPR_init ();
// Initialize the NSS libraries -- readonly
SECStatus secStatus = nssInit (cert_db_path.c_str ());
if (secStatus != SECSuccess)
{
// Message already issued.
return;
}
CERTCertList *certs = get_cert_list_from_db (server_cert_nickname ());
if (! certs)
{
if (s.verbose >= 5)
clog << _F("No certificate found in database %s", cert_db_path.c_str ()) << endl;
goto cleanup;
}
for (CERTCertListNode *node = CERT_LIST_HEAD (certs);
! CERT_LIST_END (node, certs);
node = CERT_LIST_NEXT (node))
{
compile_server_info server_info;
// The certificate we're working with.
CERTCertificate *db_cert = node->cert;
if (get_host_name (db_cert, server_info.host_name) == false)
continue;
// Get the serial number.
server_info.certinfo = get_cert_serial_number (db_cert);
// Our results will at a minimum contain this server.
nss_add_server_info (server_info, servers);
// Augment the list by querying all online servers and keeping the ones
// with the same cert serial number.
vector<compile_server_info> online_servers;
nss_get_or_keep_online_server_info (s, online_servers, false/*keep*/);
nss_keep_server_info_with_cert_and_port (s, server_info, online_servers);
nss_add_server_info (online_servers, servers);
}
cleanup:
if (certs)
CERT_DestroyCertList (certs);
nssCleanup (cert_db_path.c_str ());
}
// Utility Functions.
//-----------------------------------------------------------------------
ostream &operator<< (ostream &s, const compile_server_info &i)
{
// Don't print empty information
if (i.empty ())
return s;
s << " host=";
if (! i.host_name.empty ())
s << i.host_name;
else
s << "unknown";
s << " address=";
if (i.hasAddress())
{
PRStatus prStatus;
switch (i.address.raw.family)
{
case PR_AF_INET:
case PR_AF_INET6:
{
#define MAX_NETADDR_SIZE 46 // from the NSPR API reference.
char buf[MAX_NETADDR_SIZE];
prStatus = PR_NetAddrToString(& i.address, buf, sizeof (buf));
if (prStatus == PR_SUCCESS) {
s << buf;
break;
}
}
// Fall through
default:
s << "offline";
break;
}
}
else
s << "offline";
s << " port=";
if (i.port != 0)
s << i.port;
else
s << "unknown";
s << " sysinfo=\"";
if (! i.sysinfo.empty ())
s << i.sysinfo << '"';
else
s << "unknown\"";
s << " version=";
if (! i.version.empty ())
s << i.version;
else
s << "unknown";
s << " certinfo=\"";
if (! i.certinfo.empty ())
s << i.certinfo << '"';
else
s << "unknown\"";
if (! i.mok_fingerprints.empty ())
{
// FIXME: Yikes, this output is ugly. Perhaps the server output
// needs a more structured approach.
s << " mok_fingerprints=\"";
vector<string>::const_iterator it;
for (it = i.mok_fingerprints.begin (); it != i.mok_fingerprints.end ();
it++)
{
if (it != i.mok_fingerprints.begin ())
s << ", ";
s << *it;
}
s << "\"";
}
return s;
}
ostream &operator<< (ostream &s, const vector<compile_server_info> &v)
{
// Indicate an empty list.
if (v.size () == 0 || (v.size () == 1 && v[0].empty()))
s << "No Servers" << endl;
else
{
for (unsigned i = 0; i < v.size(); ++i)
{
// Don't print empty items.
if (! v[i].empty())
s << v[i] << endl;
}
}
return s;
}
PRNetAddr &
copyNetAddr (PRNetAddr &x, const PRNetAddr &y)
{
PRUint32 saveScope = 0;
// For IPv6 addresses, don't overwrite the scope_id of x unless x is uninitialized or it is 0.
if (x.raw.family == PR_AF_INET6)
saveScope = x.ipv6.scope_id;
x = y;
if (saveScope != 0)
x.ipv6.scope_id = saveScope;
return x;
}
bool
operator== (const PRNetAddr &x, const PRNetAddr &y)
{
// Same address family?
if (x.raw.family != y.raw.family)
return false;
switch (x.raw.family)
{
case PR_AF_INET6:
// If both scope ids are set, compare them.
if (x.ipv6.scope_id != 0 && y.ipv6.scope_id != 0 && x.ipv6.scope_id != y.ipv6.scope_id)
return false; // not equal
// Scope is not a factor. Compare the address bits
return memcmp (& x.ipv6.ip, & y.ipv6.ip, sizeof(x.ipv6.ip)) == 0;
case PR_AF_INET:
return x.inet.ip == y.inet.ip;
default:
break;
}
return false;
}
bool
operator!= (const PRNetAddr &x, const PRNetAddr &y)
{
return !(x == y);
}
static PRIPv6Addr &
copyAddress (PRIPv6Addr &PRin6, const in6_addr &in6)
{
// The NSPR type is a typedef of struct in6_addr, but C++ won't let us copy it
assert (sizeof (PRin6) == sizeof (in6));
memcpy (& PRin6, & in6, sizeof (PRin6));
return PRin6;
}
// Return the default server specification, used when none is given on the
// command line.
static string
default_server_spec (const systemtap_session &s)
{
// If --privilege=X has been used, where X is not stapdev,
// the default is online,trusted,compatible,signer
// otherwise
// the default is online,trusted,compatible
//
// Having said that,
// 'online' and 'compatible' will only succeed if we have avahi
// 'trusted' and 'signer' will only succeed if we have NSS
//
string working_string = "online,trusted,compatible";
if (! pr_contains (s.privilege, pr_stapdev))
working_string += ",signer";
return working_string;
}
static int
server_spec_to_pmask (const string &server_spec)
{
// Construct a mask of the server properties that have been requested.
// The available properties are:
// trusted - servers which are trusted SSL peers.
// online - online servers.
// compatible - servers which compile for the current kernel release
// and architecture.
// signer - servers which are trusted module signers.
// specified - servers which have been specified using --use-server=XXX.
// If no servers have been specified, then this is
// equivalent to --list-servers=trusted,online,compatible.
// all - all trusted servers, trusted module signers,
// servers currently online and specified servers.
string working_spec = server_spec;
vector<string> properties;
tokenize (working_spec, properties, ",");
int pmask = 0;
unsigned limit = properties.size ();
for (unsigned i = 0; i < limit; ++i)
{
const string &property = properties[i];
// Tolerate (and ignore) empty properties.
if (property.empty ())
continue;
if (property == "all")
{
pmask |= compile_server_all;
}
else if (property == "specified")
{
pmask |= compile_server_specified;
}
else if (property == "trusted")
{
pmask |= compile_server_trusted;
}
else if (property == "online")
{
pmask |= compile_server_online;
}
else if (property == "compatible")
{
pmask |= compile_server_compatible;
}
else if (property == "signer")
{
pmask |= compile_server_signer;
}
else
{
// XXX PR13274 needs-session to use print_warning()
clog << _F("WARNING: unsupported compile server property: %s", property.c_str())
<< endl;
}
}
return pmask;
}
void
nss_client_query_server_status (systemtap_session &s)
{
unsigned limit = s.server_status_strings.size ();
for (unsigned i = 0; i < limit; ++i)
query_server_status (s, s.server_status_strings[i]);
}
static void
query_server_status (systemtap_session &s, const string &status_string)
{
// If this string is empty, then the default is "specified"
string working_string = status_string;
if (working_string.empty ())
working_string = "specified";
// If the query is "specified" and no servers have been specified
// (i.e. '--use-server' and/or '--use-http-server' not used or used
// with no argument), then use the default query.
//
// TODO: This may not be necessary. The underlying queries should handle
// "specified" properly.
if (working_string == "specified"
&& (s.specified_servers.empty ()
|| (s.specified_servers.size () == 1
&& s.specified_servers[0].empty ()))