• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdeio
 

tdeio/tdeio

  • tdeio
  • tdeio
tdefileshare.cpp
1/* This file is part of the KDE project
2 Copyright (c) 2001 David Faure <david@mandrakesoft.com>
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 "tdefileshare.h"
21#include <tqdir.h>
22#include <tqfile.h>
23#include <tqregexp.h>
24#include <kprocess.h>
25#include <kprocio.h>
26#include <tdelocale.h>
27#include <kstaticdeleter.h>
28#include <kstandarddirs.h>
29#include <kdebug.h>
30#include <kdirwatch.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <errno.h>
34#include <kdirnotify_stub.h>
35#include <ksimpleconfig.h>
36#include <kuser.h>
37
38KFileShare::Authorization KFileShare::s_authorization = NotInitialized;
39//TQStringList* KFileShare::s_shareList = 0L;
40//static KStaticDeleter<TQStringList> sdShareList;
41TQMap<TQString,TQString>* KFileShare::s_shareMap = 0L;
42static KStaticDeleter<TQMap<TQString,TQString> > sdShareMap;
43
44KFileShare::ShareMode KFileShare::s_shareMode;
45bool KFileShare::s_sambaEnabled;
46bool KFileShare::s_nfsEnabled;
47bool KFileShare::s_restricted;
48TQString KFileShare::s_fileShareGroup;
49bool KFileShare::s_sharingEnabled;
50
51
52#define FILESHARECONF "/etc/security/fileshare.conf"
53
54KFileSharePrivate::KFileSharePrivate()
55{
56 KDirWatch::self()->addFile(FILESHARECONF);
57 connect(KDirWatch::self(), TQ_SIGNAL(dirty (const TQString&)),this,
58 TQ_SLOT(slotFileChange(const TQString &)));
59 connect(KDirWatch::self(), TQ_SIGNAL(created(const TQString&)),this,
60 TQ_SLOT(slotFileChange(const TQString &)));
61 connect(KDirWatch::self(), TQ_SIGNAL(deleted(const TQString&)),this,
62 TQ_SLOT(slotFileChange(const TQString &)));
63}
64
65KFileSharePrivate::~KFileSharePrivate()
66{
67 KDirWatch::self()->removeFile(FILESHARECONF);
68}
69
70KFileSharePrivate *KFileSharePrivate::_self=0L;
71
72static KStaticDeleter<KFileSharePrivate> kstFileShare;
73
74KFileSharePrivate* KFileSharePrivate::self()
75{
76 if (!_self)
77 _self = kstFileShare.setObject(_self, new KFileSharePrivate());
78 return _self;
79}
80
81void KFileSharePrivate::slotFileChange(const TQString &file)
82{
83 if(file==FILESHARECONF) {
84 KFileShare::readConfig();
85 KFileShare::readShareList();
86 }
87}
88
89void KFileShare::readConfig() // static
90{
91 // Create KFileSharePrivate instance
92 KFileSharePrivate::self();
93 KSimpleConfig config(TQString::fromLatin1(FILESHARECONF),true);
94
95 s_sharingEnabled = config.readEntry("FILESHARING", "yes") == "yes";
96 s_restricted = config.readEntry("RESTRICT", "yes") == "yes";
97 s_fileShareGroup = config.readEntry("FILESHAREGROUP", "fileshare");
98
99
100 if (!s_sharingEnabled)
101 s_authorization = UserNotAllowed;
102 else
103 if (!s_restricted )
104 s_authorization = Authorized;
105 else {
106 // check if current user is in fileshare group
107 KUserGroup shareGroup(s_fileShareGroup);
108 if (shareGroup.users().findIndex(KUser()) > -1 )
109 s_authorization = Authorized;
110 else
111 s_authorization = UserNotAllowed;
112 }
113
114 if (config.readEntry("SHARINGMODE", "simple") == "simple")
115 s_shareMode = Simple;
116 else
117 s_shareMode = Advanced;
118
119
120 s_sambaEnabled = config.readEntry("SAMBA", "yes") == "yes";
121 s_nfsEnabled = config.readEntry("NFS", "yes") == "yes";
122}
123
124KFileShare::ShareMode KFileShare::shareMode() {
125 if ( s_authorization == NotInitialized )
126 readConfig();
127
128 return s_shareMode;
129}
130
131bool KFileShare::sharingEnabled() {
132 if ( s_authorization == NotInitialized )
133 readConfig();
134
135 return s_sharingEnabled;
136}
137
138bool KFileShare::isRestricted() {
139 if ( s_authorization == NotInitialized )
140 readConfig();
141
142 return s_restricted;
143}
144
145TQString KFileShare::fileShareGroup() {
146 if ( s_authorization == NotInitialized )
147 readConfig();
148
149 return s_fileShareGroup;
150}
151
152
153bool KFileShare::sambaEnabled() {
154 if ( s_authorization == NotInitialized )
155 readConfig();
156
157 return s_sambaEnabled;
158}
159
160bool KFileShare::nfsEnabled() {
161 if ( s_authorization == NotInitialized )
162 readConfig();
163
164 return s_nfsEnabled;
165}
166
167
168void KFileShare::readShareList()
169{
170 KFileSharePrivate::self();
171 if ( !s_shareMap )
172 sdShareMap.setObject( s_shareMap, new TQMap<TQString,TQString> );
173 else
174 s_shareMap->clear();
175
176 // /usr/sbin on Mandrake, $PATH allows flexibility for other distributions
177 TQString exe = findExe( "filesharelist" );
178 if (exe.isEmpty()) {
179 s_authorization = ErrorNotFound;
180 return;
181 }
182 KProcIO proc;
183 proc << exe;
184 if ( !proc.start( TDEProcess::Block ) ) {
185 kdError() << "Can't run " << exe << endl;
186 s_authorization = ErrorNotFound;
187 return;
188 }
189
190 // Reading code shamelessly stolen from khostname.cpp ;)
191 TQString line;
192 TQString options;
193 TQString path;
194 int length;
195 TQRegExp rx_line("([^\\s]+)\\s+(.*)");
196 do {
197 length = proc.readln(line, true);
198 if ( length > 0 )
199 {
200 if ( line[length-1] != '/' )
201 line += '/';
202 if( rx_line.search( line ) != -1 ) {
203 options = rx_line.cap(1);
204 path = rx_line.cap(2);
205 (*s_shareMap)[path] = options;
206 }
207 kdDebug(7000) << "Shared dir:" << line << endl;
208 }
209 } while (length > -1);
210}
211
212
213int KFileShare::isDirectoryShared( const TQString& _path )
214{
215 int ret(0);
216
217 if ( ! s_shareMap )
218 readShareList();
219
220 TQString path( _path );
221 if ( path[path.length()-1] != '/' )
222 path += '/';
223 //return s_shareList && s_shareList->contains( path );
224 if( (*s_shareMap).contains(path) && !((*s_shareMap)[path].isEmpty()) ) {
225 ret+=1;
226 if( (*s_shareMap)[path].find("readwrite") != -1 )
227 ret+=2;
228 }
229
230 return ret;
231}
232
233KFileShare::Authorization KFileShare::authorization()
234{
235 // The app should do this on startup, but if it doesn't, let's do here.
236 if ( s_authorization == NotInitialized )
237 readConfig();
238 return s_authorization;
239}
240
241TQString KFileShare::findExe( const char* exeName )
242{
243 // /usr/sbin on Mandrake, $PATH allows flexibility for other distributions
244 TQString path = TQString::fromLocal8Bit(getenv("PATH")) + TQString::fromLatin1(":/usr/sbin");
245 TQString exe = TDEStandardDirs::findExe( exeName, path );
246 if (exe.isEmpty())
247 kdError() << exeName << " not found in " << path << endl;
248 return exe;
249}
250
251bool KFileShare::setShared( const TQString& path, bool shared )
252{
253 return SuSEsetShared( path, shared, false );
254}
255
256bool KFileShare::SuSEsetShared( const TQString& path, bool shared, bool rw )
257{
258 if (! KFileShare::sharingEnabled() ||
259 KFileShare::shareMode() == Advanced)
260 return false;
261
262 TQString exe = KFileShare::findExe( "fileshareset" );
263 if (exe.isEmpty())
264 return false;
265
266 // we want to share, so we kick it first - just to be sure
267 TDEProcess proc;
268 proc << exe;
269 proc << "--remove";
270 proc << path;
271 proc.start( TDEProcess::Block );
272 proc.clearArguments();
273
274 proc << exe;
275 if( rw )
276 proc << "--rw";
277 if ( shared )
278 proc << "--add";
279 else
280 proc << "--remove";
281 proc << path;
282 proc.start( TDEProcess::Block ); // should be ok, the perl script terminates fast
283 bool ok = proc.normalExit() && (proc.exitStatus() == 0);
284 kdDebug(7000) << "KFileSharePropsPlugin::setShared normalExit="
285 << proc.normalExit() << endl;
286 kdDebug(7000) << "KFileSharePropsPlugin::setShared exitStatus="
287 << proc.exitStatus() << endl;
288 if ( proc.normalExit() ) {
289 switch( proc.exitStatus() ) {
290 case 1:
291 // User is not authorized
292 break;
293 case 3:
294 // Called script with --add, but path was already shared before.
295 // Result is nevertheless what the client wanted, so
296 // this is alright.
297 ok = true;
298 break;
299 case 4:
300 // Invalid mount point
301 break;
302 case 5:
303 // Called script with --remove, but path was not shared before.
304 // Result is nevertheless what the client wanted, so
305 // this is alright.
306 ok = true;
307 break;
308 case 6:
309 // There is no export method
310 break;
311 case 7:
312 // file sharing is disabled
313 break;
314 case 8:
315 // advanced sharing is enabled
316 break;
317 case 255:
318 // Abitrary error
319 break;
320 }
321 }
322
323 return ok;
324}
325
326bool KFileShare::sambaActive()
327{
328 // rcsmb is not executable by users, try ourselves
329 int status = system( "/sbin/checkproc -p /var/run/samba/smbd.pid /usr/sbin/smbd" );
330 return status != -1 && WIFEXITED( status ) && WEXITSTATUS( status ) == 0;
331}
332
333bool KFileShare::nfsActive()
334{
335 // rcnfsserver is not executable by users, try ourselves
336 int status = system( "/sbin/checkproc /usr/sbin/rpc.mountd" );
337 if( status != -1 && WIFEXITED( status ) && WEXITSTATUS( status ) == 0 )
338 {
339 status = system( "/sbin/checkproc -n nfsd" );
340 if( status != -1 && WIFEXITED( status ) && WEXITSTATUS( status ) == 0 )
341 return true;
342 }
343 return false;
344}
345
346#include "tdefileshare.moc"
KDirWatch::self
static KDirWatch * self()
The KDirWatch instance usually globally used in an application.
Definition: kdirwatch.cpp:1634
KDirWatch::addFile
void addFile(const TQString &file)
Adds a file to be watched.
Definition: kdirwatch.cpp:1696
KDirWatch::removeFile
void removeFile(const TQString &file)
Removes a file from the list of watched files.
Definition: kdirwatch.cpp:1723
KFileShare::shareMode
static ShareMode shareMode()
Returns the configured share mode.
Definition: tdefileshare.cpp:124
KFileShare::readShareList
static void readShareList()
Reads the list of shared folders.
Definition: tdefileshare.cpp:168
KFileShare::sharingEnabled
static bool sharingEnabled()
Returns whether sharing is enabled If this is false, file sharing is disabled and nobody can share fi...
Definition: tdefileshare.cpp:131
KFileShare::sambaActive
static bool sambaActive()
Returns whether Samba is active (service is running)
Definition: tdefileshare.cpp:326
KFileShare::authorization
static Authorization authorization()
Call this to know if the current user is authorized to share directories.
Definition: tdefileshare.cpp:233
KFileShare::sambaEnabled
static bool sambaEnabled()
Returns whether Samba is enabled.
Definition: tdefileshare.cpp:153
KFileShare::nfsActive
static bool nfsActive()
Returns whether NFS is active (service is running)
Definition: tdefileshare.cpp:333
KFileShare::nfsEnabled
static bool nfsEnabled()
Returns whether NFS is enabled.
Definition: tdefileshare.cpp:160
KFileShare::isDirectoryShared
static int isDirectoryShared(const TQString &path)
Call this to know if a directory is currently shared.
Definition: tdefileshare.cpp:213
KFileShare::isRestricted
static bool isRestricted()
Returns whether file sharing is restricted.
Definition: tdefileshare.cpp:138
KFileShare::fileShareGroup
static TQString fileShareGroup()
Returns the group that is used for file sharing.
Definition: tdefileshare.cpp:145
KFileShare::setShared
static bool setShared(const TQString &path, bool shared)
Uses a suid perl script to share the given path with NFS and Samba.
Definition: tdefileshare.cpp:251
KFileShare::readConfig
static void readConfig()
Reads the file share configuration file.
Definition: tdefileshare.cpp:89
KFileShare::ShareMode
ShareMode
The used share mode.
Definition: tdefileshare.h:101

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdeio by doxygen 1.9.4
This website is maintained by Timothy Pearson.