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