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
|
/*
* Copyright (c) 1998 Denis Perchine <dyp@perchine.com>
* Copyright (c) 2004 Szombathelyi György <gyurco@freemail.hu>
* Former maintainer: Adriaan de Groot <groot@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
**/
#include "globals.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_CRYPT_H
#include <crypt.h>
#endif
#include <tqfile.h>
#include <kmessagebox.h>
#include "misc.h"
#include "kglobal_.h"
bool backup(const TQString & name)
{
TQString tmp = name + TQString::tqfromLatin1(KU_BACKUP_EXT);
TQFile::remove( tmp );
if (copyFile(TQFile::encodeName(name), TQFile::encodeName(tmp)) == -1)
{
TQString str;
KMessageBox::error( 0, i18n("Can't create backup file for %1").tqarg(name) );
return false;
}
return true;
}
time_t now() {
struct timeval tv;
gettimeofday( &tv, NULL );
return ( tv.tv_sec );
}
#define BLOCK_SIZE 65536
int copyFile(const TQString & from, const TQString & to)
{
TQFile fi;
TQFile fo;
char buf[BLOCK_SIZE];
fi.setName(from);
fo.setName(to);
if (!fi.exists()) {
KMessageBox::error( 0, i18n("File %1 does not exist.").tqarg(from) );
return (-1);
}
if (!fi.open(IO_ReadOnly)) {
KMessageBox::error( 0, i18n("Cannot open file %1 for reading.").tqarg(from) );
return (-1);
}
if (!fo.open(IO_Raw | IO_WriteOnly | IO_Truncate)) {
KMessageBox::error( 0, i18n("Cannot open file %1 for writing.").tqarg(to) );
return (-1);
}
while (!fi.atEnd()) {
int len = fi.readBlock(buf, BLOCK_SIZE);
if (len <= 0)
break;
fo.writeBlock(buf, len);
}
fi.close();
fo.close();
return (0);
}
TQStringList readShells()
{
TQStringList shells;
FILE *f = fopen(SHELL_FILE,"r");
if (f) {
while (!feof(f)) {
char s[200];
fgets(s, 200, f);
if (feof(f))
break;
s[strlen(s)-1]=0;
if ((s[0])&&(s[0]!='#'))
shells.append(TQFile::decodeName(s));
}
fclose(f);
}
return shells;
}
void addShell(const TQString &shell)
{
TQStringList shells = readShells();
if (shells.contains(shell))
return;
FILE *f = fopen(SHELL_FILE,"a");
if (f)
{
fputs(TQFile::encodeName(shell).data(), f);
fputc('\n', f);
}
fclose(f);
}
TQCString genSalt( int len )
{
TQCString salt( len + 1 );
const char * set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPTQRSTUVWXYZ0123456789./";
salt[0] = set[getpid() % strlen(set)];
for( int i = 1; i < len; i++ ) {
salt[i] = set[kapp->random() % strlen(set)];
}
return salt;
}
TQString encryptPass( const TQString &pass, bool md5 )
{
TQCString salt;
char tmp[128];
if ( md5 ) {
salt = "$1$";
salt += genSalt( 8 );
salt += '$';
} else {
salt = genSalt( 2 );
}
strcpy( tmp, crypt( TQFile::encodeName( pass ), salt ) );
return TQString::fromLocal8Bit( tmp );
}
int timeToDays(time_t time)
{
return time < 0 ? -1 : time/(24*60*60);
}
time_t daysToTime(int days)
{
return days*24*60*60;
}
|