• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.2 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 
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;
44 
45 #define FILESHARECONF "/etc/security/fileshare.conf"
46 
47 static 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 
61 KFileSharePrivate::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 
72 KFileSharePrivate::~KFileSharePrivate()
73 {
74  KDirWatch::self()->removeFile(FILESHARECONF);
75 }
76 
77 KFileSharePrivate* KFileSharePrivate::self()
78 {
79  K_GLOBAL_STATIC(KFileSharePrivate, _self)
80  return _self;
81 }
82 
83 void KFileSharePrivate::slotFileChange(const QString &file)
84 {
85  if(file==FILESHARECONF) {
86  KFileShare::readConfig();
87  KFileShare::readShareList();
88  }
89 }
90 
91 KFileShare::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 
106 void 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 
139 KFileShare::ShareMode KFileShare::shareMode() {
140  if ( s_authorization == NotInitialized )
141  readConfig();
142 
143  return s_shareMode;
144 }
145 
146 bool KFileShare::sharingEnabled() {
147  if ( s_authorization == NotInitialized )
148  readConfig();
149 
150  return s_sharingEnabled;
151 }
152 
153 bool KFileShare::isRestricted() {
154  if ( s_authorization == NotInitialized )
155  readConfig();
156 
157  return s_restricted;
158 }
159 
160 QString KFileShare::fileShareGroup() {
161  if ( s_authorization == NotInitialized )
162  readConfig();
163 
164  return s_fileShareGroup;
165 }
166 
167 
168 bool KFileShare::sambaEnabled() {
169  if ( s_authorization == NotInitialized )
170  readConfig();
171 
172  return s_sambaEnabled;
173 }
174 
175 bool KFileShare::nfsEnabled() {
176  if ( s_authorization == NotInitialized )
177  readConfig();
178 
179  return s_nfsEnabled;
180 }
181 
182 
183 void 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 
216 bool 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 
227 KFileShare::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 
235 bool 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 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Apr 16 2013 21:04:53 by doxygen 1.8.3.1 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.10.2 API Reference

Skip menu "kdelibs-4.10.2 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