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