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