kfileshare.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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()
00086 {
00087
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
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
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
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
00216 if ( s_authorization == NotInitialized )
00217 readConfig();
00218 return s_authorization;
00219 }
00220
00221 QString KFileShare::findExe( const char* exeName )
00222 {
00223
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 );
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
00259 break;
00260 case 3:
00261
00262
00263
00264 ok = true;
00265 break;
00266 case 4:
00267
00268 break;
00269 case 5:
00270
00271
00272
00273 ok = true;
00274 break;
00275 case 6:
00276
00277 break;
00278 case 7:
00279
00280 break;
00281 case 8:
00282
00283 break;
00284 case 255:
00285
00286 break;
00287 }
00288 }
00289
00290 return ok;
00291 }
00292
00293 #include "kfileshare.moc"
This file is part of the documentation for kio Library Version 3.3.2.