• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kio
kfileshare.cpp
Go to the documentation of this file.
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>
4
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.
8
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.
13
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.
18*/
19
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>
35
36static KFileShare::Authorization s_authorization = KFileShare::NotInitialized;
37K_GLOBAL_STATIC(QStringList, s_shareList)
38static KFileShare::ShareMode s_shareMode;
39static bool s_sambaEnabled;
40static bool s_nfsEnabled;
41static bool s_restricted;
42static QString s_fileShareGroup;
43static bool s_sharingEnabled;
44
45#define FILESHARECONF "/etc/security/fileshare.conf"
46
47static QString findExe( const char* exeName )
48{
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;
59}
60
61KFileSharePrivate::KFileSharePrivate()
62{
63 KDirWatch::self()->addFile(FILESHARECONF);
64 connect(KDirWatch::self(), SIGNAL(dirty(QString)),this,
65 SLOT(slotFileChange(QString)));
66 connect(KDirWatch::self(), SIGNAL(created(QString)),this,
67 SLOT(slotFileChange(QString)));
68 connect(KDirWatch::self(), SIGNAL(deleted(QString)),this,
69 SLOT(slotFileChange(QString)));
70}
71
72KFileSharePrivate::~KFileSharePrivate()
73{
74 KDirWatch::self()->removeFile(FILESHARECONF);
75}
76
77KFileSharePrivate* KFileSharePrivate::self()
78{
79 K_GLOBAL_STATIC(KFileSharePrivate, _self)
80 return _self;
81}
82
83void KFileSharePrivate::slotFileChange(const QString &file)
84{
85 if(file==FILESHARECONF) {
86 KFileShare::readConfig();
87 KFileShare::readShareList();
88 }
89}
90
91KFileShare::ShareMode readEntry(const KConfigGroup &cg, const char* key,
92 const KFileShare::ShareMode& aDefault)
93{
94 const QByteArray data=cg.readEntry(key, QByteArray());
95
96 if (!data.isEmpty()) {
97 if (data.toLower() == "simple")
98 return KFileShare::Simple;
99 else if (data.toLower() == "advanced")
100 return KFileShare::Advanced;
101 }
102
103 return aDefault;
104}
105
106void KFileShare::readConfig() // static
107{
108 // Create KFileSharePrivate instance
109 KFileSharePrivate::self();
110 KConfig config(QLatin1String(FILESHARECONF));
111 KConfigGroup group( &config, QString() );
112
113 s_sharingEnabled = group.readEntry("FILESHARING", true);
114 s_restricted = group.readEntry("RESTRICT", true);
115 s_fileShareGroup = group.readEntry("FILESHAREGROUP", "fileshare");
116
117
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;
130 }
131
132 s_shareMode = readEntry(group, "SHARINGMODE", Simple);
133
134
135 s_sambaEnabled = group.readEntry("SAMBA", true);
136 s_nfsEnabled = group.readEntry("NFS", true);
137}
138
139KFileShare::ShareMode KFileShare::shareMode() {
140 if ( s_authorization == NotInitialized )
141 readConfig();
142
143 return s_shareMode;
144}
145
146bool KFileShare::sharingEnabled() {
147 if ( s_authorization == NotInitialized )
148 readConfig();
149
150 return s_sharingEnabled;
151}
152
153bool KFileShare::isRestricted() {
154 if ( s_authorization == NotInitialized )
155 readConfig();
156
157 return s_restricted;
158}
159
160QString KFileShare::fileShareGroup() {
161 if ( s_authorization == NotInitialized )
162 readConfig();
163
164 return s_fileShareGroup;
165}
166
167
168bool KFileShare::sambaEnabled() {
169 if ( s_authorization == NotInitialized )
170 readConfig();
171
172 return s_sambaEnabled;
173}
174
175bool KFileShare::nfsEnabled() {
176 if ( s_authorization == NotInitialized )
177 readConfig();
178
179 return s_nfsEnabled;
180}
181
182
183void KFileShare::readShareList()
184{
185 KFileSharePrivate::self();
186 s_shareList->clear();
187
188 QString exe = ::findExe( "filesharelist" );
189 if (exe.isEmpty()) {
190 s_authorization = ErrorNotFound;
191 return;
192 }
193 QProcess proc;
194 proc.start( exe, QStringList() );
195 if ( !proc.waitForFinished() ) {
196 kError() << "Can't run" << exe;
197 s_authorization = ErrorNotFound;
198 return;
199 }
200
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 )
206 {
207 if ( line[length-1] != '/' )
208 line += '/';
209 s_shareList->append(line);
210 kDebug(7000) << "Shared dir:" << line;
211 }
212 }
213}
214
215
216bool KFileShare::isDirectoryShared( const QString& _path )
217{
218 if ( ! s_shareList.exists() )
219 readShareList();
220
221 QString path( _path );
222 if ( path[path.length()-1] != '/' )
223 path += '/';
224 return s_shareList->contains( path );
225}
226
227KFileShare::Authorization KFileShare::authorization()
228{
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;
233}
234
235bool KFileShare::setShared( const QString& path, bool shared )
236{
237 if (! KFileShare::sharingEnabled() ||
238 KFileShare::shareMode() == Advanced)
239 return false;
240
241 kDebug(7000) << path << "," << shared;
242 QString exe = ::findExe( "fileshareset" );
243 if (exe.isEmpty())
244 return false;
245
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;
286 }
287
288 return ok;
289}
290
291//#include "kfileshare.moc"
292#include "kfileshare_p.moc"
293
KConfigGroup
KConfigGroup::readEntry
QString readEntry(const char *key, const char *aDefault=0) const
KConfig
KDirWatch::addFile
void addFile(const QString &file)
KDirWatch::removeFile
void removeFile(const QString &file)
KDirWatch::self
static KDirWatch * self()
KFileSharePrivate
Definition: kfileshare_p.h:31
KFileSharePrivate::KFileSharePrivate
KFileSharePrivate()
Definition: kfileshare.cpp:61
KFileSharePrivate::~KFileSharePrivate
~KFileSharePrivate()
Definition: kfileshare.cpp:72
KFileSharePrivate::slotFileChange
void slotFileChange(const QString &)
Definition: kfileshare.cpp:83
KFileSharePrivate::self
static KFileSharePrivate * self()
Definition: kfileshare.cpp:77
KStandardDirs::findExe
static QString findExe(const QString &appname, const QString &pathstr=QString(), SearchOptions options=NoSearchOptions)
KUserGroup
KUserGroup::users
QList< KUser > users() const
KUser
QProcess
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
kDebug
#define kDebug
kconfig.h
kconfiggroup.h
kdebug.h
kdirwatch.h
s_shareMode
static KFileShare::ShareMode s_shareMode
Definition: kfileshare.cpp:38
s_fileShareGroup
static QString s_fileShareGroup
Definition: kfileshare.cpp:42
s_sharingEnabled
static bool s_sharingEnabled
Definition: kfileshare.cpp:43
s_sambaEnabled
static bool s_sambaEnabled
Definition: kfileshare.cpp:39
findExe
static QString findExe(const char *exeName)
Definition: kfileshare.cpp:47
readEntry
KFileShare::ShareMode readEntry(const KConfigGroup &cg, const char *key, const KFileShare::ShareMode &aDefault)
Definition: kfileshare.cpp:91
s_authorization
static KFileShare::Authorization s_authorization
Definition: kfileshare.cpp:36
s_nfsEnabled
static bool s_nfsEnabled
Definition: kfileshare.cpp:40
s_restricted
static bool s_restricted
Definition: kfileshare.cpp:41
FILESHARECONF
#define FILESHARECONF
Definition: kfileshare.cpp:45
kfileshare.h
kfileshare_p.h
klocale.h
kstandarddirs.h
readConfig
TsConfig readConfig(const QString &fname)
kuser.h
KFileShare
Common functionality for the file sharing (communication with the backend)
Definition: kfileshare.h:31
KFileShare::shareMode
ShareMode shareMode()
Returns the configured share mode.
Definition: kfileshare.cpp:139
KFileShare::readShareList
void readShareList()
Reads the list of shared folders.
Definition: kfileshare.cpp:183
KFileShare::isDirectoryShared
bool isDirectoryShared(const QString &path)
Call this to know if a directory is currently shared.
Definition: kfileshare.cpp:216
KFileShare::sharingEnabled
bool sharingEnabled()
Returns whether sharing is enabled If this is false, file sharing is disabled and nobody can share fi...
Definition: kfileshare.cpp:146
KFileShare::authorization
Authorization authorization()
Call this to know if the current user is authorized to share directories.
Definition: kfileshare.cpp:227
KFileShare::sambaEnabled
bool sambaEnabled()
Returns whether Samba is enabled.
Definition: kfileshare.cpp:168
KFileShare::nfsEnabled
bool nfsEnabled()
Returns whether NFS is enabled.
Definition: kfileshare.cpp:175
KFileShare::fileShareGroup
QString fileShareGroup()
Returns the group that is used for file sharing.
Definition: kfileshare.cpp:160
KFileShare::isRestricted
bool isRestricted()
Returns whether file sharing is restricted.
Definition: kfileshare.cpp:153
KFileShare::Authorization
Authorization
Definition: kfileshare.h:48
KFileShare::UserNotAllowed
@ UserNotAllowed
Definition: kfileshare.h:48
KFileShare::NotInitialized
@ NotInitialized
Definition: kfileshare.h:48
KFileShare::Authorized
@ Authorized
Definition: kfileshare.h:48
KFileShare::ErrorNotFound
@ ErrorNotFound
Definition: kfileshare.h:48
KFileShare::setShared
bool setShared(const QString &path, bool shared)
Uses a suid perl script to share the given path with NFS and Samba.
Definition: kfileshare.cpp:235
KFileShare::readConfig
void readConfig()
Reads the file share configuration file.
Definition: kfileshare.cpp:106
KFileShare::ShareMode
ShareMode
The used share mode.
Definition: kfileshare.h:70
KFileShare::Advanced
@ Advanced
Definition: kfileshare.h:70
KFileShare::Simple
@ Simple
Definition: kfileshare.h:70
config
KSharedConfigPtr config()
group
group
ok
KGuiItem ok()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal