jose.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #pragma once
  18. #include <jose/jose.h>
  19. #include <getopt.h>
  20. #define __JCMD_AUTO(t) t ## _t __attribute__((cleanup(t ## _cleanup)))
  21. #define jcmd_opt_key_auto_t __JCMD_AUTO(jcmd_opt_key)
  22. #define jcmd_opt_io_auto_t __JCMD_AUTO(jcmd_opt_io)
  23. #define jcmd_opt_auto_t __JCMD_AUTO(jcmd_opt)
  24. #define FILE_AUTO FILE __attribute__((cleanup(jcmd_file_cleanup)))
  25. #define JCMD_REGISTER(summary, function, ...) \
  26. static void __attribute__((constructor)) \
  27. jcmd_ ## function ## _register(void) \
  28. { \
  29. static const char *names[] = { __VA_ARGS__, NULL }; \
  30. static jcmd_t cmd = { \
  31. .names = names, \
  32. .func = function, \
  33. .desc = summary \
  34. }; \
  35. jcmd_push(&cmd); \
  36. }
  37. typedef struct jcmd_cfg jcmd_cfg_t;
  38. typedef bool jcmd_set_t(const jcmd_cfg_t *cfg, void *vopt, const char *arg);
  39. typedef struct {
  40. const char *arg;
  41. const char *doc;
  42. } jcmd_doc_t;
  43. struct jcmd_cfg {
  44. const jcmd_doc_t *doc;
  45. struct option opt;
  46. const char *def;
  47. jcmd_set_t *set;
  48. off_t off;
  49. };
  50. typedef struct {
  51. const char *name;
  52. const char *mult;
  53. } jcmd_field_t;
  54. typedef struct {
  55. const jcmd_field_t *fields;
  56. FILE *detached;
  57. bool compact;
  58. FILE *detach;
  59. FILE *output;
  60. FILE *input;
  61. json_t *obj;
  62. } jcmd_opt_io_t;
  63. typedef struct jcmd jcmd_t;
  64. struct jcmd {
  65. const jcmd_t *next;
  66. const char *const *names;
  67. int (*func)(int argc, char *argv[]);
  68. const char *desc;
  69. };
  70. static const jcmd_doc_t jcmd_doc_key[] = {
  71. { .arg = "FILE", .doc="Read JWK(Set) from FILE" },
  72. { .arg = "-", .doc="Read JWK(Set) from standard input" },
  73. {}
  74. };
  75. void
  76. jcmd_push(jcmd_t *cmd);
  77. bool
  78. jcmd_opt_parse(int argc, char *argv[], const jcmd_cfg_t *cfgs, void *arg,
  79. const char *prefix);
  80. jcmd_set_t jcmd_opt_io_set_input; /* Takes jcmd_opt_io_t* */
  81. jcmd_set_t jcmd_opt_set_ifile; /* Takes FILE** */
  82. jcmd_set_t jcmd_opt_set_ofile; /* Takes FILE** */
  83. jcmd_set_t jcmd_opt_set_jsons; /* Takes json_t** */
  84. jcmd_set_t jcmd_opt_set_json; /* Takes json_t** */
  85. jcmd_set_t jcmd_opt_set_jwkt; /* Takes json_t** */
  86. jcmd_set_t jcmd_opt_set_jwks; /* Takes json_t** */
  87. jcmd_set_t jcmd_opt_set_flag; /* Takes bool* */
  88. void
  89. jcmd_opt_io_cleanup(jcmd_opt_io_t *io);
  90. void
  91. jcmd_opt_key_cleanup(jcmd_opt_io_t *io);
  92. json_t *
  93. jcmd_compact_field(FILE *file);
  94. void
  95. jcmd_file_cleanup(FILE **file);