]>
Commit | Line | Data |
---|---|---|
b2e465d6 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
7db98ffc | 3 | // $Id: rsh.cc,v 1.6.2.1 2004/01/16 18:58:50 mdz Exp $ |
b2e465d6 AL |
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 | /*}}}*/ | |
5e775e59 | 13 | // Include Files /*{{{*/ |
ea542140 DK |
14 | #include <config.h> |
15 | ||
b2e465d6 | 16 | #include <apt-pkg/error.h> |
472ff00e DK |
17 | #include <apt-pkg/fileutl.h> |
18 | #include <apt-pkg/hashes.h> | |
19 | #include <apt-pkg/configuration.h> | |
453b82a3 DK |
20 | #include <apt-pkg/acquire-method.h> |
21 | #include <apt-pkg/strutl.h> | |
b2e465d6 | 22 | |
453b82a3 DK |
23 | #include <stdlib.h> |
24 | #include <string.h> | |
b2e465d6 AL |
25 | #include <sys/stat.h> |
26 | #include <sys/time.h> | |
b2e465d6 AL |
27 | #include <unistd.h> |
28 | #include <signal.h> | |
29 | #include <stdio.h> | |
30 | #include <errno.h> | |
31 | #include <stdarg.h> | |
ea542140 DK |
32 | #include "rsh.h" |
33 | ||
d77559ac | 34 | #include <apti18n.h> |
b2e465d6 AL |
35 | /*}}}*/ |
36 | ||
37 | const char *Prog; | |
38 | unsigned long TimeOut = 120; | |
5e775e59 | 39 | Configuration::Item const *RshOptions = 0; |
b2e465d6 | 40 | time_t RSHMethod::FailTime = 0; |
8f3ba4e8 | 41 | std::string RSHMethod::FailFile; |
b2e465d6 AL |
42 | int RSHMethod::FailFd = -1; |
43 | ||
44 | // RSHConn::RSHConn - Constructor /*{{{*/ | |
45 | // --------------------------------------------------------------------- | |
46 | /* */ | |
47 | RSHConn::RSHConn(URI Srv) : Len(0), WriteFd(-1), ReadFd(-1), | |
dcaa1185 DK |
48 | ServerName(Srv), Process(-1) { |
49 | Buffer[0] = '\0'; | |
50 | } | |
b2e465d6 AL |
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 | /* */ | |
8f3ba4e8 | 96 | bool RSHConn::Connect(std::string Host, std::string User) |
b2e465d6 AL |
97 | { |
98 | // Create the pipes | |
99 | int Pipes[4] = {-1,-1,-1,-1}; | |
100 | if (pipe(Pipes) != 0 || pipe(Pipes+2) != 0) | |
101 | { | |
dc738e7a | 102 | _error->Errno("pipe",_("Failed to create IPC pipe to subprocess")); |
b2e465d6 AL |
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 | { | |
5e775e59 AL |
115 | const char *Args[400]; |
116 | unsigned int i = 0; | |
b2e465d6 AL |
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 | ||
c5734bad MV |
124 | Args[i++] = Prog; |
125 | ||
5e775e59 AL |
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 | ||
b2e465d6 AL |
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 | /* */ | |
8f3ba4e8 | 165 | bool RSHConn::ReadLine(std::string &Text) |
b2e465d6 AL |
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++; | |
8f3ba4e8 | 185 | Text = std::string(Buffer,I); |
b2e465d6 AL |
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(); | |
dc738e7a | 195 | return _error->Error(_("Connection timeout")); |
b2e465d6 AL |
196 | } |
197 | ||
198 | // Suck it back | |
199 | int Res = read(ReadFd,Buffer + Len,sizeof(Buffer) - Len); | |
200 | if (Res <= 0) | |
201 | { | |
dc738e7a | 202 | _error->Errno("read",_("Read error")); |
b2e465d6 AL |
203 | Close(); |
204 | return false; | |
205 | } | |
206 | Len += Res; | |
207 | } | |
208 | ||
dc738e7a | 209 | return _error->Error(_("A response overflowed the buffer.")); |
b2e465d6 AL |
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. */ | |
8f3ba4e8 | 216 | bool RSHConn::WriteMsg(std::string &Text,bool Sync,const char *Fmt,...) |
b2e465d6 AL |
217 | { |
218 | va_list args; | |
219 | va_start(args,Fmt); | |
220 | ||
180b6932 MV |
221 | // sprintf into a buffer |
222 | char Tmp[1024]; | |
223 | vsnprintf(Tmp,sizeof(Tmp),Fmt,args); | |
11d0fb91 MV |
224 | va_end(args); |
225 | ||
180b6932 MV |
226 | // concat to create the real msg |
227 | std::string Msg; | |
b2e465d6 | 228 | if (Sync == true) |
180b6932 | 229 | Msg = std::string(Tmp) + " 2> /dev/null || echo\n"; |
b2e465d6 | 230 | else |
180b6932 | 231 | Msg = std::string(Tmp) + " 2> /dev/null\n"; |
b2e465d6 AL |
232 | |
233 | // Send it off | |
180b6932 | 234 | const char *S = Msg.c_str(); |
b2e465d6 AL |
235 | unsigned long Len = strlen(S); |
236 | unsigned long Start = 0; | |
237 | while (Len != 0) | |
238 | { | |
239 | if (WaitFd(WriteFd,true,TimeOut) == false) | |
240 | { | |
241 | ||
242 | Close(); | |
dc738e7a | 243 | return _error->Error(_("Connection timeout")); |
b2e465d6 AL |
244 | } |
245 | ||
246 | int Res = write(WriteFd,S + Start,Len); | |
247 | if (Res <= 0) | |
248 | { | |
db0db9fe | 249 | _error->Errno("write",_("Write error")); |
b2e465d6 AL |
250 | Close(); |
251 | return false; | |
252 | } | |
253 | ||
254 | Len -= Res; | |
255 | Start += Res; | |
256 | } | |
257 | ||
258 | if (Sync == true) | |
259 | return ReadLine(Text); | |
260 | return true; | |
261 | } | |
262 | /*}}}*/ | |
263 | // RSHConn::Size - Return the size of the file /*{{{*/ | |
264 | // --------------------------------------------------------------------- | |
1e3f4083 | 265 | /* Right now for successful transfer the file size must be known in |
b2e465d6 | 266 | advance. */ |
650faab0 | 267 | bool RSHConn::Size(const char *Path,unsigned long long &Size) |
b2e465d6 AL |
268 | { |
269 | // Query the size | |
8f3ba4e8 | 270 | std::string Msg; |
b2e465d6 AL |
271 | Size = 0; |
272 | ||
273 | if (WriteMsg(Msg,true,"find %s -follow -printf '%%s\\n'",Path) == false) | |
274 | return false; | |
275 | ||
276 | // FIXME: Sense if the bad reply is due to a File Not Found. | |
277 | ||
278 | char *End; | |
650faab0 | 279 | Size = strtoull(Msg.c_str(),&End,10); |
b2e465d6 | 280 | if (End == Msg.c_str()) |
db0db9fe | 281 | return _error->Error(_("File not found")); |
b2e465d6 AL |
282 | return true; |
283 | } | |
284 | /*}}}*/ | |
285 | // RSHConn::ModTime - Get the modification time in UTC /*{{{*/ | |
286 | // --------------------------------------------------------------------- | |
287 | /* */ | |
288 | bool RSHConn::ModTime(const char *Path, time_t &Time) | |
289 | { | |
290 | Time = time(&Time); | |
291 | // Query the mod time | |
8f3ba4e8 | 292 | std::string Msg; |
b2e465d6 AL |
293 | |
294 | if (WriteMsg(Msg,true,"TZ=UTC find %s -follow -printf '%%TY%%Tm%%Td%%TH%%TM%%TS\\n'",Path) == false) | |
295 | return false; | |
296 | ||
297 | // Parse it | |
96cc64a5 | 298 | return FTPMDTMStrToTime(Msg.c_str(), Time); |
b2e465d6 AL |
299 | } |
300 | /*}}}*/ | |
301 | // RSHConn::Get - Get a file /*{{{*/ | |
302 | // --------------------------------------------------------------------- | |
303 | /* */ | |
650faab0 DK |
304 | bool RSHConn::Get(const char *Path,FileFd &To,unsigned long long Resume, |
305 | Hashes &Hash,bool &Missing, unsigned long long Size) | |
b2e465d6 AL |
306 | { |
307 | Missing = false; | |
308 | ||
309 | // Round to a 2048 byte block | |
310 | Resume = Resume - (Resume % 2048); | |
311 | ||
312 | if (To.Truncate(Resume) == false) | |
313 | return false; | |
314 | if (To.Seek(0) == false) | |
315 | return false; | |
316 | ||
317 | if (Resume != 0) { | |
109eb151 | 318 | if (Hash.AddFD(To,Resume) == false) { |
dc738e7a | 319 | _error->Errno("read",_("Problem hashing file")); |
b2e465d6 AL |
320 | return false; |
321 | } | |
322 | } | |
323 | ||
324 | // FIXME: Detect file-not openable type errors. | |
8f3ba4e8 | 325 | std::string Jnk; |
b2e465d6 AL |
326 | if (WriteMsg(Jnk,false,"dd if=%s bs=2048 skip=%u", Path, Resume / 2048) == false) |
327 | return false; | |
328 | ||
329 | // Copy loop | |
650faab0 | 330 | unsigned long long MyLen = Resume; |
b2e465d6 AL |
331 | unsigned char Buffer[4096]; |
332 | while (MyLen < Size) | |
333 | { | |
334 | // Wait for some data.. | |
335 | if (WaitFd(ReadFd,false,TimeOut) == false) | |
336 | { | |
337 | Close(); | |
dc738e7a | 338 | return _error->Error(_("Data socket timed out")); |
b2e465d6 AL |
339 | } |
340 | ||
341 | // Read the data.. | |
342 | int Res = read(ReadFd,Buffer,sizeof(Buffer)); | |
343 | if (Res == 0) | |
344 | { | |
345 | Close(); | |
dc738e7a | 346 | return _error->Error(_("Connection closed prematurely")); |
b2e465d6 AL |
347 | } |
348 | ||
349 | if (Res < 0) | |
350 | { | |
351 | if (errno == EAGAIN) | |
352 | continue; | |
353 | break; | |
354 | } | |
355 | MyLen += Res; | |
356 | ||
63b1700f | 357 | Hash.Add(Buffer,Res); |
b2e465d6 AL |
358 | if (To.Write(Buffer,Res) == false) |
359 | { | |
360 | Close(); | |
361 | return false; | |
362 | } | |
363 | } | |
364 | ||
365 | return true; | |
366 | } | |
367 | /*}}}*/ | |
368 | ||
369 | // RSHMethod::RSHMethod - Constructor /*{{{*/ | |
370 | // --------------------------------------------------------------------- | |
371 | /* */ | |
5e775e59 | 372 | RSHMethod::RSHMethod() : pkgAcqMethod("1.0",SendConfig) |
b2e465d6 AL |
373 | { |
374 | signal(SIGTERM,SigTerm); | |
375 | signal(SIGINT,SigTerm); | |
376 | Server = 0; | |
377 | FailFd = -1; | |
d3e8fbb3 | 378 | } |
b2e465d6 | 379 | /*}}}*/ |
5e775e59 AL |
380 | // RSHMethod::Configuration - Handle a configuration message /*{{{*/ |
381 | // --------------------------------------------------------------------- | |
8f3ba4e8 | 382 | bool RSHMethod::Configuration(std::string Message) |
5e775e59 AL |
383 | { |
384 | char ProgStr[100]; | |
385 | ||
386 | if (pkgAcqMethod::Configuration(Message) == false) | |
387 | return false; | |
388 | ||
389 | snprintf(ProgStr, sizeof ProgStr, "Acquire::%s::Timeout", Prog); | |
390 | TimeOut = _config->FindI(ProgStr,TimeOut); | |
391 | snprintf(ProgStr, sizeof ProgStr, "Acquire::%s::Options", Prog); | |
392 | RshOptions = _config->Tree(ProgStr); | |
393 | ||
394 | return true; | |
395 | } | |
396 | /*}}}*/ | |
b2e465d6 AL |
397 | // RSHMethod::SigTerm - Clean up and timestamp the files on exit /*{{{*/ |
398 | // --------------------------------------------------------------------- | |
399 | /* */ | |
65512241 | 400 | void RSHMethod::SigTerm(int) |
b2e465d6 AL |
401 | { |
402 | if (FailFd == -1) | |
403 | _exit(100); | |
b2e465d6 | 404 | |
9ce3cfc9 | 405 | // Transfer the modification times |
246bbb61 | 406 | struct timeval times[2]; |
9ce3cfc9 DK |
407 | times[0].tv_sec = FailTime; |
408 | times[1].tv_sec = FailTime; | |
246bbb61 DK |
409 | times[0].tv_usec = times[1].tv_usec = 0; |
410 | utimes(FailFile.c_str(), times); | |
9ce3cfc9 | 411 | close(FailFd); |
b2e465d6 AL |
412 | |
413 | _exit(100); | |
414 | } | |
415 | /*}}}*/ | |
416 | // RSHMethod::Fetch - Fetch a URI /*{{{*/ | |
417 | // --------------------------------------------------------------------- | |
418 | /* */ | |
419 | bool RSHMethod::Fetch(FetchItem *Itm) | |
420 | { | |
421 | URI Get = Itm->Uri; | |
422 | const char *File = Get.Path.c_str(); | |
423 | FetchResult Res; | |
424 | Res.Filename = Itm->DestFile; | |
425 | Res.IMSHit = false; | |
426 | ||
427 | // Connect to the server | |
428 | if (Server == 0 || Server->Comp(Get) == false) { | |
429 | delete Server; | |
430 | Server = new RSHConn(Get); | |
431 | } | |
432 | ||
433 | // Could not connect is a transient error.. | |
434 | if (Server->Open() == false) { | |
435 | Server->Close(); | |
436 | Fail(true); | |
437 | return true; | |
438 | } | |
439 | ||
440 | // We say this mainly because the pause here is for the | |
441 | // ssh connection that is still going | |
dc738e7a | 442 | Status(_("Connecting to %s"), Get.Host.c_str()); |
b2e465d6 AL |
443 | |
444 | // Get the files information | |
650faab0 | 445 | unsigned long long Size; |
b2e465d6 AL |
446 | if (Server->Size(File,Size) == false || |
447 | Server->ModTime(File,FailTime) == false) | |
448 | { | |
449 | //Fail(true); | |
db0db9fe | 450 | //_error->Error(_("File not found")); // Will be handled by Size |
b2e465d6 AL |
451 | return false; |
452 | } | |
453 | Res.Size = Size; | |
454 | ||
455 | // See if it is an IMS hit | |
456 | if (Itm->LastModified == FailTime) { | |
457 | Res.Size = 0; | |
458 | Res.IMSHit = true; | |
459 | URIDone(Res); | |
460 | return true; | |
461 | } | |
462 | ||
463 | // See if the file exists | |
464 | struct stat Buf; | |
465 | if (stat(Itm->DestFile.c_str(),&Buf) == 0) { | |
650faab0 | 466 | if (Size == (unsigned long long)Buf.st_size && FailTime == Buf.st_mtime) { |
b2e465d6 AL |
467 | Res.Size = Buf.st_size; |
468 | Res.LastModified = Buf.st_mtime; | |
469 | Res.ResumePoint = Buf.st_size; | |
470 | URIDone(Res); | |
471 | return true; | |
472 | } | |
473 | ||
474 | // Resume? | |
650faab0 | 475 | if (FailTime == Buf.st_mtime && Size > (unsigned long long)Buf.st_size) |
b2e465d6 AL |
476 | Res.ResumePoint = Buf.st_size; |
477 | } | |
478 | ||
479 | // Open the file | |
63b1700f | 480 | Hashes Hash; |
b2e465d6 AL |
481 | { |
482 | FileFd Fd(Itm->DestFile,FileFd::WriteAny); | |
483 | if (_error->PendingError() == true) | |
484 | return false; | |
485 | ||
486 | URIStart(Res); | |
487 | ||
488 | FailFile = Itm->DestFile; | |
489 | FailFile.c_str(); // Make sure we dont do a malloc in the signal handler | |
490 | FailFd = Fd.Fd(); | |
491 | ||
492 | bool Missing; | |
63b1700f | 493 | if (Server->Get(File,Fd,Res.ResumePoint,Hash,Missing,Res.Size) == false) |
b2e465d6 AL |
494 | { |
495 | Fd.Close(); | |
496 | ||
497 | // Timestamp | |
246bbb61 | 498 | struct timeval times[2]; |
9ce3cfc9 DK |
499 | times[0].tv_sec = FailTime; |
500 | times[1].tv_sec = FailTime; | |
246bbb61 DK |
501 | times[0].tv_usec = times[1].tv_usec = 0; |
502 | utimes(FailFile.c_str(), times); | |
b2e465d6 AL |
503 | |
504 | // If the file is missing we hard fail otherwise transient fail | |
505 | if (Missing == true) | |
506 | return false; | |
507 | Fail(true); | |
508 | return true; | |
509 | } | |
510 | ||
511 | Res.Size = Fd.Size(); | |
246bbb61 | 512 | struct timeval times[2]; |
9ce3cfc9 DK |
513 | times[0].tv_sec = FailTime; |
514 | times[1].tv_sec = FailTime; | |
246bbb61 DK |
515 | times[0].tv_usec = times[1].tv_usec = 0; |
516 | utimes(Fd.Name().c_str(), times); | |
9ce3cfc9 | 517 | FailFd = -1; |
b2e465d6 AL |
518 | } |
519 | ||
520 | Res.LastModified = FailTime; | |
a7c835af | 521 | Res.TakeHashes(Hash); |
b2e465d6 | 522 | |
b2e465d6 AL |
523 | URIDone(Res); |
524 | ||
525 | return true; | |
526 | } | |
527 | /*}}}*/ | |
528 | ||
65512241 | 529 | int main(int, const char *argv[]) |
b2e465d6 | 530 | { |
b25423f6 MZ |
531 | setlocale(LC_ALL, ""); |
532 | ||
b2e465d6 AL |
533 | RSHMethod Mth; |
534 | Prog = strrchr(argv[0],'/'); | |
535 | Prog++; | |
536 | return Mth.Run(); | |
537 | } |