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