You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

520 lines
13 KiB

  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 "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. #include <apti18n.h>
  22. /*}}}*/
  23. const char *Prog;
  24. unsigned long TimeOut = 120;
  25. Configuration::Item const *RshOptions = 0;
  26. time_t RSHMethod::FailTime = 0;
  27. string RSHMethod::FailFile;
  28. int RSHMethod::FailFd = -1;
  29. // RSHConn::RSHConn - Constructor /*{{{*/
  30. // ---------------------------------------------------------------------
  31. /* */
  32. RSHConn::RSHConn(URI Srv) : Len(0), WriteFd(-1), ReadFd(-1),
  33. ServerName(Srv), Process(-1) {}
  34. /*}}}*/
  35. // RSHConn::RSHConn - Destructor /*{{{*/
  36. // ---------------------------------------------------------------------
  37. /* */
  38. RSHConn::~RSHConn()
  39. {
  40. Close();
  41. }
  42. /*}}}*/
  43. // RSHConn::Close - Forcibly terminate the connection /*{{{*/
  44. // ---------------------------------------------------------------------
  45. /* Often this is called when things have gone wrong to indicate that the
  46. connection is no longer usable. */
  47. void RSHConn::Close()
  48. {
  49. if (Process == -1)
  50. return;
  51. close(WriteFd);
  52. close(ReadFd);
  53. kill(Process,SIGINT);
  54. ExecWait(Process,"",true);
  55. WriteFd = -1;
  56. ReadFd = -1;
  57. Process = -1;
  58. }
  59. /*}}}*/
  60. // RSHConn::Open - Connect to a host /*{{{*/
  61. // ---------------------------------------------------------------------
  62. /* */
  63. bool RSHConn::Open()
  64. {
  65. // Use the already open connection if possible.
  66. if (Process != -1)
  67. return true;
  68. if (Connect(ServerName.Host,ServerName.User) == false)
  69. return false;
  70. return true;
  71. }
  72. /*}}}*/
  73. // RSHConn::Connect - Fire up rsh and connect /*{{{*/
  74. // ---------------------------------------------------------------------
  75. /* */
  76. bool RSHConn::Connect(string Host, string User)
  77. {
  78. // Create the pipes
  79. int Pipes[4] = {-1,-1,-1,-1};
  80. if (pipe(Pipes) != 0 || pipe(Pipes+2) != 0)
  81. {
  82. _error->Errno("pipe",_("Failed to create IPC pipe to subprocess"));
  83. for (int I = 0; I != 4; I++)
  84. close(Pipes[I]);
  85. return false;
  86. }
  87. for (int I = 0; I != 4; I++)
  88. SetCloseExec(Pipes[I],true);
  89. Process = ExecFork();
  90. // The child
  91. if (Process == 0)
  92. {
  93. const char *Args[400];
  94. unsigned int i = 0;
  95. dup2(Pipes[1],STDOUT_FILENO);
  96. dup2(Pipes[2],STDIN_FILENO);
  97. // Probably should do
  98. // dup2(open("/dev/null",O_RDONLY),STDERR_FILENO);
  99. // Insert user-supplied command line options
  100. Configuration::Item const *Opts = RshOptions;
  101. if (Opts != 0)
  102. {
  103. Opts = Opts->Child;
  104. for (; Opts != 0; Opts = Opts->Next)
  105. {
  106. if (Opts->Value.empty() == true)
  107. continue;
  108. Args[i++] = Opts->Value.c_str();
  109. }
  110. }
  111. Args[i++] = Prog;
  112. if (User.empty() == false) {
  113. Args[i++] = "-l";
  114. Args[i++] = User.c_str();
  115. }
  116. if (Host.empty() == false) {
  117. Args[i++] = Host.c_str();
  118. }
  119. Args[i++] = "/bin/sh";
  120. Args[i] = 0;
  121. execvp(Args[0],(char **)Args);
  122. exit(100);
  123. }
  124. ReadFd = Pipes[0];
  125. WriteFd = Pipes[3];
  126. SetNonBlock(Pipes[0],true);
  127. SetNonBlock(Pipes[3],true);
  128. close(Pipes[1]);
  129. close(Pipes[2]);
  130. return true;
  131. }
  132. /*}}}*/
  133. // RSHConn::ReadLine - Very simple buffered read with timeout /*{{{*/
  134. // ---------------------------------------------------------------------
  135. /* */
  136. bool RSHConn::ReadLine(string &Text)
  137. {
  138. if (Process == -1 || ReadFd == -1)
  139. return false;
  140. // Suck in a line
  141. while (Len < sizeof(Buffer))
  142. {
  143. // Scan the buffer for a new line
  144. for (unsigned int I = 0; I != Len; I++)
  145. {
  146. // Escape some special chars
  147. if (Buffer[I] == 0)
  148. Buffer[I] = '?';
  149. // End of line?
  150. if (Buffer[I] != '\n')
  151. continue;
  152. I++;
  153. Text = string(Buffer,I);
  154. memmove(Buffer,Buffer+I,Len - I);
  155. Len -= I;
  156. return true;
  157. }
  158. // Wait for some data..
  159. if (WaitFd(ReadFd,false,TimeOut) == false)
  160. {
  161. Close();
  162. return _error->Error(_("Connection timeout"));
  163. }
  164. // Suck it back
  165. int Res = read(ReadFd,Buffer + Len,sizeof(Buffer) - Len);
  166. if (Res <= 0)
  167. {
  168. _error->Errno("read",_("Read error"));
  169. Close();
  170. return false;
  171. }
  172. Len += Res;
  173. }
  174. return _error->Error(_("A response overflowed the buffer."));
  175. }
  176. /*}}}*/
  177. // RSHConn::WriteMsg - Send a message with optional remote sync. /*{{{*/
  178. // ---------------------------------------------------------------------
  179. /* The remote sync flag appends a || echo which will insert blank line
  180. once the command completes. */
  181. bool RSHConn::WriteMsg(string &Text,bool Sync,const char *Fmt,...)
  182. {
  183. va_list args;
  184. va_start(args,Fmt);
  185. // sprintf the description
  186. char S[512];
  187. vsnprintf(S,sizeof(S) - 4,Fmt,args);
  188. if (Sync == true)
  189. strcat(S," 2> /dev/null || echo\n");
  190. else
  191. strcat(S," 2> /dev/null\n");
  192. // Send it off
  193. unsigned long Len = strlen(S);
  194. unsigned long Start = 0;
  195. while (Len != 0)
  196. {
  197. if (WaitFd(WriteFd,true,TimeOut) == false)
  198. {
  199. Close();
  200. return _error->Error(_("Connection timeout"));
  201. }
  202. int Res = write(WriteFd,S + Start,Len);
  203. if (Res <= 0)
  204. {
  205. _error->Errno("write",_("Write error"));
  206. Close();
  207. return false;
  208. }
  209. Len -= Res;
  210. Start += Res;
  211. }
  212. if (Sync == true)
  213. return ReadLine(Text);
  214. return true;
  215. }
  216. /*}}}*/
  217. // RSHConn::Size - Return the size of the file /*{{{*/
  218. // ---------------------------------------------------------------------
  219. /* Right now for successfull transfer the file size must be known in
  220. advance. */
  221. bool RSHConn::Size(const char *Path,unsigned long &Size)
  222. {
  223. // Query the size
  224. string Msg;
  225. Size = 0;
  226. if (WriteMsg(Msg,true,"find %s -follow -printf '%%s\\n'",Path) == false)
  227. return false;
  228. // FIXME: Sense if the bad reply is due to a File Not Found.
  229. char *End;
  230. Size = strtoul(Msg.c_str(),&End,10);
  231. if (End == Msg.c_str())
  232. return _error->Error(_("File not found"));
  233. return true;
  234. }
  235. /*}}}*/
  236. // RSHConn::ModTime - Get the modification time in UTC /*{{{*/
  237. // ---------------------------------------------------------------------
  238. /* */
  239. bool RSHConn::ModTime(const char *Path, time_t &Time)
  240. {
  241. Time = time(&Time);
  242. // Query the mod time
  243. string Msg;
  244. if (WriteMsg(Msg,true,"TZ=UTC find %s -follow -printf '%%TY%%Tm%%Td%%TH%%TM%%TS\\n'",Path) == false)
  245. return false;
  246. // Parse it
  247. return FTPMDTMStrToTime(Msg.c_str(), Time);
  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. setlocale(LC_ALL, "");
  446. RSHMethod Mth;
  447. Prog = strrchr(argv[0],'/');
  448. Prog++;
  449. return Mth.Run();
  450. }