rsh.cc 12 KB

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