rsh.cc 12 KB

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