kio Library: kfileshare.cpp Source File (kio Library)

| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

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

This file is part of the documentation for kio Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:23:59 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003