2 * Copyright (c) 1998 Denis Perchine <dyp@perchine.com>
3 * Copyright (c) 2004 Szombathelyi György <gyurco@freemail.hu>
4 * Former maintainer: Adriaan de Groot <groot@kde.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License version 2 or at your option version 3 as published by
9 * the Free Software Foundation.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include <ku_config.h>
25 #ifdef HAVE_SYS_STAT_H
40 #include <kmessagebox.h>
47 bool backup(const QString
& name
)
49 QString tmp
= name
+ QString::fromLatin1(KU_BACKUP_EXT
);
52 if (copyFile(QFile::encodeName(name
), QFile::encodeName(tmp
)) == -1)
55 KMessageBox::error( 0, i18n("Can not create backup file for %1", name
) );
64 gettimeofday( &tv
, NULL
);
68 void copyDir(const QString
&srcPath
, const QString
&dstPath
, uid_t uid
, gid_t gid
)
74 QString dot
= QString::fromLatin1(".");
75 QString dotdot
= QString::fromLatin1("..");
77 s
.setFilter( QDir::AllEntries
| QDir::Hidden
| QDir::System
);
79 for (uint i
=0; i
<s
.count(); i
++) {
87 QString
filename(s
.filePath(name
));
89 QFileInfo
info(filename
);
91 if ( info
.permission(QFile::ReadOwner
) ) mode
|= S_IRUSR
;
92 if ( info
.permission(QFile::WriteOwner
) ) mode
|= S_IWUSR
;
93 if ( info
.permission(QFile::ExeOwner
) ) mode
|= S_IXUSR
;
94 if ( info
.permission(QFile::ReadGroup
) ) mode
|= S_IRGRP
;
95 if ( info
.permission(QFile::WriteGroup
) ) mode
|= S_IWGRP
;
96 if ( info
.permission(QFile::ExeGroup
) ) mode
|= S_IXGRP
;
97 if ( info
.permission(QFile::ReadOther
) ) mode
|= S_IROTH
;
98 if ( info
.permission(QFile::WriteOther
) ) mode
|= S_IWOTH
;
99 if ( info
.permission(QFile::ExeOther
) ) mode
|= S_IXOTH
;
101 if ( info
.isSymLink() ) {
102 QString link
= info
.readLink();
104 if (symlink(QFile::encodeName(link
),QFile::encodeName(d
.filePath(name
))) != 0) {
105 KMessageBox::error( 0, i18n("Error creating symlink %1.\nError: %2",
106 d
.filePath(s
[i
]), QString::fromLocal8Bit(strerror(errno
))) );
108 } else if ( info
.isDir() ) {
112 copyDir(s
.filePath(name
), d
.filePath(name
), uid
, gid
);
114 if (chown(QFile::encodeName(d
.filePath(name
)), uid
, gid
) != 0) {
115 KMessageBox::error( 0, i18n("Cannot change owner of folder %1.\nError: %2",
116 d
.filePath(s
[i
]), QString::fromLocal8Bit(strerror(errno
))) );
119 if (chmod(QFile::encodeName(d
.filePath(name
)), mode
) != 0) {
120 KMessageBox::error( 0, i18n("Cannot change permissions on folder %1.\nError: %2",
121 d
.filePath(s
[i
]), QString::fromLocal8Bit(strerror(errno
))) );
125 if (copyFile(filename
, d
.filePath(name
)) == -1) {
129 if (chown(QFile::encodeName(d
.filePath(name
)), uid
, gid
) != 0) {
130 KMessageBox::error( 0, i18n("Cannot change owner of file %1.\nError: %2",
131 d
.filePath(s
[i
]), QString::fromLocal8Bit(strerror(errno
))) );
134 if (chmod(QFile::encodeName(d
.filePath(name
)), mode
) != 0) {
135 KMessageBox::error( 0, i18n("Cannot change permissions on file %1.\nError: %2",
136 d
.filePath(s
[i
]), QString::fromLocal8Bit(strerror(errno
))) );
142 #define BLOCK_SIZE 65536
144 int copyFile(const QString
& from
, const QString
& to
)
148 char buf
[BLOCK_SIZE
];
150 fi
.setFileName(from
);
154 KMessageBox::error( 0, i18n("File %1 does not exist.", from
) );
158 if (!fi
.open(QIODevice::ReadOnly
)) {
159 KMessageBox::error( 0, i18n("Cannot open file %1 for reading.", from
) );
163 if (!fo
.open(QIODevice::WriteOnly
| QIODevice::Truncate
)) {
164 KMessageBox::error( 0, i18n("Cannot open file %1 for writing.", to
) );
168 while (!fi
.atEnd()) {
169 int len
= fi
.read(buf
, BLOCK_SIZE
);
181 QStringList
readShells()
185 FILE *f
= fopen(SHELL_FILE
,"r");
195 if ((s
[0])&&(s
[0]!='#'))
196 shells
.append(QFile::decodeName(s
));
203 void addShell(const QString
&shell
)
205 QStringList shells
= readShells();
206 if (shells
.contains(shell
))
209 FILE *f
= fopen(SHELL_FILE
,"a");
212 fputs(QFile::encodeName(shell
).data(), f
);
218 QByteArray
genSalt( int len
)
220 QByteArray
salt( len
, 0 );
221 const char * set
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
223 salt
[0] = set
[getpid() % strlen(set
)];
224 for( int i
= 1; i
< len
; i
++ ) {
225 salt
[i
] = set
[KRandom::random() % strlen(set
)];
230 QString
encryptPass( const QString
&pass
, bool md5
)
237 salt
+= genSalt( 8 );
243 strcpy( tmp
, crypt( QFile::encodeName( pass
), salt
) );
244 return QString::fromLocal8Bit( tmp
);
247 int timeToDays(time_t time
)
249 return time
< 0 ? -1 : time
/(24*60*60);
252 time_t daysToTime(int days
)
254 return days
*24*60*60;
257 void ku_add2ops( KLDAP::LdapOperation::ModOps
&ops
, const QString
&attr
, const QList
<QByteArray
> &vals
, bool allownull
)
259 KLDAP::LdapOperation::ModOp op
;
260 op
.type
= KLDAP::LdapOperation::Mod_Replace
;
262 for ( int i
= 0; i
< vals
.count(); ++i
) {
263 if ( !vals
[i
].isEmpty() || allownull
) {
264 op
.values
.append( vals
[i
] );
270 void ku_add2ops( KLDAP::LdapOperation::ModOps
&ops
, const QString
&attr
, const QByteArray
&val
, bool allownull
)
272 QList
<QByteArray
> vals
;
273 kDebug() << "add2ops attr: " << attr
<< " value: '" << val
<< "'";
275 ku_add2ops( ops
, attr
, vals
, allownull
);