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>
26 #include <kstandarddirs.h>
28 #include <kdirwatch.h>
33 #include <kconfiggroup.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"));
53 path
+= QLatin1String(":/usr/sbin");
55 QString exe
= KStandardDirs::findExe( exeName
, path
);
57 kError() << exeName
<< "not found in" << path
;
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
)
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
;
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
;
122 s_authorization
= Authorized
;
124 // check if current user is in fileshare group
125 KUserGroup
shareGroup(s_fileShareGroup
);
126 if (shareGroup
.users().contains(KUser()) )
127 s_authorization
= Authorized
;
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
)
146 bool KFileShare::sharingEnabled() {
147 if ( s_authorization
== NotInitialized
)
150 return s_sharingEnabled
;
153 bool KFileShare::isRestricted() {
154 if ( s_authorization
== NotInitialized
)
160 QString
KFileShare::fileShareGroup() {
161 if ( s_authorization
== NotInitialized
)
164 return s_fileShareGroup
;
168 bool KFileShare::sambaEnabled() {
169 if ( s_authorization
== NotInitialized
)
172 return s_sambaEnabled
;
175 bool KFileShare::nfsEnabled() {
176 if ( s_authorization
== NotInitialized
)
183 void KFileShare::readShareList()
185 KFileSharePrivate::self();
186 s_shareList
->clear();
188 QString exe
= ::findExe( "filesharelist" );
190 s_authorization
= ErrorNotFound
;
194 proc
.start( exe
, QStringList() );
195 if ( !proc
.waitForFinished() ) {
196 kError() << "Can't run" << exe
;
197 s_authorization
= ErrorNotFound
;
201 // Reading code shamelessly stolen from khostname.cpp ;)
202 while (!proc
.atEnd()) {
203 QString line
= proc
.readLine().trimmed();
204 int length
= line
.length();
207 if ( line
[length
-1] != '/' )
209 s_shareList
->append(line
);
210 kDebug(7000) << "Shared dir:" << line
;
216 bool KFileShare::isDirectoryShared( const QString
& _path
)
221 QString
path( _path
);
222 if ( path
[path
.length()-1] != '/' )
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
)
232 return s_authorization
;
235 bool KFileShare::setShared( const QString
& path
, bool shared
)
237 if (! KFileShare::sharingEnabled() ||
238 KFileShare::shareMode() == Advanced
)
241 kDebug(7000) << path
<< "," << shared
;
242 QString exe
= ::findExe( "fileshareset" );
252 int ec
= QProcess::execute( exe
, args
); // should be ok, the perl script terminates fast
253 kDebug(7000) << "exitCode=" << ec
;
257 // User is not authorized
260 // Called script with --add, but path was already shared before.
261 // Result is nevertheless what the client wanted, so
266 // Invalid mount point
269 // Called script with --remove, but path was not shared before.
270 // Result is nevertheless what the client wanted, so
275 // There is no export method
278 // file sharing is disabled
281 // advanced sharing is enabled
291 //#include "kfileshare.moc"
292 #include "kfileshare_p.moc"