]>
git.saurik.com Git - apt.git/blob - methods/rsh.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: rsh.cc,v 1.6.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
6 RSH method - Transfer files via rsh compatible program
8 Written by Ben Collins <bcollins@debian.org>, Copyright (c) 2000
9 Licensed under the GNU General Public License v2 [no exception clauses]
11 ##################################################################### */
13 // Include Files /*{{{*/
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/fileutl.h>
18 #include <apt-pkg/hashes.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/acquire-method.h>
21 #include <apt-pkg/strutl.h>
37 unsigned long TimeOut
= 120;
38 Configuration::Item
const *RshOptions
= 0;
39 time_t RSHMethod::FailTime
= 0;
40 std::string
RSHMethod::FailFile
;
41 int RSHMethod::FailFd
= -1;
43 // RSHConn::RSHConn - Constructor /*{{{*/
44 // ---------------------------------------------------------------------
46 RSHConn::RSHConn(std::string
const &pProg
, URI Srv
) : Len(0), WriteFd(-1), ReadFd(-1),
47 ServerName(Srv
), Prog(pProg
), Process(-1) {
51 // RSHConn::RSHConn - Destructor /*{{{*/
52 // ---------------------------------------------------------------------
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. */
71 ExecWait(Process
,"",true);
77 // RSHConn::Open - Connect to a host /*{{{*/
78 // ---------------------------------------------------------------------
82 // Use the already open connection if possible.
86 if (Connect(ServerName
.Host
,ServerName
.Port
,ServerName
.User
) == false)
92 // RSHConn::Connect - Fire up rsh and connect /*{{{*/
93 // ---------------------------------------------------------------------
95 bool RSHConn::Connect(std::string Host
, unsigned int Port
, std::string User
)
100 if (asprintf (&PortStr
, "%d", Port
) == -1 || PortStr
== NULL
)
101 return _error
->Errno("asprintf", _("Failed"));
105 int Pipes
[4] = {-1,-1,-1,-1};
106 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
108 _error
->Errno("pipe",_("Failed to create IPC pipe to subprocess"));
109 for (int I
= 0; I
!= 4; I
++)
113 for (int I
= 0; I
!= 4; I
++)
114 SetCloseExec(Pipes
[I
],true);
116 Process
= ExecFork();
121 const char *Args
[400];
124 dup2(Pipes
[1],STDOUT_FILENO
);
125 dup2(Pipes
[2],STDIN_FILENO
);
127 // Probably should do
128 // dup2(open("/dev/null",O_RDONLY),STDERR_FILENO);
130 Args
[i
++] = Prog
.c_str();
132 // Insert user-supplied command line options
133 Configuration::Item
const *Opts
= RshOptions
;
137 for (; Opts
!= 0; Opts
= Opts
->Next
)
139 if (Opts
->Value
.empty() == true)
141 Args
[i
++] = Opts
->Value
.c_str();
145 if (User
.empty() == false) {
147 Args
[i
++] = User
.c_str();
149 if (PortStr
!= NULL
) {
153 if (Host
.empty() == false) {
154 Args
[i
++] = Host
.c_str();
156 Args
[i
++] = "/bin/sh";
158 execvp(Args
[0],(char **)Args
);
167 SetNonBlock(Pipes
[0],true);
168 SetNonBlock(Pipes
[3],true);
174 bool RSHConn::Connect(std::string Host
, std::string User
)
176 return Connect(Host
, 0, User
);
179 // RSHConn::ReadLine - Very simple buffered read with timeout /*{{{*/
180 // ---------------------------------------------------------------------
182 bool RSHConn::ReadLine(std::string
&Text
)
184 if (Process
== -1 || ReadFd
== -1)
188 while (Len
< sizeof(Buffer
))
190 // Scan the buffer for a new line
191 for (unsigned int I
= 0; I
!= Len
; I
++)
193 // Escape some special chars
198 if (Buffer
[I
] != '\n')
202 Text
= std::string(Buffer
,I
);
203 memmove(Buffer
,Buffer
+I
,Len
- I
);
208 // Wait for some data..
209 if (WaitFd(ReadFd
,false,TimeOut
) == false)
212 return _error
->Error(_("Connection timeout"));
216 int Res
= read(ReadFd
,Buffer
+ Len
,sizeof(Buffer
) - Len
);
219 _error
->Errno("read",_("Read error"));
226 return _error
->Error(_("A response overflowed the buffer."));
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. */
233 bool RSHConn::WriteMsg(std::string
&Text
,bool Sync
,const char *Fmt
,...)
238 // sprintf into a buffer
240 vsnprintf(Tmp
,sizeof(Tmp
),Fmt
,args
);
243 // concat to create the real msg
246 Msg
= std::string(Tmp
) + " 2> /dev/null || echo\n";
248 Msg
= std::string(Tmp
) + " 2> /dev/null\n";
251 const char *S
= Msg
.c_str();
252 unsigned long Len
= strlen(S
);
253 unsigned long Start
= 0;
256 if (WaitFd(WriteFd
,true,TimeOut
) == false)
260 return _error
->Error(_("Connection timeout"));
263 int Res
= write(WriteFd
,S
+ Start
,Len
);
266 _error
->Errno("write",_("Write error"));
276 return ReadLine(Text
);
280 // RSHConn::Size - Return the size of the file /*{{{*/
281 // ---------------------------------------------------------------------
282 /* Right now for successful transfer the file size must be known in
284 bool RSHConn::Size(const char *Path
,unsigned long long &Size
)
290 if (WriteMsg(Msg
,true,"find %s -follow -printf '%%s\\n'",Path
) == false)
293 // FIXME: Sense if the bad reply is due to a File Not Found.
296 Size
= strtoull(Msg
.c_str(),&End
,10);
297 if (End
== Msg
.c_str())
298 return _error
->Error(_("File not found"));
302 // RSHConn::ModTime - Get the modification time in UTC /*{{{*/
303 // ---------------------------------------------------------------------
305 bool RSHConn::ModTime(const char *Path
, time_t &Time
)
308 // Query the mod time
311 if (WriteMsg(Msg
,true,"TZ=UTC find %s -follow -printf '%%TY%%Tm%%Td%%TH%%TM%%TS\\n'",Path
) == false)
315 return FTPMDTMStrToTime(Msg
.c_str(), Time
);
318 // RSHConn::Get - Get a file /*{{{*/
319 // ---------------------------------------------------------------------
321 bool RSHConn::Get(const char *Path
,FileFd
&To
,unsigned long long Resume
,
322 Hashes
&Hash
,bool &Missing
, unsigned long long Size
)
326 // Round to a 2048 byte block
327 Resume
= Resume
- (Resume
% 2048);
329 if (To
.Truncate(Resume
) == false)
331 if (To
.Seek(0) == false)
335 if (Hash
.AddFD(To
,Resume
) == false) {
336 _error
->Errno("read",_("Problem hashing file"));
341 // FIXME: Detect file-not openable type errors.
343 if (WriteMsg(Jnk
,false,"dd if=%s bs=2048 skip=%u", Path
, Resume
/ 2048) == false)
347 unsigned long long MyLen
= Resume
;
348 unsigned char Buffer
[4096];
351 // Wait for some data..
352 if (WaitFd(ReadFd
,false,TimeOut
) == false)
355 return _error
->Error(_("Data socket timed out"));
359 int Res
= read(ReadFd
,Buffer
,sizeof(Buffer
));
363 return _error
->Error(_("Connection closed prematurely"));
374 Hash
.Add(Buffer
,Res
);
375 if (To
.Write(Buffer
,Res
) == false)
386 // RSHMethod::RSHMethod - Constructor /*{{{*/
387 // ---------------------------------------------------------------------
389 RSHMethod::RSHMethod(std::string
const &pProg
) : aptMethod(pProg
.c_str(),"1.0",SendConfig
), Prog(pProg
)
391 signal(SIGTERM
,SigTerm
);
392 signal(SIGINT
,SigTerm
);
397 // RSHMethod::Configuration - Handle a configuration message /*{{{*/
398 // ---------------------------------------------------------------------
399 bool RSHMethod::Configuration(std::string Message
)
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", "");
405 if (aptMethod::Configuration(Message
) == false)
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());
416 // RSHMethod::SigTerm - Clean up and timestamp the files on exit /*{{{*/
417 // ---------------------------------------------------------------------
419 void RSHMethod::SigTerm(int)
424 // Transfer the modification times
425 struct timeval times
[2];
426 times
[0].tv_sec
= FailTime
;
427 times
[1].tv_sec
= FailTime
;
428 times
[0].tv_usec
= times
[1].tv_usec
= 0;
429 utimes(FailFile
.c_str(), times
);
435 // RSHMethod::Fetch - Fetch a URI /*{{{*/
436 // ---------------------------------------------------------------------
438 bool RSHMethod::Fetch(FetchItem
*Itm
)
441 const char *File
= Get
.Path
.c_str();
443 Res
.Filename
= Itm
->DestFile
;
446 // Connect to the server
447 if (Server
== 0 || Server
->Comp(Get
) == false) {
449 Server
= new RSHConn(Prog
, Get
);
452 // Could not connect is a transient error..
453 if (Server
->Open() == false) {
459 // We say this mainly because the pause here is for the
460 // ssh connection that is still going
461 Status(_("Connecting to %s"), Get
.Host
.c_str());
463 // Get the files information
464 unsigned long long Size
;
465 if (Server
->Size(File
,Size
) == false ||
466 Server
->ModTime(File
,FailTime
) == false)
469 //_error->Error(_("File not found")); // Will be handled by Size
474 // See if it is an IMS hit
475 if (Itm
->LastModified
== FailTime
) {
482 // See if the file exists
484 if (stat(Itm
->DestFile
.c_str(),&Buf
) == 0) {
485 if (Size
== (unsigned long long)Buf
.st_size
&& FailTime
== Buf
.st_mtime
) {
486 Res
.Size
= Buf
.st_size
;
487 Res
.LastModified
= Buf
.st_mtime
;
488 Res
.ResumePoint
= Buf
.st_size
;
494 if (FailTime
== Buf
.st_mtime
&& Size
> (unsigned long long)Buf
.st_size
)
495 Res
.ResumePoint
= Buf
.st_size
;
499 Hashes
Hash(Itm
->ExpectedHashes
);
501 FileFd
Fd(Itm
->DestFile
,FileFd::WriteAny
);
502 if (_error
->PendingError() == true)
507 FailFile
= Itm
->DestFile
;
508 FailFile
.c_str(); // Make sure we don't do a malloc in the signal handler
512 if (Server
->Get(File
,Fd
,Res
.ResumePoint
,Hash
,Missing
,Res
.Size
) == false)
517 struct timeval times
[2];
518 times
[0].tv_sec
= FailTime
;
519 times
[1].tv_sec
= FailTime
;
520 times
[0].tv_usec
= times
[1].tv_usec
= 0;
521 utimes(FailFile
.c_str(), times
);
523 // If the file is missing we hard fail otherwise transient fail
530 Res
.Size
= Fd
.Size();
531 struct timeval times
[2];
532 times
[0].tv_sec
= FailTime
;
533 times
[1].tv_sec
= FailTime
;
534 times
[0].tv_usec
= times
[1].tv_usec
= 0;
535 utimes(Fd
.Name().c_str(), times
);
539 Res
.LastModified
= FailTime
;
540 Res
.TakeHashes(Hash
);
548 int main(int, const char *argv
[])
550 setlocale(LC_ALL
, "");
552 RSHMethod
Mth(flNotDir(argv
[0]));