summaryrefslogtreecommitdiffstats
path: root/kioslave/smb/kio_smb_mount.cpp
blob: f9c366235fd2aebef022fc7158f14297c9a82f03 (plain)
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
/*  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
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kio_smb.h"
#include <kstandarddirs.h>
#include <tqcstring.h>
#include <unistd.h>
#include <tqdir.h>
#include <kprocess.h>

void SMBSlave::readOutput(KProcess *, char *buffer, int buflen)
{
    mybuf += TQString::fromLocal8Bit(buffer, buflen);
}

void SMBSlave::readStdErr(KProcess *, char *buffer, int buflen)
{
    mystderr += TQString::fromLocal8Bit(buffer, buflen);
}

void SMBSlave::special( const TQByteArray & data)
{
   kdDebug(KIO_SMB)<<"Smb::special()"<<endl;
   int tmp;
   TQDataStream stream(data, IO_ReadOnly);
   stream >> tmp;
   //mounting and umounting are both blocking, "guarded" by a SIGALARM in the future
   switch (tmp)
   {
   case 1:
   case 3:
      {
         TQString remotePath, mountPoint, user;
         stream >> remotePath >> mountPoint;

         TQStringList sl=TQStringList::split("/",remotePath);
         TQString share,host;
         if (sl.count()>=2)
         {
            host=(*sl.at(0)).mid(2);
            share=(*sl.at(1));
            kdDebug(KIO_SMB)<<"special() host -"<< host <<"- share -" << share <<"-"<<endl;
         }

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

         kdDebug(KIO_SMB) << "mounting: " << remotePath.local8Bit() << " to " << mountPoint.local8Bit() << endl;

         if (tmp==3) {
             if (!KStandardDirs::makeDir(mountPoint)) {
                 error(KIO::ERR_COULD_NOT_MKDIR, mountPoint);
                 return;
             }
         }
         mybuf.truncate(0);
         mystderr.truncate(0);

         SMBUrl smburl("smb:///");
         smburl.setHost(host);
         smburl.setPath("/" + share);

         if ( !checkPassword(smburl) )
         {
           finished();
           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

         KProcess proc;
         proc.setUseShell(true);  // to have the path to smbmnt (which is used by smbmount); see man smbmount
         proc << "smbmount";

         TQString options;

         if ( smburl.user().isEmpty() )
         {
           user = "guest";
           options = "-o guest";
         }
         else
         {
           options = "-o username=" + KProcess::quote(smburl.user());
           user = smburl.user();

           if ( ! smburl.pass().isEmpty() )
             options += ",password=" + KProcess::quote(smburl.pass());
         }

         // TODO: check why the control center uses encodings with a blank char, e.g. "cp 1250"
         //if ( ! m_default_encoding.isEmpty() )
           //options += ",codepage=" + KProcess::quote(m_default_encoding);

         proc << KProcess::quote(remotePath.local8Bit());
         proc << KProcess::quote(mountPoint.local8Bit());
         proc << options;

         connect(&proc, TQT_SIGNAL( receivedStdout(KProcess *, char *, int )),
                 TQT_SLOT(readOutput(KProcess *, char *, int)));

         connect(&proc, TQT_SIGNAL( receivedStderr(KProcess *, char *, int )),
                 TQT_SLOT(readStdErr(KProcess *, char *, int)));

         if (!proc.start( KProcess::Block, KProcess::AllOutput ))
         {
            error(KIO::ERR_CANNOT_LAUNCH_PROCESS,
                  "smbmount"+i18n("\nMake sure that the samba package is installed properly on your system."));
            return;
         }

         kdDebug(KIO_SMB) << "mount exit " << proc.exitStatus() << endl
                          << "stdout:" << mybuf << endl << "stderr:" << mystderr << endl;

         if (proc.exitStatus() != 0)
         {
           error( KIO::ERR_COULD_NOT_MOUNT,
               i18n("Mounting of share \"%1\" from host \"%2\" by user \"%3\" failed.\n%4")
               .arg(share).arg(host).arg(user).arg(mybuf + "\n" + mystderr));
           return;
         }

         finished();
      }
      break;
   case 2:
   case 4:
      {
         TQString mountPoint;
         stream >> mountPoint;

         KProcess proc;
         proc.setUseShell(true);
         proc << "smbumount";
         proc << KProcess::quote(mountPoint);

         mybuf.truncate(0);
         mystderr.truncate(0);

         connect(&proc, TQT_SIGNAL( receivedStdout(KProcess *, char *, int )),
                 TQT_SLOT(readOutput(KProcess *, char *, int)));

         connect(&proc, TQT_SIGNAL( receivedStderr(KProcess *, char *, int )),
                 TQT_SLOT(readStdErr(KProcess *, char *, int)));

         if ( !proc.start( KProcess::Block, KProcess::AllOutput ) )
         {
           error(KIO::ERR_CANNOT_LAUNCH_PROCESS,
                 "smbumount"+i18n("\nMake sure that the samba package is installed properly on your system."));
           return;
         }

         kdDebug(KIO_SMB) << "smbumount exit " << proc.exitStatus() << endl
                          << "stdout:" << mybuf << endl << "stderr:" << mystderr << endl;

         if (proc.exitStatus() != 0)
         {
           error(KIO::ERR_COULD_NOT_UNMOUNT,
               i18n("Unmounting of mountpoint \"%1\" failed.\n%2")
               .arg(mountPoint).arg(mybuf + "\n" + mystderr));
           return;
         }

         if ( tmp == 4 )
         {
           bool ok;

           TQDir dir(mountPoint);
           dir.cdUp();
           ok = dir.rmdir(mountPoint);
           if ( ok )
           {
             TQString p=dir.path();
             dir.cdUp();
             ok = dir.rmdir(p);
           }

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

         finished();
      }
      break;
   default:
      break;
   }
   finished();
}

#include "kio_smb.moc"