rsh.cc 13 KB

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