]> git.saurik.com Git - apt.git/blame - methods/rsh.cc
fix and document on the fly compressor config
[apt.git] / methods / rsh.cc
CommitLineData
b2e465d6
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
7db98ffc 3// $Id: rsh.cc,v 1.6.2.1 2004/01/16 18:58:50 mdz Exp $
b2e465d6
AL
4/* ######################################################################
5
6 RSH method - Transfer files via rsh compatible program
7
8 Written by Ben Collins <bcollins@debian.org>, Copyright (c) 2000
9 Licensed under the GNU General Public License v2 [no exception clauses]
10
11 ##################################################################### */
12 /*}}}*/
5e775e59 13// Include Files /*{{{*/
ea542140
DK
14#include <config.h>
15
b2e465d6 16#include <apt-pkg/error.h>
472ff00e
DK
17#include <apt-pkg/fileutl.h>
18#include <apt-pkg/hashes.h>
19#include <apt-pkg/configuration.h>
453b82a3
DK
20#include <apt-pkg/acquire-method.h>
21#include <apt-pkg/strutl.h>
b2e465d6 22
453b82a3
DK
23#include <stdlib.h>
24#include <string.h>
b2e465d6
AL
25#include <sys/stat.h>
26#include <sys/time.h>
b2e465d6
AL
27#include <unistd.h>
28#include <signal.h>
29#include <stdio.h>
30#include <errno.h>
31#include <stdarg.h>
ea542140
DK
32#include "rsh.h"
33
d77559ac 34#include <apti18n.h>
b2e465d6
AL
35 /*}}}*/
36
b2e465d6 37unsigned long TimeOut = 120;
5e775e59 38Configuration::Item const *RshOptions = 0;
b2e465d6 39time_t RSHMethod::FailTime = 0;
8f3ba4e8 40std::string RSHMethod::FailFile;
b2e465d6
AL
41int RSHMethod::FailFd = -1;
42
43// RSHConn::RSHConn - Constructor /*{{{*/
44// ---------------------------------------------------------------------
45/* */
d29d27b5
DK
46RSHConn::RSHConn(std::string const &pProg, URI Srv) : Len(0), WriteFd(-1), ReadFd(-1),
47 ServerName(Srv), Prog(pProg), Process(-1) {
dcaa1185
DK
48 Buffer[0] = '\0';
49}
b2e465d6
AL
50 /*}}}*/
51// RSHConn::RSHConn - Destructor /*{{{*/
52// ---------------------------------------------------------------------
53/* */
54RSHConn::~RSHConn()
55{
56 Close();
57}
58 /*}}}*/
59// RSHConn::Close - Forcibly terminate the connection /*{{{*/
60// ---------------------------------------------------------------------
61/* Often this is called when things have gone wrong to indicate that the
62 connection is no longer usable. */
63void RSHConn::Close()
64{
65 if (Process == -1)
66 return;
67
68 close(WriteFd);
69 close(ReadFd);
70 kill(Process,SIGINT);
71 ExecWait(Process,"",true);
72 WriteFd = -1;
73 ReadFd = -1;
74 Process = -1;
75}
76 /*}}}*/
77// RSHConn::Open - Connect to a host /*{{{*/
78// ---------------------------------------------------------------------
79/* */
80bool RSHConn::Open()
81{
82 // Use the already open connection if possible.
83 if (Process != -1)
84 return true;
85
c7872a2c 86 if (Connect(ServerName.Host,ServerName.Port,ServerName.User) == false)
b2e465d6
AL
87 return false;
88
89 return true;
90}
91 /*}}}*/
92// RSHConn::Connect - Fire up rsh and connect /*{{{*/
93// ---------------------------------------------------------------------
94/* */
c7872a2c 95bool RSHConn::Connect(std::string Host, unsigned int Port, std::string User)
b2e465d6 96{
c7872a2c
DH
97 char *PortStr = NULL;
98 if (Port != 0)
99 {
100 if (asprintf (&PortStr, "%d", Port) == -1 || PortStr == NULL)
101 return _error->Errno("asprintf", _("Failed"));
102 }
103
b2e465d6
AL
104 // Create the pipes
105 int Pipes[4] = {-1,-1,-1,-1};
106 if (pipe(Pipes) != 0 || pipe(Pipes+2) != 0)
107 {
dc738e7a 108 _error->Errno("pipe",_("Failed to create IPC pipe to subprocess"));
b2e465d6
AL
109 for (int I = 0; I != 4; I++)
110 close(Pipes[I]);
111 return false;
112 }
113 for (int I = 0; I != 4; I++)
114 SetCloseExec(Pipes[I],true);
115
116 Process = ExecFork();
117
118 // The child
119 if (Process == 0)
120 {
5e775e59
AL
121 const char *Args[400];
122 unsigned int i = 0;
b2e465d6
AL
123
124 dup2(Pipes[1],STDOUT_FILENO);
125 dup2(Pipes[2],STDIN_FILENO);
126
127 // Probably should do
128 // dup2(open("/dev/null",O_RDONLY),STDERR_FILENO);
129
d29d27b5 130 Args[i++] = Prog.c_str();
c5734bad 131
5e775e59
AL
132 // Insert user-supplied command line options
133 Configuration::Item const *Opts = RshOptions;
134 if (Opts != 0)
135 {
136 Opts = Opts->Child;
137 for (; Opts != 0; Opts = Opts->Next)
138 {
139 if (Opts->Value.empty() == true)
140 continue;
141 Args[i++] = Opts->Value.c_str();
142 }
143 }
144
b2e465d6
AL
145 if (User.empty() == false) {
146 Args[i++] = "-l";
147 Args[i++] = User.c_str();
148 }
c7872a2c
DH
149 if (PortStr != NULL) {
150 Args[i++] = "-p";
151 Args[i++] = PortStr;
152 }
b2e465d6
AL
153 if (Host.empty() == false) {
154 Args[i++] = Host.c_str();
155 }
156 Args[i++] = "/bin/sh";
157 Args[i] = 0;
158 execvp(Args[0],(char **)Args);
159 exit(100);
160 }
161
c7872a2c
DH
162 if (PortStr != NULL)
163 free(PortStr);
164
b2e465d6
AL
165 ReadFd = Pipes[0];
166 WriteFd = Pipes[3];
167 SetNonBlock(Pipes[0],true);
168 SetNonBlock(Pipes[3],true);
169 close(Pipes[1]);
170 close(Pipes[2]);
171
172 return true;
c7872a2c
DH
173}
174bool RSHConn::Connect(std::string Host, std::string User)
175{
176 return Connect(Host, 0, User);
b2e465d6
AL
177}
178 /*}}}*/
179// RSHConn::ReadLine - Very simple buffered read with timeout /*{{{*/
180// ---------------------------------------------------------------------
181/* */
8f3ba4e8 182bool RSHConn::ReadLine(std::string &Text)
b2e465d6
AL
183{
184 if (Process == -1 || ReadFd == -1)
185 return false;
186
187 // Suck in a line
188 while (Len < sizeof(Buffer))
189 {
190 // Scan the buffer for a new line
191 for (unsigned int I = 0; I != Len; I++)
192 {
193 // Escape some special chars
194 if (Buffer[I] == 0)
195 Buffer[I] = '?';
196
197 // End of line?
198 if (Buffer[I] != '\n')
199 continue;
200
201 I++;
8f3ba4e8 202 Text = std::string(Buffer,I);
b2e465d6
AL
203 memmove(Buffer,Buffer+I,Len - I);
204 Len -= I;
205 return true;
206 }
207
208 // Wait for some data..
209 if (WaitFd(ReadFd,false,TimeOut) == false)
210 {
211 Close();
dc738e7a 212 return _error->Error(_("Connection timeout"));
b2e465d6
AL
213 }
214
215 // Suck it back
216 int Res = read(ReadFd,Buffer + Len,sizeof(Buffer) - Len);
217 if (Res <= 0)
218 {
dc738e7a 219 _error->Errno("read",_("Read error"));
b2e465d6
AL
220 Close();
221 return false;
222 }
223 Len += Res;
224 }
225
dc738e7a 226 return _error->Error(_("A response overflowed the buffer."));
b2e465d6
AL
227}
228 /*}}}*/
229// RSHConn::WriteMsg - Send a message with optional remote sync. /*{{{*/
230// ---------------------------------------------------------------------
231/* The remote sync flag appends a || echo which will insert blank line
232 once the command completes. */
8f3ba4e8 233bool RSHConn::WriteMsg(std::string &Text,bool Sync,const char *Fmt,...)
b2e465d6
AL
234{
235 va_list args;
236 va_start(args,Fmt);
237
180b6932
MV
238 // sprintf into a buffer
239 char Tmp[1024];
240 vsnprintf(Tmp,sizeof(Tmp),Fmt,args);
11d0fb91
MV
241 va_end(args);
242
180b6932
MV
243 // concat to create the real msg
244 std::string Msg;
b2e465d6 245 if (Sync == true)
180b6932 246 Msg = std::string(Tmp) + " 2> /dev/null || echo\n";
b2e465d6 247 else
180b6932 248 Msg = std::string(Tmp) + " 2> /dev/null\n";
b2e465d6
AL
249
250 // Send it off
180b6932 251 const char *S = Msg.c_str();
b2e465d6
AL
252 unsigned long Len = strlen(S);
253 unsigned long Start = 0;
254 while (Len != 0)
255 {
256 if (WaitFd(WriteFd,true,TimeOut) == false)
257 {
258
259 Close();
dc738e7a 260 return _error->Error(_("Connection timeout"));
b2e465d6
AL
261 }
262
263 int Res = write(WriteFd,S + Start,Len);
264 if (Res <= 0)
265 {
db0db9fe 266 _error->Errno("write",_("Write error"));
b2e465d6
AL
267 Close();
268 return false;
269 }
270
271 Len -= Res;
272 Start += Res;
273 }
274
275 if (Sync == true)
276 return ReadLine(Text);
277 return true;
278}
279 /*}}}*/
280// RSHConn::Size - Return the size of the file /*{{{*/
281// ---------------------------------------------------------------------
1e3f4083 282/* Right now for successful transfer the file size must be known in
b2e465d6 283 advance. */
650faab0 284bool RSHConn::Size(const char *Path,unsigned long long &Size)
b2e465d6
AL
285{
286 // Query the size
8f3ba4e8 287 std::string Msg;
b2e465d6
AL
288 Size = 0;
289
290 if (WriteMsg(Msg,true,"find %s -follow -printf '%%s\\n'",Path) == false)
291 return false;
292
293 // FIXME: Sense if the bad reply is due to a File Not Found.
294
295 char *End;
650faab0 296 Size = strtoull(Msg.c_str(),&End,10);
b2e465d6 297 if (End == Msg.c_str())
db0db9fe 298 return _error->Error(_("File not found"));
b2e465d6
AL
299 return true;
300}
301 /*}}}*/
302// RSHConn::ModTime - Get the modification time in UTC /*{{{*/
303// ---------------------------------------------------------------------
304/* */
305bool RSHConn::ModTime(const char *Path, time_t &Time)
306{
307 Time = time(&Time);
308 // Query the mod time
8f3ba4e8 309 std::string Msg;
b2e465d6
AL
310
311 if (WriteMsg(Msg,true,"TZ=UTC find %s -follow -printf '%%TY%%Tm%%Td%%TH%%TM%%TS\\n'",Path) == false)
312 return false;
313
314 // Parse it
96cc64a5 315 return FTPMDTMStrToTime(Msg.c_str(), Time);
b2e465d6
AL
316}
317 /*}}}*/
318// RSHConn::Get - Get a file /*{{{*/
319// ---------------------------------------------------------------------
320/* */
650faab0
DK
321bool RSHConn::Get(const char *Path,FileFd &To,unsigned long long Resume,
322 Hashes &Hash,bool &Missing, unsigned long long Size)
b2e465d6
AL
323{
324 Missing = false;
325
326 // Round to a 2048 byte block
327 Resume = Resume - (Resume % 2048);
328
329 if (To.Truncate(Resume) == false)
330 return false;
331 if (To.Seek(0) == false)
332 return false;
333
334 if (Resume != 0) {
109eb151 335 if (Hash.AddFD(To,Resume) == false) {
dc738e7a 336 _error->Errno("read",_("Problem hashing file"));
b2e465d6
AL
337 return false;
338 }
339 }
340
341 // FIXME: Detect file-not openable type errors.
8f3ba4e8 342 std::string Jnk;
b2e465d6
AL
343 if (WriteMsg(Jnk,false,"dd if=%s bs=2048 skip=%u", Path, Resume / 2048) == false)
344 return false;
345
346 // Copy loop
650faab0 347 unsigned long long MyLen = Resume;
b2e465d6
AL
348 unsigned char Buffer[4096];
349 while (MyLen < Size)
350 {
351 // Wait for some data..
352 if (WaitFd(ReadFd,false,TimeOut) == false)
353 {
354 Close();
dc738e7a 355 return _error->Error(_("Data socket timed out"));
b2e465d6
AL
356 }
357
358 // Read the data..
359 int Res = read(ReadFd,Buffer,sizeof(Buffer));
360 if (Res == 0)
361 {
362 Close();
dc738e7a 363 return _error->Error(_("Connection closed prematurely"));
b2e465d6
AL
364 }
365
366 if (Res < 0)
367 {
368 if (errno == EAGAIN)
369 continue;
370 break;
371 }
372 MyLen += Res;
373
63b1700f 374 Hash.Add(Buffer,Res);
b2e465d6
AL
375 if (To.Write(Buffer,Res) == false)
376 {
377 Close();
378 return false;
379 }
380 }
381
382 return true;
383}
384 /*}}}*/
385
386// RSHMethod::RSHMethod - Constructor /*{{{*/
387// ---------------------------------------------------------------------
388/* */
23e64f6d 389RSHMethod::RSHMethod(std::string const &pProg) : aptMethod(pProg.c_str(),"1.0",SendConfig), Prog(pProg)
b2e465d6
AL
390{
391 signal(SIGTERM,SigTerm);
392 signal(SIGINT,SigTerm);
393 Server = 0;
394 FailFd = -1;
d3e8fbb3 395}
b2e465d6 396 /*}}}*/
5e775e59
AL
397// RSHMethod::Configuration - Handle a configuration message /*{{{*/
398// ---------------------------------------------------------------------
8f3ba4e8 399bool RSHMethod::Configuration(std::string Message)
5e775e59 400{
25ff0797
DK
401 // enabling privilege dropping for this method requires configuration…
402 // … which is otherwise lifted straight from root, so use it by default.
403 _config->Set(std::string("Binary::") + Prog + "::APT::Sandbox::User", "");
404
23e64f6d 405 if (aptMethod::Configuration(Message) == false)
5e775e59
AL
406 return false;
407
23e64f6d
DK
408 std::string const timeconf = std::string("Acquire::") + Prog + "::Timeout";
409 TimeOut = _config->FindI(timeconf, TimeOut);
410 std::string const optsconf = std::string("Acquire::") + Prog + "::Options";
411 RshOptions = _config->Tree(optsconf.c_str());
5e775e59
AL
412
413 return true;
414}
415 /*}}}*/
b2e465d6
AL
416// RSHMethod::SigTerm - Clean up and timestamp the files on exit /*{{{*/
417// ---------------------------------------------------------------------
418/* */
65512241 419void RSHMethod::SigTerm(int)
b2e465d6
AL
420{
421 if (FailFd == -1)
422 _exit(100);
b2e465d6 423
9ce3cfc9 424 // Transfer the modification times
246bbb61 425 struct timeval times[2];
9ce3cfc9
DK
426 times[0].tv_sec = FailTime;
427 times[1].tv_sec = FailTime;
246bbb61
DK
428 times[0].tv_usec = times[1].tv_usec = 0;
429 utimes(FailFile.c_str(), times);
9ce3cfc9 430 close(FailFd);
b2e465d6
AL
431
432 _exit(100);
433}
434 /*}}}*/
435// RSHMethod::Fetch - Fetch a URI /*{{{*/
436// ---------------------------------------------------------------------
437/* */
438bool RSHMethod::Fetch(FetchItem *Itm)
439{
440 URI Get = Itm->Uri;
441 const char *File = Get.Path.c_str();
442 FetchResult Res;
443 Res.Filename = Itm->DestFile;
444 Res.IMSHit = false;
445
446 // Connect to the server
447 if (Server == 0 || Server->Comp(Get) == false) {
448 delete Server;
d29d27b5 449 Server = new RSHConn(Prog, Get);
b2e465d6
AL
450 }
451
452 // Could not connect is a transient error..
453 if (Server->Open() == false) {
454 Server->Close();
455 Fail(true);
456 return true;
457 }
458
459 // We say this mainly because the pause here is for the
460 // ssh connection that is still going
dc738e7a 461 Status(_("Connecting to %s"), Get.Host.c_str());
b2e465d6
AL
462
463 // Get the files information
650faab0 464 unsigned long long Size;
b2e465d6
AL
465 if (Server->Size(File,Size) == false ||
466 Server->ModTime(File,FailTime) == false)
467 {
468 //Fail(true);
db0db9fe 469 //_error->Error(_("File not found")); // Will be handled by Size
b2e465d6
AL
470 return false;
471 }
472 Res.Size = Size;
473
474 // See if it is an IMS hit
475 if (Itm->LastModified == FailTime) {
476 Res.Size = 0;
477 Res.IMSHit = true;
478 URIDone(Res);
479 return true;
480 }
481
482 // See if the file exists
483 struct stat Buf;
484 if (stat(Itm->DestFile.c_str(),&Buf) == 0) {
650faab0 485 if (Size == (unsigned long long)Buf.st_size && FailTime == Buf.st_mtime) {
b2e465d6
AL
486 Res.Size = Buf.st_size;
487 Res.LastModified = Buf.st_mtime;
488 Res.ResumePoint = Buf.st_size;
489 URIDone(Res);
490 return true;
491 }
492
493 // Resume?
650faab0 494 if (FailTime == Buf.st_mtime && Size > (unsigned long long)Buf.st_size)
b2e465d6
AL
495 Res.ResumePoint = Buf.st_size;
496 }
497
498 // Open the file
9224ce3d 499 Hashes Hash(Itm->ExpectedHashes);
b2e465d6
AL
500 {
501 FileFd Fd(Itm->DestFile,FileFd::WriteAny);
502 if (_error->PendingError() == true)
503 return false;
504
505 URIStart(Res);
506
507 FailFile = Itm->DestFile;
3a8776a3 508 FailFile.c_str(); // Make sure we don't do a malloc in the signal handler
b2e465d6
AL
509 FailFd = Fd.Fd();
510
511 bool Missing;
63b1700f 512 if (Server->Get(File,Fd,Res.ResumePoint,Hash,Missing,Res.Size) == false)
b2e465d6
AL
513 {
514 Fd.Close();
515
516 // Timestamp
246bbb61 517 struct timeval times[2];
9ce3cfc9
DK
518 times[0].tv_sec = FailTime;
519 times[1].tv_sec = FailTime;
246bbb61
DK
520 times[0].tv_usec = times[1].tv_usec = 0;
521 utimes(FailFile.c_str(), times);
b2e465d6
AL
522
523 // If the file is missing we hard fail otherwise transient fail
524 if (Missing == true)
525 return false;
526 Fail(true);
527 return true;
528 }
529
530 Res.Size = Fd.Size();
246bbb61 531 struct timeval times[2];
9ce3cfc9
DK
532 times[0].tv_sec = FailTime;
533 times[1].tv_sec = FailTime;
246bbb61
DK
534 times[0].tv_usec = times[1].tv_usec = 0;
535 utimes(Fd.Name().c_str(), times);
9ce3cfc9 536 FailFd = -1;
b2e465d6
AL
537 }
538
539 Res.LastModified = FailTime;
a7c835af 540 Res.TakeHashes(Hash);
b2e465d6 541
b2e465d6
AL
542 URIDone(Res);
543
544 return true;
545}
546 /*}}}*/
547
65512241 548int main(int, const char *argv[])
b2e465d6 549{
b25423f6
MZ
550 setlocale(LC_ALL, "");
551
23e64f6d 552 RSHMethod Mth(flNotDir(argv[0]));
b2e465d6
AL
553 return Mth.Run();
554}