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