Skip to content

Commit

Permalink
tools: Hopefully some capitability for win32
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Collins <[email protected]>
  • Loading branch information
benmcollins committed Feb 13, 2025
1 parent ea9cc32 commit 546ce33
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 24 deletions.
6 changes: 3 additions & 3 deletions tools/jwk2key.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include <jwt.h>

#include "jwt-util.h"

static char *out_dir;
static int retry;

Expand Down Expand Up @@ -128,8 +130,6 @@ static int check_directory(const char *path)
}


extern const char *__progname;

_Noreturn static void usage(const char *error, int exit_state)
{
if (error)
Expand Down Expand Up @@ -160,7 +160,7 @@ written as a binary file (.bin extension).\n\
\n\
By default, existing files will not be overwritten. If you use the --retry\n\
option, an attempt will be made to add -X to the file name, up to -9, in\n\
an attempt to create the file.\n", __progname);
an attempt to create the file.\n", get_progname());

exit(exit_state);
}
Expand Down
4 changes: 1 addition & 3 deletions tools/jwt-generate.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

#include "jwt-util.h"

extern const char *__progname;

_Noreturn static void usage(const char *error, int exit_state)
{
if (error)
Expand Down Expand Up @@ -56,7 +54,7 @@ is useful if you wanted to use something like `jq -C` to colorize it. A\n\
non-0 exit status will stop the token from getting generated.\n\
\n\
If you need to convert a key to JWT (e.g. from PEM or DER format) see\n\
key2jwk(1).\n", __progname);
key2jwk(1).\n", get_progname());

exit(exit_state);
}
Expand Down
67 changes: 55 additions & 12 deletions tools/jwt-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,46 @@ static char *pipe_cmd;

static FILE *json_fp;

static int write_json(const char *title, const char *str)
#ifdef _WIN32
#include <windows.h>
#endif

static inline int write_json(const char *title, const char *str)
{
char *argv[4] = { "/bin/sh", "-c", NULL, NULL };
int exit_code = 0;

if (json_fp == NULL)
json_fp = stdout;

fprintf(json_fp, "\033[0;95m[%s]\033[0m\n", title);

#ifdef _WIN32
FILE *pipe_fp = NULL;
if (pipe_cmd) {
pipe_fp = _popen(pipe_cmd, "w");
if (!pipe_fp) {
perror("popen");
return -1;
}
}

if (pipe_fp) {
if (fwrite(str, 1, strlen(str), pipe_fp) < strlen(str)) {
perror("fwrite");
exit_code = -1;
}

_pclose(pipe_fp);
}
#else
int pipe_fd[2];
int myfd = 0;
pid_t pid;
int status;

if (json_fp == NULL)
json_fp = stdout;

if (pipe_cmd) {
char *argv[4] = { "/bin/sh", "-c", NULL, NULL };

if (pipe(pipe_fd)) {
perror(pipe_cmd);
exit(EXIT_FAILURE);
Expand All @@ -41,7 +69,6 @@ static int write_json(const char *title, const char *str)
close(pipe_fd[0]);

execvp(argv[0], argv);

perror("execvp");
exit(EXIT_FAILURE);
} else {
Expand All @@ -50,27 +77,29 @@ static int write_json(const char *title, const char *str)
}
}

fprintf(json_fp, "\033[0;95m[%s]\033[0m\n", title);

if (myfd) {
if (write(myfd, str, strlen(str)) < 0) {
perror(pipe_cmd);
exit(EXIT_FAILURE);
}
} else {
}
#endif
else {
fprintf(json_fp, "\033[0;96m%s\033[0m\n", str);
}

#ifndef _WIN32
if (myfd) {
close(myfd);
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
exit_code = WEXITSTATUS(status);
}
#endif

return 0;
return exit_code;
}

static int __jwt_wcb(jwt_t *jwt, jwt_config_t *config)
static inline int __jwt_wcb(jwt_t *jwt, jwt_config_t *config)
{
jwt_value_t jval;
int ret = 0, result = 0;
Expand All @@ -96,3 +125,17 @@ static int __jwt_wcb(jwt_t *jwt, jwt_config_t *config)

return result;
}

#ifdef _WIN32
extern char **__argv;
static inline const char *get_progname(void)
{
return __argv[0];
}
#else
extern const char *__progname;
static inline const char *get_progname(void)
{
return __progname;
}
#endif
4 changes: 1 addition & 3 deletions tools/jwt-verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

#include "jwt-util.h"

extern const char *__progname;

_Noreturn static void usage(const char *error, int exit_state)
{
if (error)
Expand Down Expand Up @@ -49,7 +47,7 @@ for the HEAD, and once for the PAYLOAD. A non-0 exit status will cause\n\
the verification to fail.\n\
\n\
If you need to convert a key to JWK (e.g. from PEM or DER format) see\n\
key2jwk(1).\n", __progname);
key2jwk(1).\n", get_progname());

exit(exit_state);
}
Expand Down
6 changes: 3 additions & 3 deletions tools/key2jwk.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#include <jwt.h>

#include "jwt-util.h"

/* We make use of some LibJWT internals. Soon, this code will move into LibJWT
* so it can be used to important PEM/DER keys into a JWK keyring. Until then,
* we hack around it here. */
Expand Down Expand Up @@ -327,8 +329,6 @@ static json_t *parse_one_file(const char *file)
return jwk;
}

extern const char *__progname;

_Noreturn static void usage(const char *error, int exit_state)
{
if (error)
Expand Down Expand Up @@ -360,7 +360,7 @@ RSA-PSS keys will be set to PS256, otherwise they will look no different\n\
than an RSA key.\n\
\n\
All keys will get a generated randomized uuidv4 \"kid\" attribute unless you\n\
use the -k option..\n", __progname);
use the -k option..\n", get_progname());

exit(exit_state);
}
Expand Down

0 comments on commit 546ce33

Please sign in to comment.