Proof-reading - fixed one usage of the i18n plural form (it wasn't doing before,...
[kdeadmin.git] / kuser / ku_misc.cpp
blob7cdefd70155cd0eb1474a0c5f90dd7ef9687382e
1 /*
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.
20 **/
22 #include "globals.h"
23 #include <ku_config.h>
25 #ifdef HAVE_SYS_STAT_H
26 #include <sys/stat.h>
27 #endif
28 #include <errno.h>
29 #include <stdlib.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #ifdef HAVE_CRYPT_H
34 #include <crypt.h>
35 #endif
37 #include <QFile>
38 #include <QDir>
40 #include <kmessagebox.h>
41 #include <krandom.h>
42 #include <kdebug.h>
43 #include <klocale.h>
45 #include "ku_misc.h"
47 bool backup(const QString & name)
49 QString tmp = name + QString::fromLatin1(KU_BACKUP_EXT);
50 QFile::remove( tmp );
52 if (copyFile(QFile::encodeName(name), QFile::encodeName(tmp)) == -1)
54 QString str;
55 KMessageBox::error( 0, i18n("Can not create backup file for %1", name) );
56 return false;
58 return true;
61 time_t now() {
62 struct timeval tv;
64 gettimeofday( &tv, NULL );
65 return ( tv.tv_sec );
68 void copyDir(const QString &srcPath, const QString &dstPath, uid_t uid, gid_t gid)
70 mode_t mode;
71 QDir s(srcPath);
72 QDir d(dstPath);
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++) {
80 QString name(s[i]);
82 if (name == dot)
83 continue;
84 if (name == dotdot)
85 continue;
87 QString filename(s.filePath(name));
89 QFileInfo info(filename);
90 mode = 0;
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() ) {
109 QDir dir(filename);
111 d.mkdir(name);
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))) );
124 } else {
125 if (copyFile(filename, d.filePath(name)) == -1) {
126 continue;
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)
146 QFile fi;
147 QFile fo;
148 char buf[BLOCK_SIZE];
150 fi.setFileName(from);
151 fo.setFileName(to);
153 if (!fi.exists()) {
154 KMessageBox::error( 0, i18n("File %1 does not exist.", from) );
155 return (-1);
158 if (!fi.open(QIODevice::ReadOnly)) {
159 KMessageBox::error( 0, i18n("Cannot open file %1 for reading.", from) );
160 return (-1);
163 if (!fo.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
164 KMessageBox::error( 0, i18n("Cannot open file %1 for writing.", to) );
165 return (-1);
168 while (!fi.atEnd()) {
169 int len = fi.read(buf, BLOCK_SIZE);
170 if (len <= 0)
171 break;
172 fo.write(buf, len);
175 fi.close();
176 fo.close();
178 return (0);
181 QStringList readShells()
183 QStringList shells;
185 FILE *f = fopen(SHELL_FILE,"r");
186 if (f) {
187 while (!feof(f)) {
188 char s[200];
190 fgets(s, 200, f);
191 if (feof(f))
192 break;
194 s[strlen(s)-1]=0;
195 if ((s[0])&&(s[0]!='#'))
196 shells.append(QFile::decodeName(s));
198 fclose(f);
200 return shells;
203 void addShell(const QString &shell)
205 QStringList shells = readShells();
206 if (shells.contains(shell))
207 return;
209 FILE *f = fopen(SHELL_FILE,"a");
210 if (f)
212 fputs(QFile::encodeName(shell).data(), f);
213 fputc('\n', f);
215 fclose(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)];
227 return salt;
230 QString encryptPass( const QString &pass, bool md5 )
232 QByteArray salt;
233 char tmp[128];
235 if ( md5 ) {
236 salt = "$1$";
237 salt += genSalt( 8 );
238 salt += '$';
240 } else {
241 salt = genSalt( 2 );
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;
261 op.attr = attr;
262 for ( int i = 0; i < vals.count(); ++i ) {
263 if ( !vals[i].isEmpty() || allownull ) {
264 op.values.append( vals[i] );
267 ops.append( op );
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 << "'";
274 vals.append( val );
275 ku_add2ops( ops, attr, vals, allownull );