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.
 
 
 
 
 
 

638 lines
14 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007-2010 by √ėyvind Harboe *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. /* this file contains various functionality useful to standalone systems */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "log.h"
  24. #include "time_support.h"
  25. #ifdef HAVE_ARPA_INET_H
  26. #include <arpa/inet.h>
  27. #endif
  28. #ifdef HAVE_DIRENT_H
  29. #include <dirent.h>
  30. #endif
  31. #ifdef HAVE_NETDB_H
  32. #include <netdb.h>
  33. #endif
  34. #ifdef HAVE_NET_IF_H
  35. #include <net/if.h>
  36. #endif
  37. #ifdef HAVE_SYS_IOCTL_H
  38. #include <sys/ioctl.h>
  39. #endif
  40. #ifdef HAVE_SYS_STAT_H
  41. #include <sys/stat.h>
  42. #endif
  43. #ifdef HAVE_IFADDRS_H
  44. #include <ifaddrs.h>
  45. #endif
  46. #ifdef HAVE_MALLOC_H
  47. #include <malloc.h>
  48. #endif
  49. /* loads a file and returns a pointer to it in memory. The file contains
  50. * a 0 byte(sentinel) after len bytes - the length of the file. */
  51. int loadFile(const char *fileName, void **data, size_t *len)
  52. {
  53. /* ensure returned length is always sane */
  54. *len = 0;
  55. FILE *pFile;
  56. pFile = fopen(fileName, "rb");
  57. if (pFile == NULL) {
  58. LOG_ERROR("Can't open %s", fileName);
  59. return ERROR_FAIL;
  60. }
  61. if (fseek(pFile, 0, SEEK_END) != 0) {
  62. LOG_ERROR("Can't open %s", fileName);
  63. fclose(pFile);
  64. return ERROR_FAIL;
  65. }
  66. long fsize = ftell(pFile);
  67. if (fsize == -1) {
  68. LOG_ERROR("Can't open %s", fileName);
  69. fclose(pFile);
  70. return ERROR_FAIL;
  71. }
  72. *len = fsize;
  73. if (fseek(pFile, 0, SEEK_SET) != 0) {
  74. LOG_ERROR("Can't open %s", fileName);
  75. fclose(pFile);
  76. return ERROR_FAIL;
  77. }
  78. *data = malloc(*len + 1);
  79. if (*data == NULL) {
  80. LOG_ERROR("Can't open %s", fileName);
  81. fclose(pFile);
  82. return ERROR_FAIL;
  83. }
  84. if (fread(*data, 1, *len, pFile) != *len) {
  85. fclose(pFile);
  86. free(*data);
  87. LOG_ERROR("Can't open %s", fileName);
  88. return ERROR_FAIL;
  89. }
  90. fclose(pFile);
  91. /* 0-byte after buffer (not included in *len) serves as a sentinel */
  92. char *buf = (char *)*data;
  93. buf[*len] = 0;
  94. return ERROR_OK;
  95. }
  96. COMMAND_HANDLER(handle_cat_command)
  97. {
  98. if (CMD_ARGC != 1)
  99. return ERROR_COMMAND_SYNTAX_ERROR;
  100. /* NOTE!!! we only have line printing capability so we print the entire file as a single
  101. * line. */
  102. void *data;
  103. size_t len;
  104. int retval = loadFile(CMD_ARGV[0], &data, &len);
  105. if (retval == ERROR_OK) {
  106. command_print(CMD_CTX, "%s", (char *)data);
  107. free(data);
  108. } else
  109. command_print(CMD_CTX, "%s not found", CMD_ARGV[0]);
  110. return ERROR_OK;
  111. }
  112. COMMAND_HANDLER(handle_trunc_command)
  113. {
  114. if (CMD_ARGC != 1)
  115. return ERROR_COMMAND_SYNTAX_ERROR;
  116. FILE *config_file = NULL;
  117. config_file = fopen(CMD_ARGV[0], "w");
  118. if (config_file != NULL)
  119. fclose(config_file);
  120. return ERROR_OK;
  121. }
  122. COMMAND_HANDLER(handle_meminfo_command)
  123. {
  124. static int prev;
  125. struct mallinfo info;
  126. if (CMD_ARGC != 0)
  127. return ERROR_COMMAND_SYNTAX_ERROR;
  128. info = mallinfo();
  129. if (prev > 0)
  130. command_print(CMD_CTX, "Diff: %d", prev - info.fordblks);
  131. prev = info.fordblks;
  132. command_print(CMD_CTX, "Available ram: %d", info.fordblks);
  133. return ERROR_OK;
  134. }
  135. COMMAND_HANDLER(handle_append_command)
  136. {
  137. if (CMD_ARGC < 1)
  138. return ERROR_COMMAND_SYNTAX_ERROR;
  139. int retval = ERROR_FAIL;
  140. FILE *config_file = NULL;
  141. config_file = fopen(CMD_ARGV[0], "a");
  142. if (config_file != NULL) {
  143. fseek(config_file, 0, SEEK_END);
  144. unsigned i;
  145. for (i = 1; i < CMD_ARGC; i++) {
  146. if (fwrite(CMD_ARGV[i], 1, strlen(CMD_ARGV[i]),
  147. config_file) != strlen(CMD_ARGV[i]))
  148. break;
  149. if (i != CMD_ARGC - 1) {
  150. if (fwrite(" ", 1, 1, config_file) != 1)
  151. break;
  152. }
  153. }
  154. if ((i == CMD_ARGC) && (fwrite("\n", 1, 1, config_file) == 1))
  155. retval = ERROR_OK;
  156. fclose(config_file);
  157. }
  158. return retval;
  159. }
  160. COMMAND_HANDLER(handle_cp_command)
  161. {
  162. if (CMD_ARGC != 2)
  163. return ERROR_COMMAND_SYNTAX_ERROR;
  164. /* NOTE!!! we only have line printing capability so we print the entire file as a single
  165. * line. */
  166. void *data;
  167. size_t len;
  168. int retval = loadFile(CMD_ARGV[0], &data, &len);
  169. if (retval != ERROR_OK)
  170. return retval;
  171. FILE *f = fopen(CMD_ARGV[1], "wb");
  172. if (f == NULL)
  173. retval = ERROR_COMMAND_SYNTAX_ERROR;
  174. size_t pos = 0;
  175. for (;; ) {
  176. size_t chunk = len - pos;
  177. static const size_t maxChunk = 512 * 1024; /* ~1/sec */
  178. if (chunk > maxChunk)
  179. chunk = maxChunk;
  180. if ((retval == ERROR_OK) && (fwrite(((char *)data) + pos, 1, chunk, f) != chunk))
  181. retval = ERROR_COMMAND_SYNTAX_ERROR;
  182. if (retval != ERROR_OK)
  183. break;
  184. command_print(CMD_CTX, "%zu", len - pos);
  185. pos += chunk;
  186. if (pos == len)
  187. break;
  188. }
  189. if (retval == ERROR_OK)
  190. command_print(CMD_CTX, "Copied %s to %s", CMD_ARGV[0], CMD_ARGV[1]);
  191. else
  192. command_print(CMD_CTX, "copy failed");
  193. if (data != NULL)
  194. free(data);
  195. if (f != NULL)
  196. fclose(f);
  197. if (retval != ERROR_OK)
  198. unlink(CMD_ARGV[1]);
  199. return retval;
  200. }
  201. #define SHOW_RESULT(a, b) LOG_ERROR(# a " failed %d\n", (int)b)
  202. #define IOSIZE 512
  203. void copyfile(char *name2, char *name1)
  204. {
  205. int err;
  206. char buf[IOSIZE];
  207. int fd1, fd2;
  208. ssize_t done, wrote;
  209. fd1 = open(name1, O_WRONLY | O_CREAT, 0664);
  210. if (fd1 < 0)
  211. SHOW_RESULT(open, fd1);
  212. fd2 = open(name2, O_RDONLY);
  213. if (fd2 < 0)
  214. SHOW_RESULT(open, fd2);
  215. for (;; ) {
  216. done = read(fd2, buf, IOSIZE);
  217. if (done < 0) {
  218. SHOW_RESULT(read, done);
  219. break;
  220. }
  221. if (done == 0)
  222. break;
  223. wrote = write(fd1, buf, done);
  224. if (wrote != done)
  225. SHOW_RESULT(write, wrote);
  226. if (wrote != done)
  227. break;
  228. }
  229. err = close(fd1);
  230. if (err < 0)
  231. SHOW_RESULT(close, err);
  232. err = close(fd2);
  233. if (err < 0)
  234. SHOW_RESULT(close, err);
  235. }
  236. /* utility fn to copy a directory */
  237. void copydir(char *name, char *destdir)
  238. {
  239. int err;
  240. DIR *dirp;
  241. dirp = opendir(destdir);
  242. if (dirp == NULL)
  243. mkdir(destdir, 0777);
  244. else
  245. err = closedir(dirp);
  246. dirp = opendir(name);
  247. if (dirp == NULL)
  248. SHOW_RESULT(opendir, -1);
  249. for (;; ) {
  250. struct dirent *entry = readdir(dirp);
  251. if (entry == NULL)
  252. break;
  253. if (strcmp(entry->d_name, ".") == 0)
  254. continue;
  255. if (strcmp(entry->d_name, "..") == 0)
  256. continue;
  257. int isDir = 0;
  258. struct stat buf;
  259. char fullPath[PATH_MAX];
  260. strncpy(fullPath, name, PATH_MAX);
  261. strcat(fullPath, "/");
  262. strncat(fullPath, entry->d_name, PATH_MAX - strlen(fullPath));
  263. if (stat(fullPath, &buf) == -1) {
  264. LOG_ERROR("unable to read status from %s", fullPath);
  265. break;
  266. }
  267. isDir = S_ISDIR(buf.st_mode) != 0;
  268. if (isDir)
  269. continue;
  270. /* diag_printf("<INFO>: entry %14s",entry->d_name); */
  271. char fullname[PATH_MAX];
  272. char fullname2[PATH_MAX];
  273. strcpy(fullname, name);
  274. strcat(fullname, "/");
  275. strcat(fullname, entry->d_name);
  276. strcpy(fullname2, destdir);
  277. strcat(fullname2, "/");
  278. strcat(fullname2, entry->d_name);
  279. /* diag_printf("from %s to %s\n", fullname, fullname2); */
  280. copyfile(fullname, fullname2);
  281. /* diag_printf("\n"); */
  282. }
  283. err = closedir(dirp);
  284. if (err < 0)
  285. SHOW_RESULT(stat, err);
  286. }
  287. COMMAND_HANDLER(handle_rm_command)
  288. {
  289. if (CMD_ARGC != 1)
  290. return ERROR_COMMAND_SYNTAX_ERROR;
  291. bool del = false;
  292. if (rmdir(CMD_ARGV[0]) == 0)
  293. del = true;
  294. else if (unlink(CMD_ARGV[0]) == 0)
  295. del = true;
  296. return del ? ERROR_OK : ERROR_FAIL;
  297. }
  298. static int ioutil_Jim_Command_ls(Jim_Interp *interp,
  299. int argc,
  300. Jim_Obj * const *argv)
  301. {
  302. if (argc != 2) {
  303. Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
  304. return JIM_ERR;
  305. }
  306. char *name = (char *) Jim_GetString(argv[1], NULL);
  307. DIR *dirp = NULL;
  308. dirp = opendir(name);
  309. if (dirp == NULL)
  310. return JIM_ERR;
  311. Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0);
  312. for (;; ) {
  313. struct dirent *entry = NULL;
  314. entry = readdir(dirp);
  315. if (entry == NULL)
  316. break;
  317. if ((strcmp(".", entry->d_name) == 0) || (strcmp("..", entry->d_name) == 0))
  318. continue;
  319. Jim_ListAppendElement(interp, objPtr,
  320. Jim_NewStringObj(interp, entry->d_name, strlen(entry->d_name)));
  321. }
  322. closedir(dirp);
  323. Jim_SetResult(interp, objPtr);
  324. return JIM_OK;
  325. }
  326. static int ioutil_Jim_Command_peek(Jim_Interp *interp,
  327. int argc,
  328. Jim_Obj *const *argv)
  329. {
  330. if (argc != 2) {
  331. Jim_WrongNumArgs(interp, 1, argv, "peek ?address?");
  332. return JIM_ERR;
  333. }
  334. long address;
  335. if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
  336. return JIM_ERR;
  337. int value = *((volatile int *) address);
  338. Jim_SetResult(interp, Jim_NewIntObj(interp, value));
  339. return JIM_OK;
  340. }
  341. static int ioutil_Jim_Command_poke(Jim_Interp *interp,
  342. int argc,
  343. Jim_Obj *const *argv)
  344. {
  345. if (argc != 3) {
  346. Jim_WrongNumArgs(interp, 1, argv, "poke ?address? ?value?");
  347. return JIM_ERR;
  348. }
  349. long address;
  350. if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
  351. return JIM_ERR;
  352. long value;
  353. if (Jim_GetLong(interp, argv[2], &value) != JIM_OK)
  354. return JIM_ERR;
  355. *((volatile int *) address) = value;
  356. return JIM_OK;
  357. }
  358. /* not so pretty code to fish out ip number*/
  359. static int ioutil_Jim_Command_ip(Jim_Interp *interp, int argc,
  360. Jim_Obj *const *argv)
  361. {
  362. #if !defined(__CYGWIN__)
  363. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
  364. struct ifaddrs *ifa = NULL, *ifp = NULL;
  365. if (getifaddrs(&ifp) < 0)
  366. return JIM_ERR;
  367. for (ifa = ifp; ifa; ifa = ifa->ifa_next) {
  368. char ip[200];
  369. socklen_t salen;
  370. if (ifa->ifa_addr->sa_family == AF_INET)
  371. salen = sizeof(struct sockaddr_in);
  372. else if (ifa->ifa_addr->sa_family == AF_INET6)
  373. salen = sizeof(struct sockaddr_in6);
  374. else
  375. continue;
  376. if (getnameinfo(ifa->ifa_addr, salen, ip, sizeof(ip), NULL, 0,
  377. NI_NUMERICHOST) < 0)
  378. continue;
  379. Jim_AppendString(interp, tclOutput, ip, strlen(ip));
  380. break;
  381. }
  382. freeifaddrs(ifp);
  383. #else
  384. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "fixme!!!", 0);
  385. LOG_ERROR("NOT IMPLEMENTED!!!");
  386. #endif
  387. Jim_SetResult(interp, tclOutput);
  388. return JIM_OK;
  389. }
  390. /* not so pretty code to fish out eth0 mac address */
  391. static int ioutil_Jim_Command_mac(Jim_Interp *interp, int argc,
  392. Jim_Obj *const *argv)
  393. {
  394. struct ifreq *ifr, *ifend;
  395. struct ifreq ifreq;
  396. struct ifconf ifc;
  397. struct ifreq ifs[5];
  398. int SockFD;
  399. SockFD = socket(AF_INET, SOCK_DGRAM, 0);
  400. if (SockFD < 0)
  401. return JIM_ERR;
  402. ifc.ifc_len = sizeof(ifs);
  403. ifc.ifc_req = ifs;
  404. if (ioctl(SockFD, SIOCGIFCONF, &ifc) < 0) {
  405. close(SockFD);
  406. return JIM_ERR;
  407. }
  408. ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
  409. for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
  410. /* if (ifr->ifr_addr.sa_family == AF_INET) */
  411. {
  412. if (strcmp("eth0", ifr->ifr_name) != 0)
  413. continue;
  414. strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
  415. if (ioctl(SockFD, SIOCGIFHWADDR, &ifreq) < 0) {
  416. close(SockFD);
  417. return JIM_ERR;
  418. }
  419. close(SockFD);
  420. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
  421. char buffer[256];
  422. sprintf(buffer, "%02x-%02x-%02x-%02x-%02x-%02x",
  423. ifreq.ifr_hwaddr.sa_data[0]&0xff,
  424. ifreq.ifr_hwaddr.sa_data[1]&0xff,
  425. ifreq.ifr_hwaddr.sa_data[2]&0xff,
  426. ifreq.ifr_hwaddr.sa_data[3]&0xff,
  427. ifreq.ifr_hwaddr.sa_data[4]&0xff,
  428. ifreq.ifr_hwaddr.sa_data[5]&0xff);
  429. Jim_AppendString(interp, tclOutput, buffer, strlen(buffer));
  430. Jim_SetResult(interp, tclOutput);
  431. return JIM_OK;
  432. }
  433. }
  434. close(SockFD);
  435. return JIM_ERR;
  436. }
  437. static const struct command_registration ioutil_command_handlers[] = {
  438. {
  439. .name = "cat",
  440. .handler = handle_cat_command,
  441. .mode = COMMAND_ANY,
  442. .help = "display text file content",
  443. .usage = "file_name",
  444. },
  445. {
  446. .name = "trunc",
  447. .handler = handle_trunc_command,
  448. .mode = COMMAND_ANY,
  449. .help = "truncate a file to zero length",
  450. .usage = "file_name",
  451. },
  452. {
  453. .name = "cp",
  454. .handler = handle_cp_command,
  455. .mode = COMMAND_ANY,
  456. .help = "copy a file",
  457. .usage = "src_file_name dst_file_name",
  458. },
  459. {
  460. .name = "append_file",
  461. .handler = handle_append_command,
  462. .mode = COMMAND_ANY,
  463. .help = "append a variable number of strings to a file",
  464. .usage = "file_name [<string1>, [<string2>, ...]]",
  465. },
  466. {
  467. .name = "meminfo",
  468. .handler = handle_meminfo_command,
  469. .mode = COMMAND_ANY,
  470. .help = "display free heap space",
  471. },
  472. {
  473. .name = "rm",
  474. .mode = COMMAND_ANY,
  475. .handler = handle_rm_command,
  476. .help = "remove a directory or file",
  477. .usage = "file_name",
  478. },
  479. /*
  480. * Peek and poke are security holes -- they manipulate
  481. * server-internal addresses.
  482. */
  483. /* jim handlers */
  484. {
  485. .name = "peek",
  486. .mode = COMMAND_ANY,
  487. .jim_handler = ioutil_Jim_Command_peek,
  488. .help = "peek at a memory address",
  489. .usage = "address",
  490. },
  491. {
  492. .name = "poke",
  493. .mode = COMMAND_ANY,
  494. .jim_handler = ioutil_Jim_Command_poke,
  495. .help = "poke at a memory address",
  496. .usage = "address value",
  497. },
  498. {
  499. .name = "ls",
  500. .mode = COMMAND_ANY,
  501. .jim_handler = ioutil_Jim_Command_ls,
  502. .help = "show a listing of files",
  503. .usage = "dirname",
  504. },
  505. {
  506. .name = "mac",
  507. .mode = COMMAND_ANY,
  508. .jim_handler = ioutil_Jim_Command_mac,
  509. .help = "show MAC address",
  510. },
  511. {
  512. .name = "ip",
  513. .jim_handler = ioutil_Jim_Command_ip,
  514. .mode = COMMAND_ANY,
  515. .help = "show IP address",
  516. },
  517. COMMAND_REGISTRATION_DONE
  518. };
  519. int ioutil_init(struct command_context *cmd_ctx)
  520. {
  521. return register_commands(cmd_ctx, NULL, ioutil_command_handlers);
  522. }