fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kio / kio / kfileshare.cpp
blob4de8b3dc193f5a0393efd0861408424b0b790241
1 /* This file is part of the KDE project
2 Copyright (c) 2001 David Faure <faure@kde.org>
3 Copyright (c) 2001 Laurent Montel <lmontel@mandrakesoft.com>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "kfileshare.h"
21 #include "kfileshare_p.h"
22 #include <QtCore/QDir>
23 #include <QtCore/QFile>
24 #include <QtCore/Q_PID>
25 #include <klocale.h>
26 #include <kstandarddirs.h>
27 #include <kdebug.h>
28 #include <kdirwatch.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <kconfig.h>
33 #include <kconfiggroup.h>
34 #include <kuser.h>
36 static KFileShare::Authorization s_authorization = KFileShare::NotInitialized;
37 K_GLOBAL_STATIC(QStringList, s_shareList)
38 static KFileShare::ShareMode s_shareMode;
39 static bool s_sambaEnabled;
40 static bool s_nfsEnabled;
41 static bool s_restricted;
42 static QString s_fileShareGroup;
43 static bool s_sharingEnabled;
45 #define FILESHARECONF "/etc/security/fileshare.conf"
47 static QString findExe( const char* exeName )
49 // Normally fileshareset and filesharelist are installed in kde4/libexec;
50 // allow distributions to move it somewhere else in the PATH or in /usr/sbin.
51 QString path = QString::fromLocal8Bit(qgetenv("PATH"));
52 #ifndef Q_WS_WIN
53 path += QLatin1String(":/usr/sbin");
54 #endif
55 QString exe = KStandardDirs::findExe( exeName, path );
56 if (exe.isEmpty())
57 kError() << exeName << "not found in" << path;
58 return exe;
61 KFileSharePrivate::KFileSharePrivate()
63 KDirWatch::self()->addFile(FILESHARECONF);
64 connect(KDirWatch::self(), SIGNAL(dirty (const QString&)),this,
65 SLOT(slotFileChange(const QString &)));
66 connect(KDirWatch::self(), SIGNAL(created(const QString&)),this,
67 SLOT(slotFileChange(const QString &)));
68 connect(KDirWatch::self(), SIGNAL(deleted(const QString&)),this,
69 SLOT(slotFileChange(const QString &)));
72 KFileSharePrivate::~KFileSharePrivate()
74 KDirWatch::self()->removeFile(FILESHARECONF);
77 KFileSharePrivate* KFileSharePrivate::self()
79 K_GLOBAL_STATIC(KFileSharePrivate, _self)
80 return _self;
83 void KFileSharePrivate::slotFileChange(const QString &file)
85 if(file==FILESHARECONF) {
86 KFileShare::readConfig();
87 KFileShare::readShareList();
91 KFileShare::ShareMode readEntry(const KConfigGroup &cg, const char* key,
92 const KFileShare::ShareMode& aDefault)
94 const QByteArray data=cg.readEntry(key, QByteArray());
96 if (!data.isEmpty()) {
97 if (data.toLower() == "simple")
98 return KFileShare::Simple;
99 else if (data.toLower() == "advanced")
100 return KFileShare::Advanced;
103 return aDefault;
106 void KFileShare::readConfig() // static
108 // Create KFileSharePrivate instance
109 KFileSharePrivate::self();
110 KConfig config(QLatin1String(FILESHARECONF));
111 KConfigGroup group( &config, QString() );
113 s_sharingEnabled = group.readEntry("FILESHARING", true);
114 s_restricted = group.readEntry("RESTRICT", true);
115 s_fileShareGroup = group.readEntry("FILESHAREGROUP", "fileshare");
118 if (!s_sharingEnabled)
119 s_authorization = UserNotAllowed;
120 else
121 if (!s_restricted )
122 s_authorization = Authorized;
123 else {
124 // check if current user is in fileshare group
125 KUserGroup shareGroup(s_fileShareGroup);
126 if (shareGroup.users().contains(KUser()) )
127 s_authorization = Authorized;
128 else
129 s_authorization = UserNotAllowed;
132 s_shareMode = readEntry(group, "SHARINGMODE", Simple);
135 s_sambaEnabled = group.readEntry("SAMBA", true);
136 s_nfsEnabled = group.readEntry("NFS", true);
139 KFileShare::ShareMode KFileShare::shareMode() {
140 if ( s_authorization == NotInitialized )
141 readConfig();
143 return s_shareMode;
146 bool KFileShare::sharingEnabled() {
147 if ( s_authorization == NotInitialized )
148 readConfig();
150 return s_sharingEnabled;
153 bool KFileShare::isRestricted() {
154 if ( s_authorization == NotInitialized )
155 readConfig();
157 return s_restricted;
160 QString KFileShare::fileShareGroup() {
161 if ( s_authorization == NotInitialized )
162 readConfig();
164 return s_fileShareGroup;
168 bool KFileShare::sambaEnabled() {
169 if ( s_authorization == NotInitialized )
170 readConfig();
172 return s_sambaEnabled;
175 bool KFileShare::nfsEnabled() {
176 if ( s_authorization == NotInitialized )
177 readConfig();
179 return s_nfsEnabled;
183 void KFileShare::readShareList()
185 KFileSharePrivate::self();
186 s_shareList->clear();
188 QString exe = ::findExe( "filesharelist" );
189 if (exe.isEmpty()) {
190 s_authorization = ErrorNotFound;
191 return;
193 QProcess proc;
194 proc.start( exe, QStringList() );
195 if ( !proc.waitForFinished() ) {
196 kError() << "Can't run" << exe;
197 s_authorization = ErrorNotFound;
198 return;
201 // Reading code shamelessly stolen from khostname.cpp ;)
202 while (!proc.atEnd()) {
203 QString line = proc.readLine().trimmed();
204 int length = line.length();
205 if ( length > 0 )
207 if ( line[length-1] != '/' )
208 line += '/';
209 s_shareList->append(line);
210 kDebug(7000) << "Shared dir:" << line;
216 bool KFileShare::isDirectoryShared( const QString& _path )
218 if ( ! s_shareList )
219 readShareList();
221 QString path( _path );
222 if ( path[path.length()-1] != '/' )
223 path += '/';
224 return s_shareList->contains( path );
227 KFileShare::Authorization KFileShare::authorization()
229 // The app should do this on startup, but if it doesn't, let's do here.
230 if ( s_authorization == NotInitialized )
231 readConfig();
232 return s_authorization;
235 bool KFileShare::setShared( const QString& path, bool shared )
237 if (! KFileShare::sharingEnabled() ||
238 KFileShare::shareMode() == Advanced)
239 return false;
241 kDebug(7000) << path << "," << shared;
242 QString exe = ::findExe( "fileshareset" );
243 if (exe.isEmpty())
244 return false;
246 QStringList args;
247 if ( shared )
248 args << "--add";
249 else
250 args << "--remove";
251 args << path ;
252 int ec = QProcess::execute( exe, args ); // should be ok, the perl script terminates fast
253 kDebug(7000) << "exitCode=" << ec;
254 bool ok = !ec;
255 switch (ec) {
256 case 1:
257 // User is not authorized
258 break;
259 case 3:
260 // Called script with --add, but path was already shared before.
261 // Result is nevertheless what the client wanted, so
262 // this is alright.
263 ok = true;
264 break;
265 case 4:
266 // Invalid mount point
267 break;
268 case 5:
269 // Called script with --remove, but path was not shared before.
270 // Result is nevertheless what the client wanted, so
271 // this is alright.
272 ok = true;
273 break;
274 case 6:
275 // There is no export method
276 break;
277 case 7:
278 // file sharing is disabled
279 break;
280 case 8:
281 // advanced sharing is enabled
282 break;
283 case 255:
284 // Abitrary error
285 break;
288 return ok;
291 //#include "kfileshare.moc"
292 #include "kfileshare_p.moc"