From d3006fcc150f02df02ffeae11d8efbb15c6eaacd Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Wed, 16 Apr 2025 12:49:00 -0400 Subject: [PATCH] Use HTTP_AUTHORIZATION in CGI programs (Issue #246) --- cgi-bin/admin.c | 22 +++++++++++++--------- cgi-bin/classes.c | 24 ++++++++++++++++++------ cgi-bin/jobs.c | 24 ++++++++++++++++++------ cgi-bin/printers.c | 24 ++++++++++++++++++------ 4 files changed, 67 insertions(+), 27 deletions(-) diff --git a/cgi-bin/admin.c b/cgi-bin/admin.c index d02caf3239..7d19971137 100644 --- a/cgi-bin/admin.c +++ b/cgi-bin/admin.c @@ -1,7 +1,7 @@ /* * Administration CGI for CUPS. * - * Copyright © 2021-2024 by OpenPrinting + * Copyright © 2021-2025 by OpenPrinting * Copyright © 2007-2021 by Apple Inc. * Copyright © 1997-2007 by Easy Software Products. * @@ -65,17 +65,21 @@ main(void) fputs("DEBUG: admin.cgi started...\n", stderr); - http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); - - if (!http) + if ((http = httpConnect2(cupsGetServer(), ippGetPort(), /*addrlist*/NULL, AF_UNSPEC, cupsGetEncryption(), /*blocking*/1, /*msec*/30000, /*cancel*/NULL)) == NULL) { - perror("ERROR: Unable to connect to cupsd"); - fprintf(stderr, "DEBUG: cupsServer()=\"%s\"\n", - cupsServer() ? cupsServer() : "(null)"); - fprintf(stderr, "DEBUG: ippPort()=%d\n", ippPort()); - fprintf(stderr, "DEBUG: cupsEncryption()=%d\n", cupsEncryption()); + fprintf(stderr, "ERROR: Unable to connect to cupsd: %s\n", cupsGetErrorString()); + fprintf(stderr, "DEBUG: cupsGetServer()=\"%s\"\n", cupsGetServer() ? cupsGetServer() : "(null)"); + fprintf(stderr, "DEBUG: ippGetPort()=%d\n", ippGetPort()); + fprintf(stderr, "DEBUG: cupsGetEncryption()=%d\n", cupsGetEncryption()); exit(1); } + else + { + const char *authorization; /* HTTP_AUTHORIZATION value */ + + if ((authorization = getenv("HTTP_AUTHORIZATION")) != NULL && !strncmp(authorization, "Bearer ", 7)) + httpSetAuthString(http, "Bearer", authorization + 7); + } fprintf(stderr, "DEBUG: http=%p\n", (void *)http); diff --git a/cgi-bin/classes.c b/cgi-bin/classes.c index eb83a920d6..c176500677 100644 --- a/cgi-bin/classes.c +++ b/cgi-bin/classes.c @@ -1,7 +1,7 @@ /* * Class status CGI for CUPS. * - * Copyright © 2020-2024 by OpenPrinting. + * Copyright © 2020-2025 by OpenPrinting. * Copyright © 2007-2016 by Apple Inc. * Copyright © 1997-2006 by Easy Software Products. * @@ -9,10 +9,6 @@ * information. */ -/* - * Include necessary headers... - */ - #include "cgi-private.h" @@ -87,7 +83,23 @@ main(void) * Connect to the HTTP server... */ - http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); + if ((http = httpConnect2(cupsGetServer(), ippGetPort(), /*addrlist*/NULL, AF_UNSPEC, cupsGetEncryption(), /*blocking*/1, /*msec*/30000, /*cancel*/NULL)) == NULL) + { + fprintf(stderr, "ERROR: Unable to connect to cupsd: %s\n", cupsGetErrorString()); + fprintf(stderr, "DEBUG: cupsGetServer()=\"%s\"\n", cupsGetServer() ? cupsGetServer() : "(null)"); + fprintf(stderr, "DEBUG: ippGetPort()=%d\n", ippGetPort()); + fprintf(stderr, "DEBUG: cupsGetEncryption()=%d\n", cupsGetEncryption()); + exit(1); + } + else + { + const char *authorization; /* HTTP_AUTHORIZATION value */ + + if ((authorization = getenv("HTTP_AUTHORIZATION")) != NULL && !strncmp(authorization, "Bearer ", 7)) + httpSetAuthString(http, "Bearer", authorization + 7); + } + + fprintf(stderr, "DEBUG: http=%p\n", (void *)http); /* * Get the default printer... diff --git a/cgi-bin/jobs.c b/cgi-bin/jobs.c index 66a4f81a2a..3bbf184885 100644 --- a/cgi-bin/jobs.c +++ b/cgi-bin/jobs.c @@ -1,7 +1,7 @@ /* * Job status CGI for CUPS. * - * Copyright © 2020-2024 by OpenPrinting. + * Copyright © 2020-2025 by OpenPrinting. * Copyright © 2007-2014 by Apple Inc. * Copyright © 1997-2006 by Easy Software Products. * @@ -9,10 +9,6 @@ * information. */ -/* - * Include necessary headers... - */ - #include "cgi-private.h" @@ -53,7 +49,23 @@ main(void) * Connect to the HTTP server... */ - http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); + if ((http = httpConnect2(cupsGetServer(), ippGetPort(), /*addrlist*/NULL, AF_UNSPEC, cupsGetEncryption(), /*blocking*/1, /*msec*/30000, /*cancel*/NULL)) == NULL) + { + fprintf(stderr, "ERROR: Unable to connect to cupsd: %s\n", cupsGetErrorString()); + fprintf(stderr, "DEBUG: cupsGetServer()=\"%s\"\n", cupsGetServer() ? cupsGetServer() : "(null)"); + fprintf(stderr, "DEBUG: ippGetPort()=%d\n", ippGetPort()); + fprintf(stderr, "DEBUG: cupsGetEncryption()=%d\n", cupsGetEncryption()); + exit(1); + } + else + { + const char *authorization; /* HTTP_AUTHORIZATION value */ + + if ((authorization = getenv("HTTP_AUTHORIZATION")) != NULL && !strncmp(authorization, "Bearer ", 7)) + httpSetAuthString(http, "Bearer", authorization + 7); + } + + fprintf(stderr, "DEBUG: http=%p\n", (void *)http); /* * Get the job ID, if any... diff --git a/cgi-bin/printers.c b/cgi-bin/printers.c index b20c0a9ff2..9aeab33b46 100644 --- a/cgi-bin/printers.c +++ b/cgi-bin/printers.c @@ -1,7 +1,7 @@ /* * Printer status CGI for CUPS. * - * Copyright © 2020-2024 by OpenPrinting. + * Copyright © 2020-2025 by OpenPrinting. * Copyright © 2007-2016 by Apple Inc. * Copyright © 1997-2006 by Easy Software Products. * @@ -9,10 +9,6 @@ * information. */ -/* - * Include necessary headers... - */ - #include "cgi-private.h" #include @@ -88,7 +84,23 @@ main(void) * Connect to the HTTP server... */ - http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); + if ((http = httpConnect2(cupsGetServer(), ippGetPort(), /*addrlist*/NULL, AF_UNSPEC, cupsGetEncryption(), /*blocking*/1, /*msec*/30000, /*cancel*/NULL)) == NULL) + { + fprintf(stderr, "ERROR: Unable to connect to cupsd: %s\n", cupsGetErrorString()); + fprintf(stderr, "DEBUG: cupsGetServer()=\"%s\"\n", cupsGetServer() ? cupsGetServer() : "(null)"); + fprintf(stderr, "DEBUG: ippGetPort()=%d\n", ippGetPort()); + fprintf(stderr, "DEBUG: cupsGetEncryption()=%d\n", cupsGetEncryption()); + exit(1); + } + else + { + const char *authorization; /* HTTP_AUTHORIZATION value */ + + if ((authorization = getenv("HTTP_AUTHORIZATION")) != NULL && !strncmp(authorization, "Bearer ", 7)) + httpSetAuthString(http, "Bearer", authorization + 7); + } + + fprintf(stderr, "DEBUG: http=%p\n", (void *)http); /* * Get the default printer... -- 2.47.2