summaryrefslogtreecommitdiffstats
path: root/src/util/util.cpp
blob: 0fcfa849c3005a3ff908c488dd9868da31c4f011 (plain) (blame)
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
#include "util.h"
#include <QtGui/QDesktopServices>
#include "consoleLogger.h"
#include "TextFile.h"
#include <ctime>
#include <cstdlib>
#include <QStringList>
#include <iostream>
#include <QDir>

PVSServerEntry::PVSServerEntry(QString name)
{
    name_ = name;
}
void PVSServerEntry::addClient(QString client)
{
    clientList_.push_back(client);
}
bool PVSServerEntry::hasClient(QString client)
{
    for (std::list<QString>::iterator it = clientList_.begin(); it != clientList_.end(); it++)
    {
        if ((*it).compare(client) == 0)
            return true;
    }
    return false;
}
QString PVSServerEntry::getName()
{
    return name_;
}

int PVSServerEntry::getSize()
{
    return clientList_.size();
}


//namespace util
//{

int getRandom(int min, int max)
{
    static bool init = true;
    if (init)
    {
        init = false;
        srand ( time(NULL) + getpid() );
    }
    if (min >= max) return rand();
    return rand() % (max-min+1) + min;
}

std::list<PVSServerEntry> getPVSServerEntryList(QString fileName)
{
    QString line;
    TextFile file(fileName);
    std::list<PVSServerEntry> entryList;

    if (file.good())
    {
//		std::cout << "reading table file \"" << fileName <<"\"" << std::endl;
        PVSServerEntry* tmpEntry = NULL;
        while (!file.eof())
        {
            line = file.readLine();

            if (!(line.length() <=1))	// ignore blank
                if (!(line[0] == '#' || line[0] == '/')) // ignore comments
                {
                    if (line.mid(0, 5).compare(QString("start")) == 0)
                    {
                        if (tmpEntry != NULL)
                        {
                            // unclean file... but no reason to break down
                            //PVSServerEntry tmpEntry2 = *tmpEntry;
                            entryList.push_back(*tmpEntry);
                            delete tmpEntry;
                            tmpEntry = NULL;
                        }
                        QString tmpName(colonSplitter(line, false));
                        if (tmpName.size()<1)
                            tmpName = QString("localhost");

                        tmpEntry = new PVSServerEntry(tmpName);
                        continue;
                    }
                    if (line.mid(0, 3).compare(QString("end")) == 0)
                    {
                        if (tmpEntry!=NULL)
                        {
                            entryList.push_back(*tmpEntry);
                            delete tmpEntry;
                            tmpEntry = NULL;
                        }
                        else
                            ;// ignore

                        continue;
                    }
                    if (tmpEntry != NULL)
                    {
                        tmpEntry->addClient(line);
                    }
                }
        }
        if (tmpEntry != NULL)
        {
            delete tmpEntry;
            tmpEntry = NULL;
        }

        return entryList;
    }
    else
    {
        std::cout << "ERROR: no file \"" << fileName.toStdString() <<"\" found or file corrupt" << std::endl;
    }
    return std::list<PVSServerEntry>();
}
QString getFilenameFromPath(QString line)
{
    int lastSlash = 0;
    for (int i = 0; i < line.length(); i++)
    {
        if (line.at(i) == '/')
        {
            lastSlash = i;
        }
    }
    QString result;
    if (lastSlash != 0 && lastSlash != line.length() -1)
    {
        result = line.mid(lastSlash+1, line.length()-(lastSlash+1));
    }
    return result;
}

QString lineSplitter(QString line, const char* signs, bool first)
{
    char *pch;
    char* cLine = new char[line.length()+1];
    strcpy(cLine, line.toUtf8().data());
    pch = strtok (cLine,signs);
    if (first)
    {
        QString tmp(pch);
        delete cLine;
        return tmp;
    }
    else
    {
        QString tmp;
        char* tmpp = strtok(NULL, ";,\t\n");
        if (tmpp)
            tmp = QString(tmpp);
        delete cLine;
        return tmp;
    }
}

QString colonSplitter(QString line, bool first)
{
    char *pch;
    char* cLine = new char[line.length()+1];
    strcpy(cLine, line.toUtf8().data());
    pch = strtok (cLine," :\t\n");
    if (first)
    {
        QString tmp(pch);
        delete[] cLine;
        return tmp;
    }
    else
    {
        QString tmp;
        char* tmpp = strtok(NULL, " ;,\t\n");
        if (tmpp)
            tmp = QString(tmpp);
        delete[] cLine;
        return tmp;
    }
}

QString getUserName()
{
    QString username;
    struct passwd* passUser = getpwuid(getuid());
    if (passUser)
        username = QString(passUser->pw_name);

    if (username.isEmpty())
    {
        qDebug("USERNAME COULDNT BE RETRIEVED!");
        username = QString("USERNAMEERROR");
    }
    return username;
}

// Get full username.
QString getFullUsername()
{
    QString fullname = getUserName();
    struct passwd *pd;

    if (NULL == (pd = getpwuid(getuid())))
    {ConsoleLog writeError("getpwuid() error.");}
    else
    {
        QString tmp = pd->pw_gecos;
        QStringList userData = tmp.split(",");
        if(userData[0].length() > 0 )
        {
            fullname = userData[0];
        }
    }
    return fullname;
}

bool fileExists(QString fileName)
{
    std::ifstream file(fileName.toLocal8Bit().data());
    if (file.good())
    {
#ifdef verbose
        printf("fileExists(): file good.\n");
#endif
        file.close();
        return true;
    }
    return false;

}

QString getHomeDir()
{
    return QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
}

QString getPolicyDir()
{
    QString policyDir = getHomeDir();
    QString subPath("/.pvs/");
    policyDir.append(subPath);
    return policyDir;
}

QString getPolicyFilePath(QString fileName)
{

    QString fullPath = getPolicyDir();
    fullPath.append(fileName);
    return fullPath;
}
bool policyFileExists(QString fileName)
{
    std::ifstream file(getPolicyFilePath(fileName).toUtf8().data());
    if (file.good())
    {
        file.close();
        return true;
    }
    return false;
}

void createPolicyDir()
{
    QDir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation)).mkdir(".pvs");
	//assuming PolicyDir is defined like in getPolicyDir()
}

void createPolicyFiles()
{
    if (!policyFileExists(QString(".allow")))
    {
        std::ofstream file(getPolicyFilePath(QString(".allow")).toUtf8().data());
        file.close();
    }
#ifdef old_method
    if (!policyFileExists(QString(".pass")))
    {
        std::ofstream file(getPolicyFilePath(QString(".pass")).toUtf8().data());
        file.close();
    }
#endif
}

QString readPassFromPassFile()
{
    TextFile file(getPolicyFilePath(".pass"));
    if (file.good()) // should have been checked via exists before, but better be safe
    {
        QString pass;
        pass = file.readLine(); // we expect a password in correct format. otherwise their fault
        return pass;
    }
    else
        return QString();
}
bool getAllowed()
{
    qDebug("Checking %s", qPrintable(getPolicyFilePath(QString(".allow"))));
    TextFile file(getPolicyFilePath(".allow"));
    if (file.good()) // should have been checked via exists before, but better be safe
    {
        QString allowed;
        allowed = file.readLine();
        if ( (allowed.compare(QString("1")) == 0) ||
                (allowed.compare(QString("true")) == 0) ||
                (allowed.compare(QString("t")) == 0) ||
                (allowed.compare(QString("T")) == 0) ||
                (allowed.compare(QString("true")) == 0) ||
                (allowed.compare(QString("allow")) == 0) ||
                (allowed.compare(QString("TRUE")) == 0) )
            return true;
    }
    qDebug("...negative");
    return false;
}

QString int2String(int intInt)
{
    char tmp[50];
    snprintf(tmp, 49, "%d", intInt);
    return QString(tmp);
}

QString ptr2String(void* ptr)
{
	char tmp[50];
	snprintf(tmp, 49, "%p", ptr);
	return QString(tmp);
}

int string2Int(QString string)
{
    return atoi(string.toUtf8().data());
}

//}//end namespace util


#ifdef neverever
std::list<VNCConnectInfo*> readFile(char* fileName)
{
    QString line;
    std::ifstream file(fileName);
    std::list<VNCConnectInfo*> infoList;

    if (file.good())
    {
//		std::cout << "reading clients file \"" << fileName <<"\"" << std::endl;
        while (!file.eof())
        {
            getline(file, line);

            if (!(line.length() <=1))	// ignore blank
                if (!(line[0] == '#' || line[0] == '/')) // ignore comments
                    infoList.push_back(getConInfo(line));
        }
    }
    else
    {
        std::cout << "ERROR: no file \"" << fileName <<"\" found or file corrupt" << std::endl;
    }
    return 	infoList;
}

VNCConnectInfo* getConInfo(QString line)
{
    char** arguments = new char*[100];
    char* cLine = new char[line.length()+1];
    strcpy(cLine, line.toUtf8().data());
    int count;
    makeArgs(cLine , &count , arguments);
    std::cout << "found " << count << " arguments" << std::endl;
    if (count > 2)	// assume that a password would be the second argument in the line, therefore the third argument in the argv
    {
        QString pass = QString(arguments[2]);
        std::cout << "think i found a password" << std::endl;
        count--;
        delete arguments[2];
        // no changes to the arguments though, since the vnc-lib will ignore most of it anyway
        return new VNCConnectInfo(count, arguments, pass);
    }

    return new VNCConnectInfo(count, arguments);
}

void makeArgs(char* line, int* count, char** arguments)
{
    (*count) = 1;

    arguments[(*count)-1] = new char[strlen("dummy")+1];	// get program name from somewhere
    strcpy(arguments[(*count)-1], "dummy");

    /*// include encodings
    arguments[(*count)++] = new char[strlen("-encodings")+1];
    	strcpy(arguments[(*count)], "-encodings");
    	(*count)++;
    arguments[(*count)++] = new char[strlen("tight")+1];
    	strcpy(arguments[(*count)], "tight");
    	(*count)++;
    	*/


    if (line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r')
        line[strlen(line)-1] = '\0'; 		// remove newline

    char *pch;
    pch = strtok (line," ,\t");
    while (pch != NULL && (*count) <= 3)
    {
        (*count)++;	// count args
        arguments[(*count)-1] = new char[strlen(pch)+1];
        strcpy(arguments[(*count)-1], pch);
        pch = strtok (NULL, " ,\t");
    }
}


#endif