]>
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/strutl.h>
36 unsigned long TimeOut
= 120;
37 Configuration::Item
const *RshOptions
= 0;
38 time_t RSHMethod::FailTime
= 0;
39 std::string
RSHMethod::FailFile
;
40 int RSHMethod::FailFd
= -1;
42 // RSHConn::RSHConn - Constructor /*{{{*/
43 // ---------------------------------------------------------------------
45 RSHConn::RSHConn(std::string
const &pProg
, URI Srv
) : Len(0), WriteFd(-1), ReadFd(-1),
46 ServerName(Srv
), Prog(pProg
), Process(-1) {
50 // RSHConn::RSHConn - Destructor /*{{{*/
51 // ---------------------------------------------------------------------
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. */
70 ExecWait(Process
,"",true);
76 // RSHConn::Open - Connect to a host /*{{{*/
77 // ---------------------------------------------------------------------
81 // Use the already open connection if possible.
85 if (Connect(ServerName
.Host
,ServerName
.Port
,ServerName
.User
) == false)
91 // RSHConn::Connect - Fire up rsh and connect /*{{{*/
92 // ---------------------------------------------------------------------
94 bool RSHConn::Connect(std::string Host
, unsigned int Port
, std::string User
)
99 if (asprintf (&PortStr
, "%d", Port
) == -1 || PortStr
== NULL
)
100 return _error
->Errno("asprintf", _("Failed"));
104 int Pipes
[4] = {-1,-1,-1,-1};
105 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
107 _error
->Errno("pipe",_("Failed to create IPC pipe to subprocess"));
108 for (int I
= 0; I
!= 4; I
++)
112 for (int I
= 0; I
!= 4; I
++)
113 SetCloseExec(Pipes
[I
],true);
115 Process
= ExecFork();
120 const char *Args
[400];
123 dup2(Pipes
[1],STDOUT_FILENO
);
124 dup2(Pipes
[2],STDIN_FILENO
);
126 // Probably should do
127 // dup2(open("/dev/null",O_RDONLY),STDERR_FILENO);
129 Args
[i
++] = Prog
.c_str();
131 // Insert user-supplied command line options
132 Configuration::Item
const *Opts
= RshOptions
;
136 for (; Opts
!= 0; Opts
= Opts
->Next
)
138 if (Opts
->Value
.empty() == true)
140 Args
[i
++] = Opts
->Value
.c_str();
144 if (User
.empty() == false) {
146 Args
[i
++] = User
.c_str();
148 if (PortStr
!= NULL
) {
152 if (Host
.empty() == false) {
153 Args
[i
++] = Host
.c_str();
155 Args
[i
++] = "/bin/sh";
157 execvp(Args
[0],(char **)Args
);
166 SetNonBlock(Pipes
[0],true);
167 SetNonBlock(Pipes
[3],true);
173 bool RSHConn::Connect(std::string Host
, std::string User
)
175 return Connect(Host
, 0, User
);
178 // RSHConn::ReadLine - Very simple buffered read with timeout /*{{{*/
179 // ---------------------------------------------------------------------
181 bool RSHConn::ReadLine(std::string
&Text
)
183 if (Process
== -1 || ReadFd
== -1)
187 while (Len
< sizeof(Buffer
))
189 // Scan the buffer for a new line
190 for (unsigned int I
= 0; I
!= Len
; I
++)
192 // Escape some special chars
197 if (Buffer
[I
] != '\n')
201 Text
= std::string(Buffer
,I
);
202 memmove(Buffer
,Buffer
+I
,Len
- I
);
207 // Wait for some data..
208 if (WaitFd(ReadFd
,false,TimeOut
) == false)
211 return _error
->Error(_("Connection timeout"));
215 int Res
= read(ReadFd
,Buffer
+ Len
,sizeof(Buffer
) - Len
);
218 _error
->Errno("read",_("Read error"));
225 return _error
->Error(_("A response overflowed the buffer."));
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. */
232 bool RSHConn::WriteMsg(std::string
&Text
,bool Sync
,const char *Fmt
,...)
237 // sprintf into a buffer
239 vsnprintf(Tmp
,sizeof(Tmp
),Fmt
,args
);
242 // concat to create the real msg
245 Msg
= std::string(Tmp
) + " 2> /dev/null || echo\n";
247 Msg
= std::string(Tmp
) + " 2> /dev/null\n";
250 const char *S
= Msg
.c_str();
251 unsigned long Len
= strlen(S
);
252 unsigned long Start
= 0;
255 if (WaitFd(WriteFd
,true,TimeOut
) == false)
259 return _error
->Error(_("Connection timeout"));
262 int Res
= write(WriteFd
,S
+ Start
,Len
);
265 _error
->Errno("write",_("Write error"));
275 return ReadLine(Text
);
279 // RSHConn::Size - Return the size of the file /*{{{*/
280 // ---------------------------------------------------------------------
281 /* Right now for successful transfer the file size must be known in
283 bool RSHConn::Size(const char *Path
,unsigned long long &Size
)
289 if (WriteMsg(Msg
,true,"find %s -follow -printf '%%s\\n'",Path
) == false)
292 // FIXME: Sense if the bad reply is due to a File Not Found.
295 Size
= strtoull(Msg
.c_str(),&End
,10);
296 if (End
== Msg
.c_str())
297 return _error
->Error(_("File not found"));
301 // RSHConn::ModTime - Get the modification time in UTC /*{{{*/
302 // ---------------------------------------------------------------------
304 bool RSHConn::ModTime(const char *Path
, time_t &Time
)
307 // Query the mod time
310 if (WriteMsg(Msg
,true,"TZ=UTC find %s -follow -printf '%%TY%%Tm%%Td%%TH%%TM%%TS\\n'",Path
) == false)
314 return FTPMDTMStrToTime(Msg
.c_str(), Time
);
317 // RSHConn::Get - Get a file /*{{{*/
318 // ---------------------------------------------------------------------
320 bool RSHConn::Get(const char *Path
,FileFd
&To
,unsigned long long Resume
,
321 Hashes
&Hash
,bool &Missing
, unsigned long long Size
)
325 // Round to a 2048 byte block
326 Resume
= Resume
- (Resume
% 2048);
328 if (To
.Truncate(Resume
) == false)
330 if (To
.Seek(0) == false)
334 if (Hash
.AddFD(To
,Resume
) == false) {
335 _error
->Errno("read",_("Problem hashing file"));
340 // FIXME: Detect file-not openable type errors.
342 if (WriteMsg(Jnk
,false,"dd if=%s bs=2048 skip=%u", Path
, Resume
/ 2048) == false)
346 unsigned long long MyLen
= Resume
;
347 unsigned char Buffer
[4096];
350 // Wait for some data..
351 if (WaitFd(ReadFd
,false,TimeOut
) == false)
354 return _error
->Error(_("Data socket timed out"));
358 int Res
= read(ReadFd
,Buffer
,sizeof(Buffer
));
362 return _error
->Error(_("Connection closed prematurely"));
373 Hash
.Add(Buffer
,Res
);
374 if (To
.Write(Buffer
,Res
) == false)
385 // RSHMethod::RSHMethod - Constructor /*{{{*/
386 // ---------------------------------------------------------------------
388 RSHMethod::RSHMethod(std::string
const &pProg
) : aptMethod(pProg
.c_str(),"1.0",SendConfig
), Prog(pProg
)
390 signal(SIGTERM
,SigTerm
);
391 signal(SIGINT
,SigTerm
);
396 // RSHMethod::Configuration - Handle a configuration message /*{{{*/
397 // ---------------------------------------------------------------------
398 bool RSHMethod::Configuration(std::string Message
)
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", "");
404 if (aptMethod::Configuration(Message
) == false)
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());
415 // RSHMethod::SigTerm - Clean up and timestamp the files on exit /*{{{*/
416 // ---------------------------------------------------------------------
418 void RSHMethod::SigTerm(int)
423 // Transfer the modification times
424 struct timeval times
[2];
425 times
[0].tv_sec
= FailTime
;
426 times
[1].tv_sec
= FailTime
;
427 times
[0].tv_usec
= times
[1].tv_usec
= 0;
428 utimes(FailFile
.c_str(), times
);
434 // RSHMethod::Fetch - Fetch a URI /*{{{*/
435 // ---------------------------------------------------------------------
437 bool RSHMethod::Fetch(FetchItem
*Itm
)
440 const char *File
= Get
.Path
.c_str();
442 Res
.Filename
= Itm
->DestFile
;
445 // Connect to the server
446 if (Server
== 0 || Server
->Comp(Get
) == false) {
448 Server
= new RSHConn(Prog
, Get
);
451 // Could not connect is a transient error..
452 if (Server
->Open() == false) {
458 // We say this mainly because the pause here is for the
459 // ssh connection that is still going
460 Status(_("Connecting to %s"), Get
.Host
.c_str());
462 // Get the files information
463 unsigned long long Size
;
464 if (Server
->Size(File
,Size
) == false ||
465 Server
->ModTime(File
,FailTime
) == false)
468 //_error->Error(_("File not found")); // Will be handled by Size
473 // See if it is an IMS hit
474 if (Itm
->LastModified
== FailTime
) {
481 // See if the file exists
483 if (stat(Itm
->DestFile
.c_str(),&Buf
) == 0) {
484 if (Size
== (unsigned long long)Buf
.st_size
&& FailTime
== Buf
.st_mtime
) {
485 Res
.Size
= Buf
.st_size
;
486 Res
.LastModified
= Buf
.st_mtime
;
487 Res
.ResumePoint
= Buf
.st_size
;
493 if (FailTime
== Buf
.st_mtime
&& Size
> (unsigned long long)Buf
.st_size
)
494 Res
.ResumePoint
= Buf
.st_size
;
498 Hashes
Hash(Itm
->ExpectedHashes
);
500 FileFd
Fd(Itm
->DestFile
,FileFd::WriteAny
);
501 if (_error
->PendingError() == true)
506 FailFile
= Itm
->DestFile
;
507 FailFile
.c_str(); // Make sure we don't do a malloc in the signal handler
511 if (Server
->Get(File
,Fd
,Res
.ResumePoint
,Hash
,Missing
,Res
.Size
) == false)
516 struct timeval times
[2];
517 times
[0].tv_sec
= FailTime
;
518 times
[1].tv_sec
= FailTime
;
519 times
[0].tv_usec
= times
[1].tv_usec
= 0;
520 utimes(FailFile
.c_str(), times
);
522 // If the file is missing we hard fail otherwise transient fail
529 Res
.Size
= Fd
.Size();
530 struct timeval times
[2];
531 times
[0].tv_sec
= FailTime
;
532 times
[1].tv_sec
= FailTime
;
533 times
[0].tv_usec
= times
[1].tv_usec
= 0;
534 utimes(Fd
.Name().c_str(), times
);
538 Res
.LastModified
= FailTime
;
539 Res
.TakeHashes(Hash
);
547 int main(int, const char *argv
[])
549 return RSHMethod(flNotDir(argv
[0])).Run();