1 /*
2  *  Copyright 2016 Bjango Pty Ltd. All rights reserved.
3  *  Copyright 2010 William Tisäter. All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *
8  *    1.  Redistributions of source code must retain the above copyright
9  *        notice, this list of conditions and the following disclaimer.
10  *
11  *    2.  Redistributions in binary form must reproduce the above copyright
12  *        notice, this list of conditions and the following disclaimer in the
13  *        documentation and/or other materials provided with the distribution.
14  *
15  *    3.  The name of the copyright holder may not be used to endorse or promote
16  *        products derived from this software without specific prior written
17  *        permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
20  *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  *  DISCLAIMED. IN NO EVENT SHALL WILLIAM TISÄTER BE LIABLE FOR ANY
23  *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 
32 #include <vector>
33 #include <sstream>
34 #include <errno.h>
35 #include <fstream>
36 #include <iostream>
37 #include <string.h>
38 #include <unistd.h>
39 #include <sys/stat.h>
40 
41 #include "Utility.h"
42 #include "Clientset.h"
43 #include <syslog.h>
44 
45 using namespace std;
46 
authenticate(string uuid)47 void ClientSet::authenticate(string uuid)
48 {
49 	authenticatedClients.push_back(uuid);
50 	save_cache();
51 }
52 
is_authenticated(string _duuid)53 int ClientSet::is_authenticated(string _duuid)
54 {
55 	for (vector<string>::const_iterator search = authenticatedClients.begin(); search != authenticatedClients.end(); ++search)
56 	{
57 		if (*search == _duuid)
58 		{
59 			return 1;
60 		}
61 	}
62 
63 	return 0;
64 }
65 
clear_cache(void)66 void ClientSet::clear_cache(void)
67 {
68 	if (this->cache_dir.length())
69 	{
70 		stringstream path;
71 		string cache_file = "clients.dat";
72 		path << this->cache_dir << cache_file;
73 
74 		if (check_file_exist(path.str()))
75 		{
76 			if (unlink(path.str().c_str()) == 0)
77 			{
78 				cout << "Successfully cleared all sessions." << endl;
79 			}
80 			else
81 			{
82 				cout << "Could not clear sessions in '" << path.str() << "': " << strerror(errno) << endl;
83 			}
84 		}
85 	}
86 }
87 
save_cache(void)88 void ClientSet::save_cache(void)
89 {
90 	if (this->cache_dir.length())
91 	{
92 		stringstream path;
93 		string cache_file = "clients.dat";
94 		path << this->cache_dir << cache_file;
95 
96 		ofstream out(path.str().c_str());
97 		chmod(path.str().c_str(), 0600);
98 
99 		if (!out)
100 		{
101 			cout << "Could not create file '" << path.str() << "': " << strerror(errno) << endl;
102 			return;
103 		}
104 		for (vector<string>::const_iterator search = authenticatedClients.begin(); search != authenticatedClients.end(); ++search)
105 		{
106 			string uuid = *search;
107 			out << uuid << endl;
108 		}
109 
110 		out.close();
111 	}
112 }
113 
read_cache(const std::string & _cache_dir)114 void ClientSet::read_cache(const std::string & _cache_dir)
115 {
116 	string line;
117 	stringstream path;
118 	vector<string> array;
119 	string cache_file = "clients.dat";
120 
121 	this->cache_dir = _cache_dir;
122 	path << this->cache_dir << cache_file;
123 
124 	ifstream cache(path.str().c_str());
125 
126 	if (cache.good())
127 	{
128 		while (getline(cache, line))
129 		{
130 			if (line.length())
131 			{
132 				array = explode(line, ":");
133 
134 				if(array.size() > 1){
135 					if (array.size() < 3) continue;
136 
137 					if(to_int(array[2]) == 1)
138 						authenticatedClients.push_back(array[1]);
139 				} else {
140 					authenticatedClients.push_back(line);
141 				}
142 			}
143 		}
144 	}
145 	else
146 	{
147 		// Ignore no such file errors, we will create the file upon save
148 		if (errno != ENOENT)
149 			cout << "Could not read cache file '" << path.str() << "': " << strerror(errno) << endl;
150 	}
151 
152 	cache.close();
153 }
154