kio_smb_mount.cpp 5.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*  This file is part of the KDE project

    Copyright (C) 2000 Alexander Neundorf <neundorf@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    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
Dirk Mueller's avatar
Dirk Mueller committed
17
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Dirk Mueller's avatar
Dirk Mueller committed
18
    Boston, MA 02110-1301, USA.
19
20
21
*/

#include "kio_smb.h"
22

23
#include <unistd.h>
Thorsten Roeder's avatar
Thorsten Roeder committed
24
#include <QByteArray>
25
#include <QDir>
26
#include <QDataStream>
27
#include <KLocalizedString>
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
28
#include <KProcess>
Laurent Montel's avatar
Laurent Montel committed
29
#include <kshell.h>
30

31
32
void SMBSlave::special( const QByteArray & data)
{
33
   qCDebug(KIO_SMB)<<"Smb::special()";
34
   int tmp;
35
   QDataStream stream(data);
36
37
38
39
40
41
42
   stream >> tmp;
   //mounting and umounting are both blocking, "guarded" by a SIGALARM in the future
   switch (tmp)
   {
   case 1:
   case 3:
      {
43
44
45
         QString remotePath, mountPoint, user;
         stream >> remotePath >> mountPoint;

John Layt's avatar
John Layt committed
46
         QStringList sl=remotePath.split('/');
47
48
49
         QString share,host;
         if (sl.count()>=2)
         {
50
51
            host=sl.at(0).mid(2);
            share=sl.at(1);
52
            qCDebug(KIO_SMB)<<"special() host -"<< host <<"- share -" << share <<"-";
53
54
         }

55
56
         remotePath.replace('\\', '/');  // smbmounterplugin sends \\host/share

57
         qCDebug(KIO_SMB) << "mounting: " << remotePath.toLocal8Bit() << " to " << mountPoint.toLocal8Bit();
58

59
         if (tmp==3) {
60
             if (!QDir().mkpath(mountPoint)) {
61
62
63
64
                 error(KIO::ERR_COULD_NOT_MKDIR, mountPoint);
                 return;
             }
         }
65

66
         SMBUrl smburl(QUrl("smb:///"));
67
         smburl.setHost(host);
Thorsten Roeder's avatar
Thorsten Roeder committed
68
         smburl.setPath('/' + share);
69

70
71
72
         const int passwordError = checkPassword(smburl);
         if (passwordError != KJob::NoError && passwordError != KIO::ERR_USER_CANCELED) {
           error(passwordError, smburl.toString());
73
74
75
76
77
           return;
         }

         // using smbmount instead of "mount -t smbfs", because mount does not allow a non-root
         // user to do a mount, but a suid smbmnt does allow this
78

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
79
80
         KProcess proc;
         proc.setOutputChannelMode(KProcess::SeparateChannels);
81
82
83
84
         proc << "smbmount";

         QString options;

85
         if ( smburl.userName().isEmpty() )
86
87
         {
           user = "guest";
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
88
           options = "guest";
89
90
91
         }
         else
         {
92
93
           options = "username=" + smburl.userName();
           user = smburl.userName();
94

95
96
           if ( ! smburl.password().isEmpty() )
             options += ",password=" + smburl.password();
97
98
99
100
         }

         // TODO: check why the control center uses encodings with a blank char, e.g. "cp 1250"
         //if ( ! m_default_encoding.isEmpty() )
Laurent Montel's avatar
Laurent Montel committed
101
           //options += ",codepage=" + KShell::quoteArg(m_default_encoding);
102

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
103
104
105
         proc << remotePath;
         proc << mountPoint;
         proc << "-o" << options;
106

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
107
108
         proc.start();
         if (!proc.waitForFinished())
109
         {
110
111
            error(KIO::ERR_CANNOT_LAUNCH_PROCESS,
                  "smbmount"+i18n("\nMake sure that the samba package is installed properly on your system."));
112
113
            return;
         }
114

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
115
116
117
         QString mybuf = QString::fromLocal8Bit(proc.readAllStandardOutput());
         QString mystderr = QString::fromLocal8Bit(proc.readAllStandardError());

118
         qCDebug(KIO_SMB) << "mount exit " << proc.exitCode()
119
120
                          << "stdout:" << mybuf << endl << "stderr:" << mystderr << endl;

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
121
         if (proc.exitCode() != 0)
122
123
         {
           error( KIO::ERR_COULD_NOT_MOUNT,
124
               i18n("Mounting of share \"%1\" from host \"%2\" by user \"%3\" failed.\n%4",
Thorsten Roeder's avatar
Thorsten Roeder committed
125
                share, host, user, mybuf + '\n' + mystderr));
126
           return;
127
         }
128

129
130
131
132
133
134
         finished();
      }
      break;
   case 2:
   case 4:
      {
135
         QString mountPoint;
136
         stream >> mountPoint;
137

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
138
139
         KProcess proc;
         proc.setOutputChannelMode(KProcess::SeparateChannels);
140
         proc << "smbumount";
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
141
         proc << mountPoint;
142

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
143
144
         proc.start();
         if ( !proc.waitForFinished() )
145
         {
146
147
148
149
150
           error(KIO::ERR_CANNOT_LAUNCH_PROCESS,
                 "smbumount"+i18n("\nMake sure that the samba package is installed properly on your system."));
           return;
         }

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
151
152
153
         QString mybuf = QString::fromLocal8Bit(proc.readAllStandardOutput());
         QString mystderr = QString::fromLocal8Bit(proc.readAllStandardError());

154
         qCDebug(KIO_SMB) << "smbumount exit " << proc.exitCode()
155
156
                          << "stdout:" << mybuf << endl << "stderr:" << mystderr << endl;

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
157
         if (proc.exitCode() != 0)
158
159
         {
           error(KIO::ERR_COULD_NOT_UNMOUNT,
160
               i18n("Unmounting of mountpoint \"%1\" failed.\n%2",
Thorsten Roeder's avatar
Thorsten Roeder committed
161
                mountPoint, mybuf + '\n' + mystderr));
162
           return;
163
         }
164
165

         if ( tmp == 4 )
166
         {
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
           bool ok;

           QDir dir(mountPoint);
           dir.cdUp();
           ok = dir.rmdir(mountPoint);
           if ( ok )
           {
             QString p=dir.path();
             dir.cdUp();
             ok = dir.rmdir(p);
           }

           if ( !ok )
           {
             error(KIO::ERR_COULD_NOT_RMDIR, mountPoint);
             return;
           }
         }

         finished();
187
188
189
190
191
192
193
      }
      break;
   default:
      break;
   }
   finished();
}