]>
Commit | Line | Data |
---|---|---|
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 | Buffer[0] = '\0'; | |
47 | } | |
48 | /*}}}*/ | |
49 | // RSHConn::RSHConn - Destructor /*{{{*/ | |
50 | // --------------------------------------------------------------------- | |
51 | /* */ | |
52 | RSHConn::~RSHConn() | |
53 | { | |
54 | Close(); | |
55 | } | |
56 | /*}}}*/ | |
57 | // RSHConn::Close - Forcibly terminate the connection /*{{{*/ | |
58 | // --------------------------------------------------------------------- | |
59 | /* Often this is called when things have gone wrong to indicate that the | |
60 | connection is no longer usable. */ | |
61 | void RSHConn::Close() | |
62 | { | |
63 | if (Process == -1) | |
64 | return; | |
65 | ||
66 | close(WriteFd); | |
67 | close(ReadFd); | |
68 | kill(Process,SIGINT); | |
69 | ExecWait(Process,"",true); | |
70 | WriteFd = -1; | |
71 | ReadFd = -1; | |
72 | Process = -1; | |
73 | } | |
74 | /*}}}*/ | |
75 | // RSHConn::Open - Connect to a host /*{{{*/ | |
76 | // --------------------------------------------------------------------- | |
77 | /* */ | |
78 | bool RSHConn::Open() | |
79 | { | |
80 | // Use the already open connection if possible. | |
81 | if (Process != -1) | |
82 | return true; | |
83 | ||
84 | if (Connect(ServerName.Host,ServerName.User) == false) | |
85 | return false; | |
86 | ||
87 | return true; | |
88 | } | |
89 | /*}}}*/ | |
90 | // RSHConn::Connect - Fire up rsh and connect /*{{{*/ | |
91 | // --------------------------------------------------------------------- | |
92 | /* */ | |
93 | bool RSHConn::Connect(std::string Host, std::string User) | |
94 | { | |
95 | // Create the pipes | |
96 | int Pipes[4] = {-1,-1,-1,-1}; | |
97 | if (pipe(Pipes) != 0 || pipe(Pipes+2) != 0) | |
98 | { | |
99 | _error->Errno("pipe",_("Failed to create IPC pipe to subprocess")); | |
100 | for (int I = 0; I != 4; I++) | |
101 | close(Pipes[I]); | |
102 | return false; | |
103 | } | |
104 | for (int I = 0; I != 4; I++) | |
105 | SetCloseExec(Pipes[I],true); | |
106 | ||
107 | Process = ExecFork(); | |
108 | ||
109 | // The child | |
110 | if (Process == 0) | |
111 | { | |
112 | const char *Args[400]; | |
113 | unsigned int i = 0; | |
114 | ||
115 | dup2(Pipes[1],STDOUT_FILENO); | |
116 | dup2(Pipes[2],STDIN_FILENO); | |
117 | ||
118 | // Probably should do | |
119 | // dup2(open("/dev/null",O_RDONLY),STDERR_FILENO); | |
120 | ||
121 | Args[i++] = Prog; | |
122 | ||
123 | // Insert user-supplied command line options | |
124 | Configuration::Item const *Opts = RshOptions; | |
125 | if (Opts != 0) | |
126 | { | |
127 | Opts = Opts->Child; | |
128 | for (; Opts != 0; Opts = Opts->Next) | |
129 | { | |
130 | if (Opts->Value.empty() == true) | |
131 | continue; | |
132 | Args[i++] = Opts->Value.c_str(); | |
133 | } | |
134 | } | |
135 | ||
136 | if (User.empty() == false) { | |
137 | Args[i++] = "-l"; | |
138 | Args[i++] = User.c_str(); | |
139 | } | |
140 | if (Host.empty() == false) { | |
141 | Args[i++] = Host.c_str(); | |
142 | } | |
143 | Args[i++] = "/bin/sh"; | |
144 | Args[i] = 0; | |
145 | execvp(Args[0],(char **)Args); | |
146 | exit(100); | |
147 | } | |
148 | ||
149 | ReadFd = Pipes[0]; | |
150 | WriteFd = Pipes[3]; | |
151 | SetNonBlock(Pipes[0],true); | |
152 | SetNonBlock(Pipes[3],true); | |
153 | close(Pipes[1]); | |
154 | close(Pipes[2]); | |
155 | ||
156 | return true; | |
157 | } | |
158 | /*}}}*/ | |
159 | // RSHConn::ReadLine - Very simple buffered read with timeout /*{{{*/ | |
160 | // --------------------------------------------------------------------- | |
161 | /* */ | |
162 | bool RSHConn::ReadLine(std::string &Text) | |
163 | { | |
164 | if (Process == -1 || ReadFd == -1) | |
165 | return false; | |
166 | ||
167 | // Suck in a line | |
168 | while (Len < sizeof(Buffer)) | |
169 | { | |
170 | // Scan the buffer for a new line | |
171 | for (unsigned int I = 0; I != Len; I++) | |
172 | { | |
173 | // Escape some special chars | |
174 | if (Buffer[I] == 0) | |
175 | Buffer[I] = '?'; | |
176 | ||
177 | // End of line? | |
178 | if (Buffer[I] != '\n') | |
179 | continue; | |
180 | ||
181 | I++; | |
182 | Text = std::string(Buffer,I); | |
183 | memmove(Buffer,Buffer+I,Len - I); | |
184 | Len -= I; | |
185 | return true; | |
186 | } | |
187 | ||
188 | // Wait for some data.. | |
189 | if (WaitFd(ReadFd,false,TimeOut) == false) | |
190 | { | |
191 | Close(); | |
192 | return _error->Error(_("Connection timeout")); | |
193 | } | |
194 | ||
195 | // Suck it back | |
196 | int Res = read(ReadFd,Buffer + Len,sizeof(Buffer) - Len); | |
197 | if (Res <= 0) | |
198 | { | |
199 | _error->Errno("read",_("Read error")); | |
200 | Close(); | |
201 | return false; | |
202 | } | |
203 | Len += Res; | |
204 | } | |
205 | ||
206 | return _error->Error(_("A response overflowed the buffer.")); | |
207 | } | |
208 | /*}}}*/ | |
209 | // RSHConn::WriteMsg - Send a message with optional remote sync. /*{{{*/ | |
210 | // --------------------------------------------------------------------- | |
211 | /* The remote sync flag appends a || echo which will insert blank line | |
212 | once the command completes. */ | |
213 | bool RSHConn::WriteMsg(std::string &Text,bool Sync,const char *Fmt,...) | |
214 | { | |
215 | va_list args; | |
216 | va_start(args,Fmt); | |
217 | ||
218 | // sprintf the description | |
219 | char S[512]; | |
220 | vsnprintf(S,sizeof(S) - 4,Fmt,args); | |
221 | va_end(args); | |
222 | ||
223 | if (Sync == true) | |
224 | strcat(S," 2> /dev/null || echo\n"); | |
225 | else | |
226 | strcat(S," 2> /dev/null\n"); | |
227 | ||
228 | // Send it off | |
229 | unsigned long Len = strlen(S); | |
230 | unsigned long Start = 0; | |
231 | while (Len != 0) | |
232 | { | |
233 | if (WaitFd(WriteFd,true,TimeOut) == false) | |
234 | { | |
235 | ||
236 | Close(); | |
237 | return _error->Error(_("Connection timeout")); | |
238 | } | |
239 | ||
240 | int Res = write(WriteFd,S + Start,Len); | |
241 | if (Res <= 0) | |
242 | { | |
243 | _error->Errno("write",_("Write error")); | |
244 | Close(); | |
245 | return false; | |
246 | } | |
247 | ||
248 | Len -= Res; | |
249 | Start += Res; | |
250 | } | |
251 | ||
252 | if (Sync == true) | |
253 | return ReadLine(Text); | |
254 | return true; | |
255 | } | |
256 | /*}}}*/ | |
257 | // RSHConn::Size - Return the size of the file /*{{{*/ | |
258 | // --------------------------------------------------------------------- | |
259 | /* Right now for successfull transfer the file size must be known in | |
260 | advance. */ | |
261 | bool RSHConn::Size(const char *Path,unsigned long long &Size) | |
262 | { | |
263 | // Query the size | |
264 | std::string Msg; | |
265 | Size = 0; | |
266 | ||
267 | if (WriteMsg(Msg,true,"find %s -follow -printf '%%s\\n'",Path) == false) | |
268 | return false; | |
269 | ||
270 | // FIXME: Sense if the bad reply is due to a File Not Found. | |
271 | ||
272 | char *End; | |
273 | Size = strtoull(Msg.c_str(),&End,10); | |
274 | if (End == Msg.c_str()) | |
275 | return _error->Error(_("File not found")); | |
276 | return true; | |
277 | } | |
278 | /*}}}*/ | |
279 | // RSHConn::ModTime - Get the modification time in UTC /*{{{*/ | |
280 | // --------------------------------------------------------------------- | |
281 | /* */ | |
282 | bool RSHConn::ModTime(const char *Path, time_t &Time) | |
283 | { | |
284 | Time = time(&Time); | |
285 | // Query the mod time | |
286 | std::string Msg; | |
287 | ||
288 | if (WriteMsg(Msg,true,"TZ=UTC find %s -follow -printf '%%TY%%Tm%%Td%%TH%%TM%%TS\\n'",Path) == false) | |
289 | return false; | |
290 | ||
291 | // Parse it | |
292 | return FTPMDTMStrToTime(Msg.c_str(), Time); | |
293 | } | |
294 | /*}}}*/ | |
295 | // RSHConn::Get - Get a file /*{{{*/ | |
296 | // --------------------------------------------------------------------- | |
297 | /* */ | |
298 | bool RSHConn::Get(const char *Path,FileFd &To,unsigned long long Resume, | |
299 | Hashes &Hash,bool &Missing, unsigned long long Size) | |
300 | { | |
301 | Missing = false; | |
302 | ||
303 | // Round to a 2048 byte block | |
304 | Resume = Resume - (Resume % 2048); | |
305 | ||
306 | if (To.Truncate(Resume) == false) | |
307 | return false; | |
308 | if (To.Seek(0) == false) | |
309 | return false; | |
310 | ||
311 | if (Resume != 0) { | |
312 | if (Hash.AddFD(To,Resume) == false) { | |
313 | _error->Errno("read",_("Problem hashing file")); | |
314 | return false; | |
315 | } | |
316 | } | |
317 | ||
318 | // FIXME: Detect file-not openable type errors. | |
319 | std::string Jnk; | |
320 | if (WriteMsg(Jnk,false,"dd if=%s bs=2048 skip=%u", Path, Resume / 2048) == false) | |
321 | return false; | |
322 | ||
323 | // Copy loop | |
324 | unsigned long long MyLen = Resume; | |
325 | unsigned char Buffer[4096]; | |
326 | while (MyLen < Size) | |
327 | { | |
328 | // Wait for some data.. | |
329 | if (WaitFd(ReadFd,false,TimeOut) == false) | |
330 | { | |
331 | Close(); | |
332 | return _error->Error(_("Data socket timed out")); | |
333 | } | |
334 | ||
335 | // Read the data.. | |
336 | int Res = read(ReadFd,Buffer,sizeof(Buffer)); | |
337 | if (Res == 0) | |
338 | { | |
339 | Close(); | |
340 | return _error->Error(_("Connection closed prematurely")); | |
341 | } | |
342 | ||
343 | if (Res < 0) | |
344 | { | |
345 | if (errno == EAGAIN) | |
346 | continue; | |
347 | break; | |
348 | } | |
349 | MyLen += Res; | |
350 | ||
351 | Hash.Add(Buffer,Res); | |
352 | if (To.Write(Buffer,Res) == false) | |
353 | { | |
354 | Close(); | |
355 | return false; | |
356 | } | |
357 | } | |
358 | ||
359 | return true; | |
360 | } | |
361 | /*}}}*/ | |
362 | ||
363 | // RSHMethod::RSHMethod - Constructor /*{{{*/ | |
364 | // --------------------------------------------------------------------- | |
365 | /* */ | |
366 | RSHMethod::RSHMethod() : pkgAcqMethod("1.0",SendConfig) | |
367 | { | |
368 | signal(SIGTERM,SigTerm); | |
369 | signal(SIGINT,SigTerm); | |
370 | Server = 0; | |
371 | FailFd = -1; | |
372 | }; | |
373 | /*}}}*/ | |
374 | // RSHMethod::Configuration - Handle a configuration message /*{{{*/ | |
375 | // --------------------------------------------------------------------- | |
376 | bool RSHMethod::Configuration(std::string Message) | |
377 | { | |
378 | char ProgStr[100]; | |
379 | ||
380 | if (pkgAcqMethod::Configuration(Message) == false) | |
381 | return false; | |
382 | ||
383 | snprintf(ProgStr, sizeof ProgStr, "Acquire::%s::Timeout", Prog); | |
384 | TimeOut = _config->FindI(ProgStr,TimeOut); | |
385 | snprintf(ProgStr, sizeof ProgStr, "Acquire::%s::Options", Prog); | |
386 | RshOptions = _config->Tree(ProgStr); | |
387 | ||
388 | return true; | |
389 | } | |
390 | /*}}}*/ | |
391 | // RSHMethod::SigTerm - Clean up and timestamp the files on exit /*{{{*/ | |
392 | // --------------------------------------------------------------------- | |
393 | /* */ | |
394 | void RSHMethod::SigTerm(int sig) | |
395 | { | |
396 | if (FailFd == -1) | |
397 | _exit(100); | |
398 | close(FailFd); | |
399 | ||
400 | // Timestamp | |
401 | struct utimbuf UBuf; | |
402 | UBuf.actime = FailTime; | |
403 | UBuf.modtime = FailTime; | |
404 | utime(FailFile.c_str(),&UBuf); | |
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 utimbuf UBuf; | |
492 | UBuf.actime = FailTime; | |
493 | UBuf.modtime = FailTime; | |
494 | utime(FailFile.c_str(),&UBuf); | |
495 | ||
496 | // If the file is missing we hard fail otherwise transient fail | |
497 | if (Missing == true) | |
498 | return false; | |
499 | Fail(true); | |
500 | return true; | |
501 | } | |
502 | ||
503 | Res.Size = Fd.Size(); | |
504 | } | |
505 | ||
506 | Res.LastModified = FailTime; | |
507 | Res.TakeHashes(Hash); | |
508 | ||
509 | // Timestamp | |
510 | struct utimbuf UBuf; | |
511 | UBuf.actime = FailTime; | |
512 | UBuf.modtime = FailTime; | |
513 | utime(Queue->DestFile.c_str(),&UBuf); | |
514 | FailFd = -1; | |
515 | ||
516 | URIDone(Res); | |
517 | ||
518 | return true; | |
519 | } | |
520 | /*}}}*/ | |
521 | ||
522 | int main(int argc, const char *argv[]) | |
523 | { | |
524 | setlocale(LC_ALL, ""); | |
525 | ||
526 | RSHMethod Mth; | |
527 | Prog = strrchr(argv[0],'/'); | |
528 | Prog++; | |
529 | return Mth.Run(); | |
530 | } |