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