packtool.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <iostream>
  31. #include <string>
  32. #include "node/Identity.hpp"
  33. #include "node/Pack.hpp"
  34. #include "node/Utils.hpp"
  35. #include <unistd.h>
  36. using namespace ZeroTier;
  37. static void printHelp(const char *pn)
  38. {
  39. std::cout << "Usage: " << pn << " <command> [<args>]" << std::endl << std::endl;
  40. std::cout << "Commands:" << std::endl;
  41. std::cout << " list <packfile> [<identity.secret/public>]" << std::endl;
  42. std::cout << " create <packfile> <identity.secret> <file> [<file> ...]" << std::endl;
  43. std::cout << " extract <packfile> <destination directory>" << std::endl;
  44. std::cout << std::endl << "To check signatures, use 'list' with an identity argument." << std::endl;
  45. }
  46. static Pack *readPack(const char *path)
  47. {
  48. std::string tmp;
  49. if (!Utils::readFile(path,tmp))
  50. return (Pack *)0;
  51. Pack *p = new Pack();
  52. if (!p->deserialize(tmp)) {
  53. delete p;
  54. return (Pack *)0;
  55. }
  56. return p;
  57. }
  58. int main(int argc,char **argv)
  59. {
  60. if (argc < 2) {
  61. printHelp(argv[0]);
  62. return -1;
  63. }
  64. if (!strcmp(argv[1],"list")) {
  65. if (argc < 3) {
  66. printHelp(argv[0]);
  67. return -1;
  68. }
  69. Pack *pack = readPack(argv[2]);
  70. if (!pack) {
  71. std::cout << "Could not read " << argv[2] << std::endl;
  72. return -1;
  73. }
  74. std::vector<const Pack::Entry *> entries = pack->getAll();
  75. for(std::vector<const Pack::Entry *>::iterator e=entries.begin();e!=entries.end();++e) {
  76. std::cout << (*e)->name << '\t' << (*e)->content.length() << '\t' << Utils::hex((*e)->sha256,32) << "\tsigned by: " << (*e)->signedBy.toString() << std::endl;
  77. }
  78. if (argc >= 4) {
  79. std::string idser;
  80. if (!Utils::readFile(argv[3],idser)) {
  81. std::cout << "Unable to read identity from " << argv[3] << std::endl;
  82. return -1;
  83. }
  84. Identity id;
  85. if (!id.fromString(idser)) {
  86. std::cout << "Invalid identity" << std::endl;
  87. return -1;
  88. }
  89. entries = pack->verifyAll(id,true);
  90. for(std::vector<const Pack::Entry *>::iterator e=entries.begin();e!=entries.end();++e) {
  91. std::cout << "!!! Signature verification FAILED for: " << (*e)->name << std::endl;
  92. }
  93. if (!entries.size())
  94. std::cout << "Signature for all entries verified OK" << std::endl;
  95. }
  96. delete pack;
  97. } else if (!strcmp(argv[1],"create")) {
  98. if (argc < 5) {
  99. printHelp(argv[0]);
  100. return -1;
  101. }
  102. std::string idser;
  103. if (!Utils::readFile(argv[3],idser)) {
  104. std::cout << "Unable to read identity from " << argv[3] << std::endl;
  105. return -1;
  106. }
  107. Identity id;
  108. if (!id.fromString(idser)) {
  109. std::cout << "Invalid identity" << std::endl;
  110. return -1;
  111. }
  112. if (!id.hasPrivate()) {
  113. std::cout << "Identity must include private key to sign" << std::endl;
  114. return -1;
  115. }
  116. Pack pack;
  117. for(int i=4;i<argc;++i) {
  118. std::string fdata;
  119. if (!Utils::readFile(argv[i],fdata)) {
  120. std::cout << "Unable to read " << argv[i] << std::endl;
  121. return -1;
  122. }
  123. pack.put(std::string(argv[i]),fdata);
  124. std::cout << "Added " << argv[i] << std::endl;
  125. }
  126. if (!pack.signAll(id)) {
  127. std::cout << "Unable to sign with identity" << std::endl;
  128. return -1;
  129. } else std::cout << "Signed all entries with identity " << id.address().toString() << std::endl;
  130. std::string packser = pack.serialize();
  131. if (!Utils::writeFile(argv[2],packser)) {
  132. std::cout << "Unable to write " << argv[2] << std::endl;
  133. return -1;
  134. }
  135. std::cout << "Wrote " << packser.length() << " bytes to " << argv[2] << std::endl;
  136. } else if (!strcmp(argv[1],"extract")) {
  137. if (argc < 4) {
  138. printHelp(argv[0]);
  139. return -1;
  140. }
  141. Pack *pack = readPack(argv[2]);
  142. if (!pack) {
  143. std::cout << "Could not read " << argv[2] << std::endl;
  144. return -1;
  145. }
  146. if (chdir(argv[3])) {
  147. std::cout << "Unable to change to " << argv[3] << " for output." << std::endl;
  148. return -1;
  149. }
  150. std::vector<const Pack::Entry *> entries = pack->getAll();
  151. for(std::vector<const Pack::Entry *>::iterator e=entries.begin();e!=entries.end();++e) {
  152. if (!Utils::writeFile((*e)->name.c_str(),(*e)->content))
  153. std::cout << "Error writing " << (*e)->name << std::endl;
  154. else std::cout << "Wrote " << (*e)->name << " (" << (*e)->content.length() << ")" << std::endl;
  155. }
  156. } else {
  157. printHelp(argv[0]);
  158. return -1;
  159. }
  160. return 0;
  161. }