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