]> git.saurik.com Git - apt.git/blob - methods/rsh.cc
d249ae96102aa456e69faa60fb83ea26f43035d6
[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
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>
29 #include "rsh.h"
30
31 #include <apti18n.h>
32 /*}}}*/
33
34 const char *Prog;
35 unsigned long TimeOut = 120;
36 Configuration::Item const *RshOptions = 0;
37 time_t RSHMethod::FailTime = 0;
38 std::string RSHMethod::FailFile;
39 int RSHMethod::FailFd = -1;
40
41 // RSHConn::RSHConn - Constructor /*{{{*/
42 // ---------------------------------------------------------------------
43 /* */
44 RSHConn::RSHConn(URI Srv) : Len(0), WriteFd(-1), ReadFd(-1),
45 ServerName(Srv), Process(-1) {}
46 /*}}}*/
47 // RSHConn::RSHConn - Destructor /*{{{*/
48 // ---------------------------------------------------------------------
49 /* */
50 RSHConn::~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. */
59 void 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 /* */
76 bool 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 /* */
91 bool RSHConn::Connect(std::string Host, std::string User)
92 {
93 // Create the pipes
94 int Pipes[4] = {-1,-1,-1,-1};
95 if (pipe(Pipes) != 0 || pipe(Pipes+2) != 0)
96 {
97 _error->Errno("pipe",_("Failed to create IPC pipe to subprocess"));
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 {
110 const char *Args[400];
111 unsigned int i = 0;
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
119 Args[i++] = Prog;
120
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
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 /* */
160 bool RSHConn::ReadLine(std::string &Text)
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++;
180 Text = std::string(Buffer,I);
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();
190 return _error->Error(_("Connection timeout"));
191 }
192
193 // Suck it back
194 int Res = read(ReadFd,Buffer + Len,sizeof(Buffer) - Len);
195 if (Res <= 0)
196 {
197 _error->Errno("read",_("Read error"));
198 Close();
199 return false;
200 }
201 Len += Res;
202 }
203
204 return _error->Error(_("A response overflowed the buffer."));
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. */
211 bool RSHConn::WriteMsg(std::string &Text,bool Sync,const char *Fmt,...)
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();
233 return _error->Error(_("Connection timeout"));
234 }
235
236 int Res = write(WriteFd,S + Start,Len);
237 if (Res <= 0)
238 {
239 _error->Errno("write",_("Write error"));
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. */
257 bool RSHConn::Size(const char *Path,unsigned long long &Size)
258 {
259 // Query the size
260 std::string Msg;
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;
269 Size = strtoull(Msg.c_str(),&End,10);
270 if (End == Msg.c_str())
271 return _error->Error(_("File not found"));
272 return true;
273 }
274 /*}}}*/
275 // RSHConn::ModTime - Get the modification time in UTC /*{{{*/
276 // ---------------------------------------------------------------------
277 /* */
278 bool RSHConn::ModTime(const char *Path, time_t &Time)
279 {
280 Time = time(&Time);
281 // Query the mod time
282 std::string Msg;
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
288 return FTPMDTMStrToTime(Msg.c_str(), Time);
289 }
290 /*}}}*/
291 // RSHConn::Get - Get a file /*{{{*/
292 // ---------------------------------------------------------------------
293 /* */
294 bool RSHConn::Get(const char *Path,FileFd &To,unsigned long long Resume,
295 Hashes &Hash,bool &Missing, unsigned long long Size)
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) {
308 if (Hash.AddFD(To,Resume) == false) {
309 _error->Errno("read",_("Problem hashing file"));
310 return false;
311 }
312 }
313
314 // FIXME: Detect file-not openable type errors.
315 std::string Jnk;
316 if (WriteMsg(Jnk,false,"dd if=%s bs=2048 skip=%u", Path, Resume / 2048) == false)
317 return false;
318
319 // Copy loop
320 unsigned long long MyLen = Resume;
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();
328 return _error->Error(_("Data socket timed out"));
329 }
330
331 // Read the data..
332 int Res = read(ReadFd,Buffer,sizeof(Buffer));
333 if (Res == 0)
334 {
335 Close();
336 return _error->Error(_("Connection closed prematurely"));
337 }
338
339 if (Res < 0)
340 {
341 if (errno == EAGAIN)
342 continue;
343 break;
344 }
345 MyLen += Res;
346
347 Hash.Add(Buffer,Res);
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 /* */
362 RSHMethod::RSHMethod() : pkgAcqMethod("1.0",SendConfig)
363 {
364 signal(SIGTERM,SigTerm);
365 signal(SIGINT,SigTerm);
366 Server = 0;
367 FailFd = -1;
368 };
369 /*}}}*/
370 // RSHMethod::Configuration - Handle a configuration message /*{{{*/
371 // ---------------------------------------------------------------------
372 bool RSHMethod::Configuration(std::string Message)
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 /*}}}*/
387 // RSHMethod::SigTerm - Clean up and timestamp the files on exit /*{{{*/
388 // ---------------------------------------------------------------------
389 /* */
390 void 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 /* */
408 bool 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
431 Status(_("Connecting to %s"), Get.Host.c_str());
432
433 // Get the files information
434 unsigned long long Size;
435 if (Server->Size(File,Size) == false ||
436 Server->ModTime(File,FailTime) == false)
437 {
438 //Fail(true);
439 //_error->Error(_("File not found")); // Will be handled by Size
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) {
455 if (Size == (unsigned long long)Buf.st_size && FailTime == Buf.st_mtime) {
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?
464 if (FailTime == Buf.st_mtime && Size > (unsigned long long)Buf.st_size)
465 Res.ResumePoint = Buf.st_size;
466 }
467
468 // Open the file
469 Hashes Hash;
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;
482 if (Server->Get(File,Fd,Res.ResumePoint,Hash,Missing,Res.Size) == false)
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;
503 Res.TakeHashes(Hash);
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
518 int main(int argc, const char *argv[])
519 {
520 setlocale(LC_ALL, "");
521
522 RSHMethod Mth;
523 Prog = strrchr(argv[0],'/');
524 Prog++;
525 return Mth.Run();
526 }