kio Library API Documentation

kfileshare.cpp

00001 /* This file is part of the KDE project 00002 Copyright (c) 2001 David Faure <david@mandrakesoft.com> 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., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #include "kfileshare.h" 00021 #include <qdir.h> 00022 #include <qfile.h> 00023 #include <kprocess.h> 00024 #include <kprocio.h> 00025 #include <klocale.h> 00026 #include <kstaticdeleter.h> 00027 #include <kstandarddirs.h> 00028 #include <kdebug.h> 00029 #include <kdirwatch.h> 00030 #include <stdio.h> 00031 #include <stdlib.h> 00032 #include <errno.h> 00033 #include <kdirnotify_stub.h> 00034 #include <ksimpleconfig.h> 00035 #include <kuser.h> 00036 00037 KFileShare::Authorization KFileShare::s_authorization = NotInitialized; 00038 QStringList* KFileShare::s_shareList = 0L; 00039 static KStaticDeleter<QStringList> sdShareList; 00040 00041 KFileShare::ShareMode KFileShare::s_shareMode; 00042 bool KFileShare::s_sambaEnabled; 00043 bool KFileShare::s_nfsEnabled; 00044 bool KFileShare::s_restricted; 00045 QString KFileShare::s_fileShareGroup; 00046 bool KFileShare::s_sharingEnabled; 00047 00048 00049 #define FILESHARECONF "/etc/security/fileshare.conf" 00050 00051 KFileSharePrivate::KFileSharePrivate() 00052 { 00053 if (QFile::exists(FILESHARECONF)) { 00054 KDirWatch::self()->addFile(FILESHARECONF); 00055 connect(KDirWatch::self(), SIGNAL(dirty (const QString&)),this, 00056 SLOT(slotFileChange(const QString &))); 00057 } 00058 } 00059 00060 KFileSharePrivate::~KFileSharePrivate() 00061 { 00062 00063 } 00064 00065 KFileSharePrivate *KFileSharePrivate::_self=0L; 00066 00067 static KStaticDeleter<KFileSharePrivate> kstFileShare; 00068 00069 KFileSharePrivate* KFileSharePrivate::self() 00070 { 00071 if (!_self) 00072 _self = kstFileShare.setObject(_self, new KFileSharePrivate()); 00073 return _self; 00074 } 00075 00076 void KFileSharePrivate::slotFileChange(const QString &file) 00077 { 00078 if(file==FILESHARECONF) { 00079 KFileShare::readConfig(); 00080 KFileShare::readShareList(); 00081 } 00082 } 00083 00084 00085 void KFileShare::readConfig() // static 00086 { 00087 // Create KFileSharePrivate instance 00088 KFileSharePrivate::self(); 00089 KSimpleConfig config(QString::fromLatin1(FILESHARECONF),true); 00090 00091 s_sharingEnabled = config.readEntry("FILESHARING", "yes") == "yes"; 00092 s_restricted = config.readEntry("RESTRICT", "yes") == "yes"; 00093 s_fileShareGroup = config.readEntry("FILESHAREGROUP", "fileshare"); 00094 00095 00096 if (!s_sharingEnabled) 00097 s_authorization = UserNotAllowed; 00098 else 00099 if (!s_restricted ) 00100 s_authorization = Authorized; 00101 else { 00102 // check if current user is in fileshare group 00103 KUserGroup shareGroup(s_fileShareGroup); 00104 if (shareGroup.users().findIndex(KUser()) > -1 ) 00105 s_authorization = Authorized; 00106 else 00107 s_authorization = UserNotAllowed; 00108 } 00109 00110 if (config.readEntry("SHARINGMODE", "simple") == "simple") 00111 s_shareMode = Simple; 00112 else 00113 s_shareMode = Advanced; 00114 00115 00116 s_sambaEnabled = config.readEntry("SAMBA", "yes") == "yes"; 00117 s_nfsEnabled = config.readEntry("NFS", "yes") == "yes"; 00118 } 00119 00120 KFileShare::ShareMode KFileShare::shareMode() { 00121 if ( s_authorization == NotInitialized ) 00122 readConfig(); 00123 00124 return s_shareMode; 00125 } 00126 00127 bool KFileShare::sharingEnabled() { 00128 if ( s_authorization == NotInitialized ) 00129 readConfig(); 00130 00131 return s_sharingEnabled; 00132 } 00133 00134 bool KFileShare::isRestricted() { 00135 if ( s_authorization == NotInitialized ) 00136 readConfig(); 00137 00138 return s_restricted; 00139 } 00140 00141 QString KFileShare::fileShareGroup() { 00142 if ( s_authorization == NotInitialized ) 00143 readConfig(); 00144 00145 return s_fileShareGroup; 00146 } 00147 00148 00149 bool KFileShare::sambaEnabled() { 00150 if ( s_authorization == NotInitialized ) 00151 readConfig(); 00152 00153 return s_sambaEnabled; 00154 } 00155 00156 bool KFileShare::nfsEnabled() { 00157 if ( s_authorization == NotInitialized ) 00158 readConfig(); 00159 00160 return s_nfsEnabled; 00161 } 00162 00163 00164 void KFileShare::readShareList() 00165 { 00166 KFileSharePrivate::self(); 00167 if ( !s_shareList ) 00168 sdShareList.setObject( s_shareList, new QStringList ); 00169 else 00170 s_shareList->clear(); 00171 00172 // /usr/sbin on Mandrake, $PATH allows flexibility for other distributions 00173 QString exe = findExe( "filesharelist" ); 00174 if (exe.isEmpty()) { 00175 s_authorization = ErrorNotFound; 00176 return; 00177 } 00178 KProcIO proc; 00179 proc << exe; 00180 if ( !proc.start( KProcess::Block ) ) { 00181 kdError() << "Can't run " << exe << endl; 00182 s_authorization = ErrorNotFound; 00183 return; 00184 } 00185 00186 // Reading code shamelessly stolen from khostname.cpp ;) 00187 QString line; 00188 int length; 00189 do { 00190 length = proc.readln(line, true); 00191 if ( length > 0 ) 00192 { 00193 if ( line[length-1] != '/' ) 00194 line += '/'; 00195 s_shareList->append(line); 00196 kdDebug(7000) << "Shared dir:" << line << endl; 00197 } 00198 } while (length > -1); 00199 } 00200 00201 00202 bool KFileShare::isDirectoryShared( const QString& _path ) 00203 { 00204 if ( ! s_shareList ) 00205 readShareList(); 00206 00207 QString path( _path ); 00208 if ( path[path.length()-1] != '/' ) 00209 path += '/'; 00210 return s_shareList && s_shareList->contains( path ); 00211 } 00212 00213 KFileShare::Authorization KFileShare::authorization() 00214 { 00215 // The app should do this on startup, but if it doesn't, let's do here. 00216 if ( s_authorization == NotInitialized ) 00217 readConfig(); 00218 return s_authorization; 00219 } 00220 00221 QString KFileShare::findExe( const char* exeName ) 00222 { 00223 // /usr/sbin on Mandrake, $PATH allows flexibility for other distributions 00224 QString path = QString::fromLocal8Bit(getenv("PATH")) + QString::fromLatin1(":/usr/sbin"); 00225 QString exe = KStandardDirs::findExe( exeName, path ); 00226 if (exe.isEmpty()) 00227 kdError() << exeName << " not found in " << path << endl; 00228 return exe; 00229 } 00230 00231 bool KFileShare::setShared( const QString& path, bool shared ) 00232 { 00233 if (! KFileShare::sharingEnabled() || 00234 KFileShare::shareMode() == Advanced) 00235 return false; 00236 00237 kdDebug(7000) << "KFileShare::setShared " << path << "," << shared << endl; 00238 QString exe = KFileShare::findExe( "fileshareset" ); 00239 if (exe.isEmpty()) 00240 return false; 00241 00242 KProcess proc; 00243 proc << exe; 00244 if ( shared ) 00245 proc << "--add"; 00246 else 00247 proc << "--remove"; 00248 proc << path; 00249 proc.start( KProcess::Block ); // should be ok, the perl script terminates fast 00250 bool ok = proc.normalExit() && (proc.exitStatus() == 0); 00251 kdDebug(7000) << "KFileSharePropsPlugin::setShared normalExit=" 00252 << proc.normalExit() << endl; 00253 kdDebug(7000) << "KFileSharePropsPlugin::setShared exitStatus=" 00254 << proc.exitStatus() << endl; 00255 if ( proc.normalExit() ) { 00256 switch( proc.exitStatus() ) { 00257 case 1: 00258 // User is not authorized 00259 break; 00260 case 3: 00261 // Called script with --add, but path was already shared before. 00262 // Result is nevertheless what the client wanted, so 00263 // this is alright. 00264 ok = true; 00265 break; 00266 case 4: 00267 // Invalid mount point 00268 break; 00269 case 5: 00270 // Called script with --remove, but path was not shared before. 00271 // Result is nevertheless what the client wanted, so 00272 // this is alright. 00273 ok = true; 00274 break; 00275 case 6: 00276 // There is no export method 00277 break; 00278 case 7: 00279 // file sharing is disabled 00280 break; 00281 case 8: 00282 // advanced sharing is enabled 00283 break; 00284 case 255: 00285 // Abitrary error 00286 break; 00287 } 00288 } 00289 00290 return ok; 00291 } 00292 00293 #include "kfileshare.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.3.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 17 11:29:25 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003