Print chain to stdout with -o

weechat-hashes
C. McEnroe 2021-01-10 18:00:41 -05:00
parent cdff668d8f
commit c6cd90c2dd
4 changed files with 14 additions and 24 deletions

View File

@ -30,10 +30,10 @@
.Op Ar config ... .Op Ar config ...
. .
.Nm .Nm
.Fl o
.Op Fl S Ar bind .Op Fl S Ar bind
.Op Fl h Ar host .Op Fl h Ar host
.Op Fl p Ar port .Op Fl p Ar port
.Fl o Ar chain
.Op Ar config ... .Op Ar config ...
. .
.Nm .Nm
@ -224,10 +224,9 @@ Set nickname to
.Ar nick . .Ar nick .
The default nickname is the user's name. The default nickname is the user's name.
. .
.It Fl o Ar chain .It Fl o
Write the server certificate chain Print the server certificate chain
in PEM format to to standard output in PEM format
.Ar chain
and exit. and exit.
. .
.It Fl p Ar port , Cm port = Ar port .It Fl p Ar port , Cm port = Ar port
@ -318,7 +317,7 @@ sasl-external
Connect to the server Connect to the server
and write its certificate to a file: and write its certificate to a file:
.Bd -literal -offset indent .Bd -literal -offset indent
catgirl -h irc.example.org -o ~/.config/catgirl/example.pem catgirl -o -h irc.example.org > ~/.config/catgirl/example.pem
.Ed .Ed
.It .It
Configure Configure

10
chat.c
View File

@ -137,10 +137,10 @@ int main(int argc, char *argv[]) {
#endif #endif
bool insecure = false; bool insecure = false;
bool printCert = false;
const char *bind = NULL; const char *bind = NULL;
const char *host = NULL; const char *host = NULL;
const char *port = "6697"; const char *port = "6697";
const char *chain = NULL;
const char *trust = NULL; const char *trust = NULL;
const char *cert = NULL; const char *cert = NULL;
const char *priv = NULL; const char *priv = NULL;
@ -169,7 +169,7 @@ int main(int argc, char *argv[]) {
{ .val = 'k', .name = "priv", required_argument }, { .val = 'k', .name = "priv", required_argument },
{ .val = 'l', .name = "log", no_argument }, { .val = 'l', .name = "log", no_argument },
{ .val = 'n', .name = "nick", required_argument }, { .val = 'n', .name = "nick", required_argument },
{ .val = 'o', .name = "write-chain", required_argument }, { .val = 'o', .name = "print-chain", no_argument },
{ .val = 'p', .name = "port", required_argument }, { .val = 'p', .name = "port", required_argument },
{ .val = 'r', .name = "real", required_argument }, { .val = 'r', .name = "real", required_argument },
{ .val = 's', .name = "save", required_argument }, { .val = 's', .name = "save", required_argument },
@ -204,7 +204,7 @@ int main(int argc, char *argv[]) {
break; case 'k': priv = optarg; break; case 'k': priv = optarg;
break; case 'l': logEnable = true; break; case 'l': logEnable = true;
break; case 'n': nick = optarg; break; case 'n': nick = optarg;
break; case 'o': insecure = true; chain = optarg; break; case 'o': insecure = true; printCert = true;
break; case 'p': port = optarg; break; case 'p': port = optarg;
break; case 'r': real = optarg; break; case 'r': real = optarg;
break; case 's': save = optarg; break; case 's': save = optarg;
@ -238,9 +238,9 @@ int main(int argc, char *argv[]) {
commandCompleteAdd(); commandCompleteAdd();
ircConfig(insecure, trust, cert, priv); ircConfig(insecure, trust, cert, priv);
if (chain) { if (printCert) {
ircConnect(bind, host, port); ircConnect(bind, host, port);
ircWriteChain(chain); ircPrintCert();
ircClose(); ircClose();
return EX_OK; return EX_OK;
} }

2
chat.h
View File

@ -226,7 +226,7 @@ void ircConfig(
bool insecure, const char *trust, const char *cert, const char *priv bool insecure, const char *trust, const char *cert, const char *priv
); );
int ircConnect(const char *bind, const char *host, const char *port); int ircConnect(const char *bind, const char *host, const char *port);
void ircWriteChain(const char *path); void ircPrintCert(void);
void ircRecv(void); void ircRecv(void);
void ircSend(const char *ptr, size_t len); void ircSend(const char *ptr, size_t len);
void ircFormat(const char *format, ...) void ircFormat(const char *format, ...)

15
irc.c
View File

@ -160,20 +160,11 @@ int ircConnect(const char *bindHost, const char *host, const char *port) {
return sock; return sock;
} }
void ircWriteChain(const char *path) { void ircPrintCert(void) {
FILE *file = fopen(path, "w");
if (!file) err(EX_CANTCREAT, "%s", path);
int n = fprintf(file, "subject= %s\n", tls_peer_cert_subject(client));
if (n < 0) err(EX_IOERR, "%s", path);
size_t len; size_t len;
const byte *pem = tls_peer_cert_chain_pem(client, &len); const byte *pem = tls_peer_cert_chain_pem(client, &len);
len = fwrite(pem, len, 1, file); printf("subject= %s\n", tls_peer_cert_subject(client));
if (!len) err(EX_IOERR, "%s", path); fwrite(pem, len, 1, stdout);
int error = fclose(file);
if (error) err(EX_IOERR, "%s", path);
} }
enum { MessageCap = 8191 + 512 }; enum { MessageCap = 8191 + 512 };