KIO
kfileshare.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE project 00002 Copyright (c) 2001 David Faure <faure@kde.org> 00003 Copyright (c) 2001 Laurent Montel <lmontel@mandrakesoft.com> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kfileshare.h" 00021 #include "kfileshare_p.h" 00022 #include <QtCore/QDir> 00023 #include <QtCore/QFile> 00024 #include <QtCore/Q_PID> 00025 #include <klocale.h> 00026 #include <kstandarddirs.h> 00027 #include <kdebug.h> 00028 #include <kdirwatch.h> 00029 #include <stdio.h> 00030 #include <stdlib.h> 00031 #include <errno.h> 00032 #include <kconfig.h> 00033 #include <kconfiggroup.h> 00034 #include <kuser.h> 00035 00036 static KFileShare::Authorization s_authorization = KFileShare::NotInitialized; 00037 K_GLOBAL_STATIC(QStringList, s_shareList) 00038 static KFileShare::ShareMode s_shareMode; 00039 static bool s_sambaEnabled; 00040 static bool s_nfsEnabled; 00041 static bool s_restricted; 00042 static QString s_fileShareGroup; 00043 static bool s_sharingEnabled; 00044 00045 #define FILESHARECONF "/etc/security/fileshare.conf" 00046 00047 static QString findExe( const char* exeName ) 00048 { 00049 // Normally fileshareset and filesharelist are installed in kde4/libexec; 00050 // allow distributions to move it somewhere else in the PATH or in /usr/sbin. 00051 QString path = QString::fromLocal8Bit(qgetenv("PATH")); 00052 #ifndef Q_WS_WIN 00053 path += QLatin1String(":/usr/sbin"); 00054 #endif 00055 QString exe = KStandardDirs::findExe( exeName, path ); 00056 if (exe.isEmpty()) 00057 kError() << exeName << "not found in" << path; 00058 return exe; 00059 } 00060 00061 KFileSharePrivate::KFileSharePrivate() 00062 { 00063 KDirWatch::self()->addFile(FILESHARECONF); 00064 connect(KDirWatch::self(), SIGNAL(dirty (const QString&)),this, 00065 SLOT(slotFileChange(const QString &))); 00066 connect(KDirWatch::self(), SIGNAL(created(const QString&)),this, 00067 SLOT(slotFileChange(const QString &))); 00068 connect(KDirWatch::self(), SIGNAL(deleted(const QString&)),this, 00069 SLOT(slotFileChange(const QString &))); 00070 } 00071 00072 KFileSharePrivate::~KFileSharePrivate() 00073 { 00074 KDirWatch::self()->removeFile(FILESHARECONF); 00075 } 00076 00077 KFileSharePrivate* KFileSharePrivate::self() 00078 { 00079 K_GLOBAL_STATIC(KFileSharePrivate, _self) 00080 return _self; 00081 } 00082 00083 void KFileSharePrivate::slotFileChange(const QString &file) 00084 { 00085 if(file==FILESHARECONF) { 00086 KFileShare::readConfig(); 00087 KFileShare::readShareList(); 00088 } 00089 } 00090 00091 KFileShare::ShareMode readEntry(const KConfigGroup &cg, const char* key, 00092 const KFileShare::ShareMode& aDefault) 00093 { 00094 const QByteArray data=cg.readEntry(key, QByteArray()); 00095 00096 if (!data.isEmpty()) { 00097 if (data.toLower() == "simple") 00098 return KFileShare::Simple; 00099 else if (data.toLower() == "advanced") 00100 return KFileShare::Advanced; 00101 } 00102 00103 return aDefault; 00104 } 00105 00106 void KFileShare::readConfig() // static 00107 { 00108 // Create KFileSharePrivate instance 00109 KFileSharePrivate::self(); 00110 KConfig config(QLatin1String(FILESHARECONF)); 00111 KConfigGroup group( &config, QString() ); 00112 00113 s_sharingEnabled = group.readEntry("FILESHARING", true); 00114 s_restricted = group.readEntry("RESTRICT", true); 00115 s_fileShareGroup = group.readEntry("FILESHAREGROUP", "fileshare"); 00116 00117 00118 if (!s_sharingEnabled) 00119 s_authorization = UserNotAllowed; 00120 else 00121 if (!s_restricted ) 00122 s_authorization = Authorized; 00123 else { 00124 // check if current user is in fileshare group 00125 KUserGroup shareGroup(s_fileShareGroup); 00126 if (shareGroup.users().contains(KUser()) ) 00127 s_authorization = Authorized; 00128 else 00129 s_authorization = UserNotAllowed; 00130 } 00131 00132 s_shareMode = readEntry(group, "SHARINGMODE", Simple); 00133 00134 00135 s_sambaEnabled = group.readEntry("SAMBA", true); 00136 s_nfsEnabled = group.readEntry("NFS", true); 00137 } 00138 00139 KFileShare::ShareMode KFileShare::shareMode() { 00140 if ( s_authorization == NotInitialized ) 00141 readConfig(); 00142 00143 return s_shareMode; 00144 } 00145 00146 bool KFileShare::sharingEnabled() { 00147 if ( s_authorization == NotInitialized ) 00148 readConfig(); 00149 00150 return s_sharingEnabled; 00151 } 00152 00153 bool KFileShare::isRestricted() { 00154 if ( s_authorization == NotInitialized ) 00155 readConfig(); 00156 00157 return s_restricted; 00158 } 00159 00160 QString KFileShare::fileShareGroup() { 00161 if ( s_authorization == NotInitialized ) 00162 readConfig(); 00163 00164 return s_fileShareGroup; 00165 } 00166 00167 00168 bool KFileShare::sambaEnabled() { 00169 if ( s_authorization == NotInitialized ) 00170 readConfig(); 00171 00172 return s_sambaEnabled; 00173 } 00174 00175 bool KFileShare::nfsEnabled() { 00176 if ( s_authorization == NotInitialized ) 00177 readConfig(); 00178 00179 return s_nfsEnabled; 00180 } 00181 00182 00183 void KFileShare::readShareList() 00184 { 00185 KFileSharePrivate::self(); 00186 s_shareList->clear(); 00187 00188 QString exe = ::findExe( "filesharelist" ); 00189 if (exe.isEmpty()) { 00190 s_authorization = ErrorNotFound; 00191 return; 00192 } 00193 QProcess proc; 00194 proc.start( exe, QStringList() ); 00195 if ( !proc.waitForFinished() ) { 00196 kError() << "Can't run" << exe; 00197 s_authorization = ErrorNotFound; 00198 return; 00199 } 00200 00201 // Reading code shamelessly stolen from khostname.cpp ;) 00202 while (!proc.atEnd()) { 00203 QString line = proc.readLine().trimmed(); 00204 int length = line.length(); 00205 if ( length > 0 ) 00206 { 00207 if ( line[length-1] != '/' ) 00208 line += '/'; 00209 s_shareList->append(line); 00210 kDebug(7000) << "Shared dir:" << line; 00211 } 00212 } 00213 } 00214 00215 00216 bool KFileShare::isDirectoryShared( const QString& _path ) 00217 { 00218 if ( ! s_shareList.exists() ) 00219 readShareList(); 00220 00221 QString path( _path ); 00222 if ( path[path.length()-1] != '/' ) 00223 path += '/'; 00224 return s_shareList->contains( path ); 00225 } 00226 00227 KFileShare::Authorization KFileShare::authorization() 00228 { 00229 // The app should do this on startup, but if it doesn't, let's do here. 00230 if ( s_authorization == NotInitialized ) 00231 readConfig(); 00232 return s_authorization; 00233 } 00234 00235 bool KFileShare::setShared( const QString& path, bool shared ) 00236 { 00237 if (! KFileShare::sharingEnabled() || 00238 KFileShare::shareMode() == Advanced) 00239 return false; 00240 00241 kDebug(7000) << path << "," << shared; 00242 QString exe = ::findExe( "fileshareset" ); 00243 if (exe.isEmpty()) 00244 return false; 00245 00246 QStringList args; 00247 if ( shared ) 00248 args << "--add"; 00249 else 00250 args << "--remove"; 00251 args << path ; 00252 int ec = QProcess::execute( exe, args ); // should be ok, the perl script terminates fast 00253 kDebug(7000) << "exitCode=" << ec; 00254 bool ok = !ec; 00255 switch (ec) { 00256 case 1: 00257 // User is not authorized 00258 break; 00259 case 3: 00260 // Called script with --add, but path was already shared before. 00261 // Result is nevertheless what the client wanted, so 00262 // this is alright. 00263 ok = true; 00264 break; 00265 case 4: 00266 // Invalid mount point 00267 break; 00268 case 5: 00269 // Called script with --remove, but path was not shared before. 00270 // Result is nevertheless what the client wanted, so 00271 // this is alright. 00272 ok = true; 00273 break; 00274 case 6: 00275 // There is no export method 00276 break; 00277 case 7: 00278 // file sharing is disabled 00279 break; 00280 case 8: 00281 // advanced sharing is enabled 00282 break; 00283 case 255: 00284 // Abitrary error 00285 break; 00286 } 00287 00288 return ok; 00289 } 00290 00291 //#include "kfileshare.moc" 00292 #include "kfileshare_p.moc" 00293
KDE 4.6 API Reference