rsh.cc 14 KB

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