File: kfileshare.cpp

package info (click to toggle)
kde4libs 4:4.14.2-5+deb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 82,428 kB
  • ctags: 99,415
  • sloc: cpp: 761,864; xml: 12,344; ansic: 6,295; java: 4,060; perl: 2,938; yacc: 2,507; python: 1,207; sh: 1,179; ruby: 337; lex: 278; makefile: 29
file content (295 lines) | stat: -rw-r--r-- 7,871 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* This file is part of the KDE project
   Copyright (c) 2001 David Faure <faure@kde.org>
   Copyright (c) 2001 Laurent Montel <lmontel@mandrakesoft.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "kfileshare.h"
#include "kfileshare_p.h"
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/Q_PID>
#include <klocale.h>
#include <kstandarddirs.h>
#include <kdebug.h>
#include <kdirwatch.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <kconfig.h>
#include <kconfiggroup.h>
#include <kuser.h>

static KFileShare::Authorization s_authorization = KFileShare::NotInitialized;
K_GLOBAL_STATIC(QStringList, s_shareList)
static KFileShare::ShareMode s_shareMode;
static bool s_sambaEnabled;
static bool s_nfsEnabled;
static bool s_restricted;
static QString s_fileShareGroup;
static bool s_sharingEnabled;

#define FILESHARECONF "/etc/security/fileshare.conf"

static QString findExe( const char* exeName )
{
   // Normally fileshareset and filesharelist are installed in kde4/libexec;
   // allow distributions to move it somewhere else in the PATH or in /usr/sbin.
   QString path = QString::fromLocal8Bit(qgetenv("PATH"));
#ifndef Q_WS_WIN
   path += QLatin1String(":/usr/sbin");
#endif
   QString exe = KStandardDirs::findExe( exeName, path );
   if (exe.isEmpty())
       kError() << exeName << "not found in" << path;
   return exe;
}

KFileSharePrivate::KFileSharePrivate()
{
  KDirWatch::self()->addFile(FILESHARECONF);
  connect(KDirWatch::self(), SIGNAL(dirty(QString)),this,
          SLOT(slotFileChange(QString)));
  connect(KDirWatch::self(), SIGNAL(created(QString)),this,
          SLOT(slotFileChange(QString)));
  connect(KDirWatch::self(), SIGNAL(deleted(QString)),this,
          SLOT(slotFileChange(QString)));
}

KFileSharePrivate::~KFileSharePrivate()
{
  KDirWatch::self()->removeFile(FILESHARECONF);
}

KFileSharePrivate* KFileSharePrivate::self()
{
   K_GLOBAL_STATIC(KFileSharePrivate, _self)
   return _self;
}

void KFileSharePrivate::slotFileChange(const QString &file)
{
  if(file==FILESHARECONF) {
     KFileShare::readConfig();
     KFileShare::readShareList();
  }
}

KFileShare::ShareMode readEntry(const KConfigGroup &cg, const char* key,
	const KFileShare::ShareMode& aDefault)
{
    const QByteArray data=cg.readEntry(key, QByteArray());

    if (!data.isEmpty()) {
        if (data.toLower() == "simple")
            return KFileShare::Simple;
        else if (data.toLower() == "advanced")
            return KFileShare::Advanced;
    }

    return aDefault;
}

void KFileShare::readConfig() // static
{
    // Create KFileSharePrivate instance
    KFileSharePrivate::self();
    KConfig config(QLatin1String(FILESHARECONF));
    KConfigGroup group( &config, QString() );

    s_sharingEnabled = group.readEntry("FILESHARING", true);
    s_restricted = group.readEntry("RESTRICT", true);
    s_fileShareGroup = group.readEntry("FILESHAREGROUP", "fileshare");


    if (!s_sharingEnabled)
        s_authorization = UserNotAllowed;
    else
    if (!s_restricted )
        s_authorization = Authorized;
    else {
        // check if current user is in fileshare group
        KUserGroup shareGroup(s_fileShareGroup);
        if (shareGroup.users().contains(KUser()) )
            s_authorization = Authorized;
        else
            s_authorization = UserNotAllowed;
    }

    s_shareMode = readEntry(group, "SHARINGMODE", Simple);


    s_sambaEnabled = group.readEntry("SAMBA", true);
    s_nfsEnabled = group.readEntry("NFS", true);
}

KFileShare::ShareMode KFileShare::shareMode() {
  if ( s_authorization == NotInitialized )
      readConfig();

  return s_shareMode;
}

bool KFileShare::sharingEnabled() {
  if ( s_authorization == NotInitialized )
      readConfig();

  return s_sharingEnabled;
}

bool KFileShare::isRestricted() {
  if ( s_authorization == NotInitialized )
      readConfig();

  return s_restricted;
}

QString KFileShare::fileShareGroup() {
  if ( s_authorization == NotInitialized )
      readConfig();

  return s_fileShareGroup;
}


bool KFileShare::sambaEnabled() {
  if ( s_authorization == NotInitialized )
      readConfig();

  return s_sambaEnabled;
}

bool KFileShare::nfsEnabled() {
  if ( s_authorization == NotInitialized )
      readConfig();

  return s_nfsEnabled;
}


void KFileShare::readShareList()
{
    KFileSharePrivate::self();
    s_shareList->clear();

    QString exe = ::findExe( "filesharelist" );
    if (exe.isEmpty()) {
        s_authorization = ErrorNotFound;
        return;
    }
    QProcess proc;
    proc.start( exe, QStringList() );
    if ( !proc.waitForFinished() ) {
        kError() << "Can't run" << exe;
        s_authorization = ErrorNotFound;
        return;
    }

    // Reading code shamelessly stolen from khostname.cpp ;)
    while (!proc.atEnd()) {
        QString line = proc.readLine().trimmed();
        int length = line.length();
	if ( length > 0 )
	{
            if ( line[length-1] != '/' )
                line += '/';
            s_shareList->append(line);
            kDebug(7000) << "Shared dir:" << line;
        }
    }
}


bool KFileShare::isDirectoryShared( const QString& _path )
{
    if ( ! s_shareList.exists() )
        readShareList();

    QString path( _path );
    if ( path[path.length()-1] != '/' )
        path += '/';
    return s_shareList->contains( path );
}

KFileShare::Authorization KFileShare::authorization()
{
    // The app should do this on startup, but if it doesn't, let's do here.
    if ( s_authorization == NotInitialized )
        readConfig();
    return s_authorization;
}

bool KFileShare::setShared( const QString& path, bool shared )
{
    if (! KFileShare::sharingEnabled() ||
          KFileShare::shareMode() == Advanced)
       return false;

    kDebug(7000) << path << "," << shared;
    QString kdesu = ::findExe("kdesu");
    QString exe = ::findExe( "fileshareset" );
    if (kdesu.isEmpty() || exe.isEmpty())
        return false;

    QStringList args;
    args << "--" << exe;
    if ( shared )
        args << "--add";
    else
        args << "--remove";
    args << path ;
    int ec = QProcess::execute( kdesu, args ); // should be ok, the perl script terminates fast
    kDebug(7000) << "exitCode=" << ec;
    bool ok = !ec;
    switch (ec) {
        case 1:
          // User is not authorized
          break;
        case 3:
          // Called script with --add, but path was already shared before.
          // Result is nevertheless what the client wanted, so
          // this is alright.
          ok = true;
          break;
        case 4:
          // Invalid mount point
          break;
        case 5:
          // Called script with --remove, but path was not shared before.
          // Result is nevertheless what the client wanted, so
          // this is alright.
          ok = true;
          break;
        case 6:
          // There is no export method
          break;
        case 7:
          // file sharing is disabled
          break;
        case 8:
          // advanced sharing is enabled
          break;
        case 255:
          // Abitrary error
          break;
    }

    return ok;
}

//#include "kfileshare.moc"
#include "kfileshare_p.moc"