1
0

jose.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2016 Red Hat, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include "jose.h"
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <ctype.h>
  23. #define MAXBUFLEN 1024
  24. #define RQARG required_argument
  25. #define NOARG no_argument
  26. static jcmd_t *cmds;
  27. static bool
  28. is_json_object_file(FILE *file)
  29. {
  30. int c;
  31. do {
  32. c = fgetc(file);
  33. } while (isspace(c));
  34. return ungetc(c, file) == '{';
  35. }
  36. static bool
  37. jwks_extend(json_t *jwks, json_t *jwk_or_jwkset)
  38. {
  39. json_t *keys = json_object_get(jwk_or_jwkset, "keys");
  40. size_t size = json_array_size(keys);
  41. if (!json_is_array(keys))
  42. return json_array_append_new(jwks, jwk_or_jwkset) == 0;
  43. for (size_t i = 0; i < size; i++) {
  44. if (json_array_append(jwks, json_array_get(keys, i)) == -1) {
  45. json_decref(jwk_or_jwkset);
  46. return false;
  47. }
  48. }
  49. json_decref(jwk_or_jwkset);
  50. return size > 0;
  51. }
  52. void
  53. jcmd_push(jcmd_t *cmd)
  54. {
  55. cmd->next = cmds;
  56. cmds = cmd;
  57. }
  58. bool
  59. jcmd_opt_parse(int argc, char *argv[], const jcmd_cfg_t *cfgs, void *vopt,
  60. const char *prefix)
  61. {
  62. size_t ncfgs = 0;
  63. int maxa = 0;
  64. int maxl = 0;
  65. for (; cfgs[ncfgs].doc; ncfgs++) {
  66. const jcmd_cfg_t *cfg = &cfgs[ncfgs];
  67. for (size_t i = 0; cfg->doc[i].doc; i++) {
  68. int len = 0;
  69. if (!cfg->doc[i].arg)
  70. continue;
  71. len = strlen(cfg->doc[i].arg);
  72. if (len > maxa)
  73. maxa = len;
  74. len = strlen(cfg->opt.name) + len;
  75. if (len > maxl)
  76. maxl = len;
  77. }
  78. if (cfg->def) {
  79. uint8_t *buf = vopt;
  80. if (!cfg->set(cfg, &buf[cfg->off], cfg->def)) {
  81. fprintf(stderr, "Invalid default value for %s!\n", cfg->opt.name);
  82. return false;
  83. }
  84. }
  85. }
  86. char sopts[ncfgs * 3 + 3];
  87. struct option lopts[ncfgs + 3];
  88. memset(lopts, 0, sizeof(lopts));
  89. lopts[0].has_arg = no_argument;
  90. lopts[0].name = "help";
  91. lopts[0].val = 'h';
  92. lopts[1].has_arg = no_argument;
  93. lopts[1].name = "version";
  94. lopts[1].val = 'v';
  95. strcpy(sopts, "hv");
  96. for (size_t i = 0; i < ncfgs; i++) {
  97. strncat(sopts, &(char) { cfgs[i].opt.val }, 1);
  98. lopts[i + 2] = cfgs[i].opt;
  99. switch (cfgs[i].opt.has_arg) {
  100. case optional_argument: strcat(sopts, ":"); /* fallthrough */
  101. case required_argument: strcat(sopts, ":"); /* fallthrough */
  102. default: break;
  103. }
  104. }
  105. for (int c; (c = getopt_long(argc, argv, sopts, lopts, NULL)) >= 0; ) {
  106. bool found = false;
  107. for (size_t i = 0; i < ncfgs; i++) {
  108. uint8_t *buf = vopt;
  109. if (cfgs[i].opt.val == c) {
  110. found = true;
  111. if (!cfgs[i].set(&cfgs[i], &buf[cfgs[i].off], optarg)) {
  112. fprintf(stderr, "Invalid %s!\n", cfgs[i].opt.name);
  113. goto usage;
  114. }
  115. break;
  116. }
  117. }
  118. if (!found) {
  119. switch (c) {
  120. case 'h': goto usage;
  121. case 'v': fprintf(stderr, "José %d\n", JOSE_VERSION); return false;
  122. default: fprintf(stderr, "Unknown option: %c!\n", c); goto usage;
  123. }
  124. }
  125. }
  126. return true;
  127. usage:
  128. fprintf(stderr, "Usage: %s\n\n", prefix);
  129. for (size_t i = 0; i < ncfgs; i++) {
  130. for (size_t j = 0; cfgs[i].doc[j].doc; j++) {
  131. const char *n = cfgs[i].opt.name;
  132. const char v = cfgs[i].opt.val;
  133. const char *a = cfgs[i].doc[j].arg;
  134. const char d = a ? '=' : ' ';
  135. a = a ? a : "";
  136. fprintf(stderr, " -%c %-*s --%s%c%-*s %s\n",
  137. v, maxa, a,
  138. n, d, maxl - (int) strlen(n), a,
  139. cfgs[i].doc[j].doc);
  140. }
  141. if (cfgs[i].def) {
  142. fprintf(stderr, "%*sDefault: \"%s\"\n",
  143. maxa + maxl + 11, "", cfgs[i].def);
  144. }
  145. fprintf(stderr, "\n");
  146. }
  147. return false;
  148. }
  149. static bool
  150. valid_b64(const char *b64, size_t len)
  151. {
  152. for (size_t i = 0; i < len; i++) {
  153. if (b64[i] != 0 && !strchr(JOSE_B64_MAP, b64[i]))
  154. return false;
  155. }
  156. return true;
  157. }
  158. static json_t *
  159. parse_compact(jcmd_opt_io_t *io, const char *arg)
  160. {
  161. json_auto_t *tmp = json_object();
  162. size_t i = 0;
  163. for (size_t j = 0; io->fields[j].name; j++) {
  164. const char *enc = strchr(&arg[i], '.');
  165. size_t len = strlen(&arg[i]);
  166. if (enc)
  167. len = enc - &arg[i];
  168. else if (io->fields[j + 1].name)
  169. return NULL;
  170. if (!valid_b64(&arg[i], len))
  171. return NULL;
  172. if (json_object_set_new(tmp, io->fields[j].name,
  173. json_stringn(&arg[i], len)) < 0)
  174. return NULL;
  175. i += len + 1;
  176. }
  177. return json_incref(tmp);
  178. }
  179. bool
  180. jcmd_opt_io_set_input(const jcmd_cfg_t *cfg, void *vopt, const char *arg)
  181. {
  182. jcmd_opt_io_t *io = vopt;
  183. jcmd_file_cleanup(&io->input);
  184. json_decrefp(&io->obj);
  185. io->obj = json_loads(arg, 0, NULL);
  186. if (!io->obj)
  187. io->obj = parse_compact(io, arg);
  188. if (!io->obj) {
  189. if (strcmp("-", arg) == 0)
  190. io->input = stdin;
  191. else
  192. io->input = fopen(arg, "r");
  193. if (!io->input)
  194. return false;
  195. if (is_json_object_file(io->input)) {
  196. io->obj = json_loadf(io->input, JSON_DISABLE_EOF_CHECK, NULL);
  197. jcmd_file_cleanup(&io->input);
  198. } else {
  199. io->obj = json_object();
  200. for (size_t i = 0;
  201. io->fields[i].name &&
  202. io->fields[i + 1].name &&
  203. io->fields[i + 2].name; i++) {
  204. if (json_object_set_new(io->obj, io->fields[i].name,
  205. jcmd_compact_field(io->input)) < 0)
  206. return false;
  207. }
  208. }
  209. }
  210. return json_is_object(io->obj);
  211. }
  212. bool
  213. jcmd_opt_set_ifile(const jcmd_cfg_t *cfg, void *vopt, const char *arg)
  214. {
  215. FILE **file = vopt;
  216. jcmd_file_cleanup(file);
  217. if (strcmp("-", arg) == 0)
  218. *file = stdin;
  219. else
  220. *file = fopen(arg, "r");
  221. return *file;
  222. }
  223. bool
  224. jcmd_opt_set_ofile(const jcmd_cfg_t *cfg, void *vopt, const char *arg)
  225. {
  226. FILE **file = vopt;
  227. jcmd_file_cleanup(file);
  228. if (strcmp("-", arg) == 0)
  229. *file = stdout;
  230. else
  231. *file = fopen(arg, "w");
  232. return *file;
  233. }
  234. bool
  235. jcmd_opt_set_jsons(const jcmd_cfg_t *cfg, void *vopt, const char *arg)
  236. {
  237. json_auto_t *tmp = NULL;
  238. json_t **json = vopt;
  239. if (!jcmd_opt_set_json(cfg, &tmp, arg))
  240. return false;
  241. if (!*json)
  242. *json = json_array();
  243. return json_array_append(*json, tmp) >= 0;
  244. }
  245. bool
  246. jcmd_opt_set_json(const jcmd_cfg_t *cfg, void *vopt, const char *arg)
  247. {
  248. const int flags = JSON_DISABLE_EOF_CHECK | JSON_DECODE_ANY;
  249. json_t **json = vopt;
  250. json_decrefp(json);
  251. *json = json_loads(arg, flags, NULL);
  252. if (!*json) {
  253. if (strcmp(arg, "-") == 0) {
  254. *json = json_loadf(stdin, flags, NULL);
  255. } else {
  256. FILE_AUTO *file = fopen(arg, "r");
  257. *json = json_loadf(file, flags, NULL);
  258. }
  259. }
  260. return *json;
  261. }
  262. bool
  263. jcmd_opt_set_jwkt(const jcmd_cfg_t *cfg, void *vopt, const char *arg)
  264. {
  265. const int flags = JSON_DISABLE_EOF_CHECK | JSON_DECODE_ANY;
  266. json_auto_t *tmp = NULL;
  267. json_t **jwks = vopt;
  268. if (!*jwks)
  269. *jwks = json_array();
  270. tmp = json_loads(arg, flags, NULL);
  271. if (!tmp) {
  272. if (strcmp(arg, "-") == 0) {
  273. tmp = json_loadf(stdin, flags, NULL);
  274. } else {
  275. FILE_AUTO *file = fopen(arg, "r");
  276. tmp = json_loadf(file, flags, NULL);
  277. }
  278. }
  279. if (!tmp)
  280. return false;
  281. switch (json_typeof(tmp)) {
  282. case JSON_OBJECT:
  283. case JSON_STRING:
  284. return jwks_extend(*jwks, json_incref(tmp));
  285. default:
  286. return false;
  287. }
  288. }
  289. bool
  290. jcmd_opt_set_jwks(const jcmd_cfg_t *cfg, void *vopt, const char *arg)
  291. {
  292. const int flags = JSON_DISABLE_EOF_CHECK | JSON_DECODE_ANY;
  293. json_auto_t *tmp = NULL;
  294. json_t **jwks = vopt;
  295. if (!*jwks)
  296. *jwks = json_array();
  297. if (strcmp(arg, "-") == 0) {
  298. tmp = json_loadf(stdin, flags, NULL);
  299. } else {
  300. FILE_AUTO *file = fopen(arg, "r");
  301. tmp = json_loadf(file, flags, NULL);
  302. }
  303. switch (tmp ? json_typeof(tmp) : JSON_INTEGER) {
  304. case JSON_OBJECT:
  305. case JSON_STRING:
  306. return jwks_extend(*jwks, json_incref(tmp));
  307. default:
  308. return false;
  309. }
  310. }
  311. bool
  312. jcmd_opt_set_flag(const jcmd_cfg_t *cfg, void *vopt, const char *arg)
  313. {
  314. bool *flag = vopt;
  315. return *flag = true;
  316. }
  317. void
  318. jcmd_opt_io_cleanup(jcmd_opt_io_t *io)
  319. {
  320. if (!io)
  321. return;
  322. jcmd_file_cleanup(&io->detached);
  323. jcmd_file_cleanup(&io->detach);
  324. jcmd_file_cleanup(&io->output);
  325. jcmd_file_cleanup(&io->input);
  326. json_decrefp(&io->obj);
  327. }
  328. json_t *
  329. jcmd_compact_field(FILE *file)
  330. {
  331. json_t *str = NULL;
  332. char *buf = NULL;
  333. size_t used = 0;
  334. size_t size = 0;
  335. for (int c = fgetc(file); c != EOF && c != '.'; c = fgetc(file)) {
  336. if (used >= size) {
  337. char *tmp = NULL;
  338. size += 4096;
  339. tmp = realloc(buf, size);
  340. if (!tmp)
  341. goto error;
  342. buf = tmp;
  343. }
  344. buf[used++] = c;
  345. }
  346. str = json_stringn(buf ? buf : "", buf ? used : 0);
  347. error:
  348. free(buf);
  349. return str;
  350. }
  351. void
  352. jcmd_file_cleanup(FILE **file)
  353. {
  354. if (file && *file) {
  355. if (*file != stdin && *file != stdout)
  356. fclose(*file);
  357. *file = NULL;
  358. }
  359. }
  360. static int
  361. nnames(const jcmd_t *cmd)
  362. {
  363. int n = 0;
  364. while (cmd->names[n])
  365. n++;
  366. return n;
  367. }
  368. static int
  369. cmp(const void *a, const void *b)
  370. {
  371. const jcmd_t * const *ap = a;
  372. const jcmd_t * const *bp = b;
  373. int c = 0;
  374. c = nnames(*ap) - nnames(*bp);
  375. for (size_t i = 0; c == 0; i++) {
  376. const char *an = (*ap)->names[i];
  377. const char *bn = (*bp)->names[i];
  378. if (!an && !bn)
  379. return 0;
  380. if (!an && bn)
  381. return -1;
  382. if (an && !bn)
  383. return 1;
  384. c = strcmp(an, bn);
  385. }
  386. return c;
  387. }
  388. int
  389. main(int argc, char *argv[])
  390. {
  391. const char *last = NULL;
  392. char full[40] = {};
  393. size_t len = 0;
  394. for (int i = 0; i < argc; i++)
  395. len += strlen(argv[i]) + 1;
  396. char cmd[len];
  397. len = 0;
  398. for (const jcmd_t *c = cmds; c; c = c->next) {
  399. strcpy(cmd, "jose");
  400. len++;
  401. for (int i = 1; i < argc && c->names[i - 1]; i++) {
  402. const char *name = c->names[i - 1];
  403. if (strcmp(argv[i], name) != 0)
  404. break;
  405. if (!c->names[i]) {
  406. argv[--i] = cmd;
  407. return c->func(argc - i, argv + i);
  408. }
  409. strcat(cmd, " ");
  410. strcat(cmd, name);
  411. }
  412. }
  413. const jcmd_t *all[len];
  414. for (const jcmd_t *c = cmds; c; c = c->next)
  415. all[--len] = c;
  416. qsort(all, sizeof(all) / sizeof(*all), sizeof(*all), cmp);
  417. fprintf(stderr, "Usage: jose COMMAND [OPTIONS] [ARGUMENTS]\n\n");
  418. fprintf(stderr, "Commands:\n");
  419. for (size_t i = 0; i < sizeof(all) / sizeof(*all); i++) {
  420. if (!(last && strcmp(all[i]->names[0], last) == 0))
  421. fprintf(stderr, "\n");
  422. strcpy(full, "jose");
  423. for (size_t j = 0; all[i]->names[j]; j++) {
  424. snprintf(full + strlen(full),
  425. sizeof(full) - strlen(full) - 1,
  426. " %s", all[i]->names[j]);
  427. }
  428. fprintf(stderr, " %-13s %s\n", full, all[i]->desc);
  429. last = all[i]->names[0];
  430. }
  431. fprintf(stderr, "\n");
  432. return EXIT_FAILURE;
  433. }