Branch data Line data Source code
1 : : /*
2 : : Compile-server and client common functions
3 : : Copyright (C) 2011 Red Hat Inc.
4 : :
5 : : This file is part of systemtap, and is free software. You can
6 : : redistribute it and/or modify it under the terms of the GNU General
7 : : Public License (GPL); either version 2, or (at your option) any
8 : : later version.
9 : : */
10 : : #include "config.h"
11 : :
12 : : // Disable the code in this file if NSS is not available
13 : : #if HAVE_NSS
14 : : #include "util.h"
15 : : #include "cscommon.h"
16 : :
17 : : #include <fstream>
18 : : #include <string>
19 : : #include <vector>
20 : : #include <cstdlib>
21 : : #include <cstring>
22 : : #include <cassert>
23 : : #include <iomanip>
24 : :
25 : : extern "C"
26 : : {
27 : : #include <ssl.h>
28 : : }
29 : :
30 : : using namespace std;
31 : :
32 : 339 : cs_protocol_version::~cs_protocol_version ()
33 : : {
34 [ - + ]: 339 : assert (this->v);
35 : 339 : free ((void*)this->v);
36 : 339 : }
37 : :
38 : : const cs_protocol_version &
39 : 409 : cs_protocol_version::operator= (const char *v)
40 : : {
41 [ + + ]: 409 : if (this->v)
42 : 70 : free ((void *)this->v);
43 : 409 : this->v = strdup (v);
44 : 409 : return *this;
45 : : }
46 : :
47 : : bool
48 : 185 : cs_protocol_version::operator< (const cs_protocol_version &that) const
49 : : {
50 : : // Compare the levels of each version in turn.
51 [ + - ]: 185 : vector<string> these_tokens;
52 [ + - ][ + - ]: 185 : tokenize (this->v, these_tokens, ".");
[ + - ][ + - ]
[ + - ]
53 [ + - ]: 185 : vector<string> those_tokens;
54 [ + - ][ + - ]: 185 : tokenize (that.v, those_tokens, ".");
[ + - ][ + - ]
[ + - ]
55 : :
56 : 185 : unsigned this_limit = these_tokens.size ();
57 : 185 : unsigned that_limit = those_tokens.size ();
58 : : unsigned i;
59 [ + + ][ + - ]: 450 : for (i = 0; i < this_limit && i < that_limit; ++i)
[ + + ]
60 : : {
61 : : char *e;
62 [ + - ]: 265 : unsigned long this_level = strtoul (these_tokens[i].c_str (), & e, 0);
63 [ - + ]: 265 : assert (! *e);
64 [ + - ]: 265 : unsigned long that_level = strtoul (those_tokens[i].c_str (), & e, 0);
65 [ - + ]: 265 : assert (! *e);
66 [ + + ]: 265 : if (this_level > that_level)
67 : 105 : return false;
68 [ - + ]: 160 : if (this_level < that_level)
69 : 0 : return true;
70 : : }
71 : :
72 : : // If the other version has more components, then this one is less than that one.
73 [ - + ]: 80 : if (i < that_limit)
74 : : {
75 [ # # ]: 0 : assert (i == this_limit);
76 : 0 : return true;
77 : : }
78 : : // This version is greater than or equal to that one.
79 [ + - ][ + - ]: 185 : return false;
80 : : }
81 : :
82 : : int
83 : 70 : read_from_file (const string &fname, cs_protocol_version &data)
84 : : {
85 : : // C++ streams may not set errno in the even of a failure. However if we
86 : : // set it to 0 before each operation and it gets set during the operation,
87 : : // then we can use its value in order to determine what happened.
88 [ + - ]: 70 : string dataStr;
89 : 70 : errno = 0;
90 [ + - ][ + - ]: 70 : ifstream f (fname.c_str ());
91 [ + - ][ - + ]: 70 : if (! f.good ())
92 : : {
93 [ # # ][ # # ]: 0 : clog << _F("Unable to open file '%s' for reading: ", fname.c_str());
[ # # ][ # # ]
94 : 0 : goto error;
95 : : }
96 : :
97 : : // Read the data;
98 : 70 : errno = 0;
99 [ + - ]: 70 : f >> dataStr;
100 [ + - ][ - + ]: 70 : if (f.fail ())
101 : : {
102 [ # # ][ # # ]: 0 : clog << _F("Unable to read from file '%s': ", fname.c_str());
[ # # ][ # # ]
103 : 0 : goto error;
104 : : }
105 : :
106 [ + - ]: 70 : data = dataStr.c_str ();
107 : :
108 : : // NB: not necessary to f.close ();
109 : 70 : return 0; // Success
110 : :
111 : : error:
112 [ # # ]: 0 : if (errno)
113 [ # # ][ # # ]: 0 : clog << strerror (errno) << endl;
114 : : else
115 [ # # ][ # # ]: 0 : clog << _("unknown error") << endl;
116 [ + - ][ + - ]: 70 : return 1; // Failure
117 : : }
118 : :
119 : 53 : string get_cert_serial_number (const CERTCertificate *cert)
120 : : {
121 [ + - ]: 53 : ostringstream serialNumber;
122 [ + - ][ + - ]: 53 : serialNumber << hex << setfill('0') << right;
[ + - ]
123 [ + + ]: 318 : for (unsigned i = 0; i < cert->serialNumber.len; ++i)
124 : : {
125 [ + + ]: 265 : if (i > 0)
126 [ + - ]: 212 : serialNumber << ':';
127 [ + - ][ + - ]: 265 : serialNumber << setw(2) << (unsigned)cert->serialNumber.data[i];
128 : : }
129 [ + - ][ + - ]: 53 : return serialNumber.str ();
130 [ + - ][ + - ]: 7257 : }
131 : : #endif /* HAVE_NSS */
|