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