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