From 4d13be6f127d20efe6e496ef6d9f583092d35f09 Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Wed, 22 Mar 2023 16:23:41 +0530 Subject: [PATCH 01/16] Added the functionality of Preserved Comments in cupsd.conf when cupsctl is used with command line arguments --- cups/adminutil.c | 11 ++-- cups/file.c | 136 +++++++++++++++++++++++++++++++++++++++++++++++ cups/file.h | 1 + 3 files changed, 145 insertions(+), 3 deletions(-) diff --git a/cups/adminutil.c b/cups/adminutil.c index dadf5c438c..6a97158a4d 100644 --- a/cups/adminutil.c +++ b/cups/adminutil.c @@ -459,6 +459,8 @@ cupsAdminSetServerSettings( cups_option_t *cupsd_settings, /* New settings */ *setting; /* Current setting */ _cups_globals_t *cg = _cupsGlobals(); /* Global data */ + char comment_check[1024]; /* Comment already? */ + char comment[1024] = ""; /* Comment */ /* @@ -698,8 +700,11 @@ cupsAdminSetServerSettings( if (server_port <= 0) server_port = IPP_PORT; - while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum)) + while (_cupsFileGetConfAndComments(cupsd, line, sizeof(line), &value, &linenum, comment, sizeof(comment))) { + if(_cups_strcasecmp(comment, comment_check)) + cupsFilePrintf(temp, "\n%s\n", comment); + if ((!_cups_strcasecmp(line, "Port") || !_cups_strcasecmp(line, "Listen")) && (remote_admin >= 0 || remote_any >= 0 || share_printers >= 0)) { @@ -774,8 +779,6 @@ cupsAdminSetServerSettings( if (debug_logging) { - cupsFilePuts(temp, - "# Show troubleshooting information in error_log.\n"); cupsFilePuts(temp, "LogLevel debug\n"); } else @@ -1059,6 +1062,8 @@ cupsAdminSetServerSettings( } else cupsFilePrintf(temp, "%*s%s\n", indent, "", line); + + _cups_strcpy(comment_check, comment); } /* diff --git a/cups/file.c b/cups/file.c index 2fd0780804..ff995bbcb9 100644 --- a/cups/file.c +++ b/cups/file.c @@ -841,6 +841,142 @@ cupsFileGetConf(cups_file_t *fp, /* I - CUPS file */ return (NULL); } +/* + * 'cupsFileGetConfAndComments()' - Get a line and Comments from a configuration file. + */ + +char * /* O - Line read or @code NULL@ on end of file or error */ +_cupsFileGetConfAndComments( + cups_file_t *fp, /* I - CUPS file */ + char *buf, /* O - String buffer */ + size_t buflen, /* I - Size of string buffer */ + char **value, /* O - Pointer to value */ + int *linenum, /* IO - Current line number */ + char *comment, /* I - Comment */ + size_t commentlen) /* I - Size of comment buffer */ +{ + char *ptr; /* Pointer into line */ + +/* + * Range check input... + */ + + DEBUG_printf(("2cupsFileGetConfAndComments(fp=%p, buf=%p, buflen=" CUPS_LLFMT + ", value=%p, linenum=%p, comment=%p, commentlen=" CUPS_LLFMT ", comment_linenum=%p)", (void *)fp, (void *)buf, CUPS_LLCAST buflen, (void *)value, (void *)linenum, (void *)comment, CUPS_LLCAST commentlen)); + + if (!fp || (fp->mode != 'r' && fp->mode != 's') || + !buf || buflen < 2 || !value || !comment || commentlen<2) + { + if (value) + *value = NULL; + + return (NULL); + } + + /* + * Read the next non-comment line... + */ + + *value = NULL; + + while (cupsFileGets(fp, buf, buflen)) + { + (*linenum) ++; + + /* + * Strip any comments... + */ + + if ((ptr = strchr(buf, '#')) != NULL) + { + if (ptr > buf && ptr[-1] == '\\') + { + // Unquote the #... + _cups_strcpy(ptr - 1, ptr); + } + else + { + // Strip the comment and any trailing whitespace... + while (ptr > buf) + { + if (!_cups_isspace(ptr[-1])) + break; + + ptr --; + } + + _cups_strcpy(comment, buf); + *ptr = '\0'; + } + } + + /* + * Strip leading whitespace... + */ + + for (ptr = buf; _cups_isspace(*ptr); ptr ++); + + if (ptr > buf) + _cups_strcpy(buf, ptr); + + /* + * See if there is anything left... + */ + + if (buf[0]) + { + /* + * Yes, grab any value and return... + */ + + for (ptr = buf; *ptr; ptr ++) + if (_cups_isspace(*ptr)) + break; + + if (*ptr) + { + /* + * Have a value, skip any other spaces... + */ + + while (_cups_isspace(*ptr)) + *ptr++ = '\0'; + + if (*ptr) + *value = ptr; + + /* + * Strip trailing whitespace and > for lines that begin with <... + */ + + ptr += strlen(ptr) - 1; + + if (buf[0] == '<' && *ptr == '>') + *ptr-- = '\0'; + else if (buf[0] == '<' && *ptr != '>') + { + /* + * Syntax error... + */ + + *value = NULL; + return (buf); + } + + while (ptr > *value && _cups_isspace(*ptr)) + *ptr-- = '\0'; + } + + /* + * Return the line... + */ + + return (buf); + } + } + + return (NULL); +} /* * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may diff --git a/cups/file.h b/cups/file.h index 49ca58a0c4..fe8947e9fd 100644 --- a/cups/file.h +++ b/cups/file.h @@ -59,6 +59,7 @@ typedef struct _cups_file_s cups_file_t;/**** CUPS file type ****/ * Prototypes... */ +extern char *_cupsFileGetConfAndComments(cups_file_t *fp, char *buf, size_t buflen, char **value, int *linenum, char *comment, size_t commentlen) _CUPS_PRIVATE; extern int cupsFileClose(cups_file_t *fp) _CUPS_API_1_2; extern int cupsFileCompression(cups_file_t *fp) _CUPS_API_1_2; extern int cupsFileEOF(cups_file_t *fp) _CUPS_API_1_2; From b9c57d1ca9c2cbccbd4af4e0e50d0833e5ae2b85 Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Thu, 23 Mar 2023 00:29:16 +0530 Subject: [PATCH 02/16] Done some changes in cupsFileGetConf() to preserve conmments in cupsd.conf --- cups/adminutil.c | 23 +++++--- cups/file.c | 137 +---------------------------------------------- cups/file.h | 1 - 3 files changed, 17 insertions(+), 144 deletions(-) diff --git a/cups/adminutil.c b/cups/adminutil.c index 6a97158a4d..ca8db9e19e 100644 --- a/cups/adminutil.c +++ b/cups/adminutil.c @@ -459,8 +459,7 @@ cupsAdminSetServerSettings( cups_option_t *cupsd_settings, /* New settings */ *setting; /* Current setting */ _cups_globals_t *cg = _cupsGlobals(); /* Global data */ - char comment_check[1024]; /* Comment already? */ - char comment[1024] = ""; /* Comment */ + int linenum_check; /* same linenumber? */ /* @@ -669,6 +668,7 @@ cupsAdminSetServerSettings( * Copy the old file to the new, making changes along the way... */ + linenum_check = 0; cupsd_num_settings = 0; in_admin_location = 0; in_cancel_job = 0; @@ -700,10 +700,17 @@ cupsAdminSetServerSettings( if (server_port <= 0) server_port = IPP_PORT; - while (_cupsFileGetConfAndComments(cupsd, line, sizeof(line), &value, &linenum, comment, sizeof(comment))) + while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum)) { - if(_cups_strcasecmp(comment, comment_check)) - cupsFilePrintf(temp, "\n%s\n", comment); + if (strchr(line, '#') != NULL) + { + if(linenum == linenum_check+1) + cupsFilePrintf(temp, "%s\n", line); + else + cupsFilePrintf(temp, "\n%s\n", line); + linenum_check = linenum; + } + if ((!_cups_strcasecmp(line, "Port") || !_cups_strcasecmp(line, "Listen")) && (remote_admin >= 0 || remote_any >= 0 || share_printers >= 0)) @@ -1061,9 +1068,11 @@ cupsAdminSetServerSettings( cupsFilePrintf(temp, "%*s%s %s\n", indent, "", line, value); } else - cupsFilePrintf(temp, "%*s%s\n", indent, "", line); + { + if (strchr(line, '#') == NULL) + cupsFilePrintf(temp, "%*s%s\n", indent, "", line); + } - _cups_strcpy(comment_check, comment); } /* diff --git a/cups/file.c b/cups/file.c index ff995bbcb9..211da92e23 100644 --- a/cups/file.c +++ b/cups/file.c @@ -769,6 +769,7 @@ cupsFileGetConf(cups_file_t *fp, /* I - CUPS file */ ptr --; } + return (buf); *ptr = '\0'; } } @@ -841,142 +842,6 @@ cupsFileGetConf(cups_file_t *fp, /* I - CUPS file */ return (NULL); } -/* - * 'cupsFileGetConfAndComments()' - Get a line and Comments from a configuration file. - */ - -char * /* O - Line read or @code NULL@ on end of file or error */ -_cupsFileGetConfAndComments( - cups_file_t *fp, /* I - CUPS file */ - char *buf, /* O - String buffer */ - size_t buflen, /* I - Size of string buffer */ - char **value, /* O - Pointer to value */ - int *linenum, /* IO - Current line number */ - char *comment, /* I - Comment */ - size_t commentlen) /* I - Size of comment buffer */ -{ - char *ptr; /* Pointer into line */ - -/* - * Range check input... - */ - - DEBUG_printf(("2cupsFileGetConfAndComments(fp=%p, buf=%p, buflen=" CUPS_LLFMT - ", value=%p, linenum=%p, comment=%p, commentlen=" CUPS_LLFMT ", comment_linenum=%p)", (void *)fp, (void *)buf, CUPS_LLCAST buflen, (void *)value, (void *)linenum, (void *)comment, CUPS_LLCAST commentlen)); - - if (!fp || (fp->mode != 'r' && fp->mode != 's') || - !buf || buflen < 2 || !value || !comment || commentlen<2) - { - if (value) - *value = NULL; - - return (NULL); - } - - /* - * Read the next non-comment line... - */ - - *value = NULL; - - while (cupsFileGets(fp, buf, buflen)) - { - (*linenum) ++; - - /* - * Strip any comments... - */ - - if ((ptr = strchr(buf, '#')) != NULL) - { - if (ptr > buf && ptr[-1] == '\\') - { - // Unquote the #... - _cups_strcpy(ptr - 1, ptr); - } - else - { - // Strip the comment and any trailing whitespace... - while (ptr > buf) - { - if (!_cups_isspace(ptr[-1])) - break; - - ptr --; - } - - _cups_strcpy(comment, buf); - *ptr = '\0'; - } - } - - /* - * Strip leading whitespace... - */ - - for (ptr = buf; _cups_isspace(*ptr); ptr ++); - - if (ptr > buf) - _cups_strcpy(buf, ptr); - - /* - * See if there is anything left... - */ - - if (buf[0]) - { - /* - * Yes, grab any value and return... - */ - - for (ptr = buf; *ptr; ptr ++) - if (_cups_isspace(*ptr)) - break; - - if (*ptr) - { - /* - * Have a value, skip any other spaces... - */ - - while (_cups_isspace(*ptr)) - *ptr++ = '\0'; - - if (*ptr) - *value = ptr; - - /* - * Strip trailing whitespace and > for lines that begin with <... - */ - - ptr += strlen(ptr) - 1; - - if (buf[0] == '<' && *ptr == '>') - *ptr-- = '\0'; - else if (buf[0] == '<' && *ptr != '>') - { - /* - * Syntax error... - */ - - *value = NULL; - return (buf); - } - - while (ptr > *value && _cups_isspace(*ptr)) - *ptr-- = '\0'; - } - - /* - * Return the line... - */ - - return (buf); - } - } - - return (NULL); -} /* * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may diff --git a/cups/file.h b/cups/file.h index fe8947e9fd..49ca58a0c4 100644 --- a/cups/file.h +++ b/cups/file.h @@ -59,7 +59,6 @@ typedef struct _cups_file_s cups_file_t;/**** CUPS file type ****/ * Prototypes... */ -extern char *_cupsFileGetConfAndComments(cups_file_t *fp, char *buf, size_t buflen, char **value, int *linenum, char *comment, size_t commentlen) _CUPS_PRIVATE; extern int cupsFileClose(cups_file_t *fp) _CUPS_API_1_2; extern int cupsFileCompression(cups_file_t *fp) _CUPS_API_1_2; extern int cupsFileEOF(cups_file_t *fp) _CUPS_API_1_2; From ccf71a9e71dd7e5e136ad7bdbd2ce5183cc5dcc0 Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Wed, 29 Mar 2023 09:34:07 +0530 Subject: [PATCH 03/16] Added: Private Api to read comments and configuration from cupsd.conf --- cups/adminutil.c | 35 +----------- cups/file.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++- cups/file.h | 1 + 3 files changed, 138 insertions(+), 35 deletions(-) diff --git a/cups/adminutil.c b/cups/adminutil.c index ca8db9e19e..d4a284c630 100644 --- a/cups/adminutil.c +++ b/cups/adminutil.c @@ -700,7 +700,7 @@ cupsAdminSetServerSettings( if (server_port <= 0) server_port = IPP_PORT; - while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum)) + while (_cupsFileGetConfAndComments(cupsd, line, sizeof(line), &value, &linenum)) { if (strchr(line, '#') != NULL) { @@ -721,7 +721,6 @@ cupsAdminSetServerSettings( if (remote_admin > 0 || remote_any > 0 || share_printers > 0) { - cupsFilePuts(temp, "# Allow remote access\n"); cupsFilePrintf(temp, "Port %d\n", server_port); } else @@ -761,7 +760,6 @@ cupsAdminSetServerSettings( localp = cupsGetOption("BrowseLocalProtocols", cupsd_num_settings, cupsd_settings); - cupsFilePuts(temp, "# Share local printers on the local network.\n"); cupsFilePuts(temp, "Browsing On\n"); if (!localp) @@ -847,11 +845,6 @@ cupsAdminSetServerSettings( { wrote_admin_location = 1; - if (remote_admin) - cupsFilePuts(temp, " # Allow remote administration...\n"); - else - cupsFilePuts(temp, " # Restrict access to the admin pages...\n"); - cupsFilePuts(temp, " Order allow,deny\n"); if (remote_admin) @@ -866,13 +859,6 @@ cupsAdminSetServerSettings( { wrote_conf_location = 1; - if (remote_admin) - cupsFilePuts(temp, " # Allow remote access to the configuration " - "files...\n"); - else - cupsFilePuts(temp, " # Restrict access to the configuration " - "files...\n"); - cupsFilePuts(temp, " Order allow,deny\n"); if (remote_admin) @@ -887,13 +873,6 @@ cupsAdminSetServerSettings( { wrote_log_location = 1; - if (remote_admin) - cupsFilePuts(temp, " # Allow remote access to the log " - "files...\n"); - else - cupsFilePuts(temp, " # Restrict access to the log " - "files...\n"); - cupsFilePuts(temp, " Order allow,deny\n"); if (remote_admin) @@ -909,18 +888,6 @@ cupsAdminSetServerSettings( { wrote_root_location = 1; - if (remote_admin > 0 && share_printers > 0) - cupsFilePuts(temp, " # Allow shared printing and remote " - "administration...\n"); - else if (remote_admin > 0) - cupsFilePuts(temp, " # Allow remote administration...\n"); - else if (share_printers > 0) - cupsFilePuts(temp, " # Allow shared printing...\n"); - else if (remote_any > 0) - cupsFilePuts(temp, " # Allow remote access...\n"); - else - cupsFilePuts(temp, " # Restrict access to the server...\n"); - cupsFilePuts(temp, " Order allow,deny\n"); if (remote_admin > 0 || remote_any > 0 || share_printers > 0) diff --git a/cups/file.c b/cups/file.c index 211da92e23..582af00869 100644 --- a/cups/file.c +++ b/cups/file.c @@ -769,7 +769,142 @@ cupsFileGetConf(cups_file_t *fp, /* I - CUPS file */ ptr --; } - return (buf); + *ptr = '\0'; + } + } + + /* + * Strip leading whitespace... + */ + + for (ptr = buf; _cups_isspace(*ptr); ptr ++); + + if (ptr > buf) + _cups_strcpy(buf, ptr); + + /* + * See if there is anything left... + */ + + if (buf[0]) + { + /* + * Yes, grab any value and return... + */ + + for (ptr = buf; *ptr; ptr ++) + if (_cups_isspace(*ptr)) + break; + + if (*ptr) + { + /* + * Have a value, skip any other spaces... + */ + + while (_cups_isspace(*ptr)) + *ptr++ = '\0'; + + if (*ptr) + *value = ptr; + + /* + * Strip trailing whitespace and > for lines that begin with <... + */ + + ptr += strlen(ptr) - 1; + + if (buf[0] == '<' && *ptr == '>') + *ptr-- = '\0'; + else if (buf[0] == '<' && *ptr != '>') + { + /* + * Syntax error... + */ + + *value = NULL; + return (buf); + } + + while (ptr > *value && _cups_isspace(*ptr)) + *ptr-- = '\0'; + } + + /* + * Return the line... + */ + + return (buf); + } + } + + return (NULL); +} + + +/* + * 'cupsFileGetConfAndComments()' - Get line and comments from a configuration file. + */ + +char * /* O - Line read or @code NULL@ on end of file or error */ +_cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ + char *buf, /* O - String buffer */ + size_t buflen, /* I - Size of string buffer */ + char **value, /* O - Pointer to value */ + int *linenum) /* IO - Current line number */ +{ + char *ptr; /* Pointer into line */ + + + /* + * Range check input... + */ + + DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT + ", value=%p, linenum=%p)", (void *)fp, (void *)buf, CUPS_LLCAST buflen, (void *)value, (void *)linenum)); + + if (!fp || (fp->mode != 'r' && fp->mode != 's') || + !buf || buflen < 2 || !value) + { + if (value) + *value = NULL; + + return (NULL); + } + + /* + * Read the next non-comment line... + */ + + *value = NULL; + + while (cupsFileGets(fp, buf, buflen)) + { + (*linenum) ++; + + /* + * Strip any comments... + */ + + if ((ptr = strchr(buf, '#')) != NULL) + { + if (ptr > buf && ptr[-1] == '\\') + { + // Unquote the #... + _cups_strcpy(ptr - 1, ptr); + } + else + { + // Strip the comment and any trailing whitespace... + while (ptr > buf) + { + if (!_cups_isspace(ptr[-1])) + break; + + ptr --; + } + + return (buf); *ptr = '\0'; } } diff --git a/cups/file.h b/cups/file.h index 49ca58a0c4..739ee1a4ea 100644 --- a/cups/file.h +++ b/cups/file.h @@ -59,6 +59,7 @@ typedef struct _cups_file_s cups_file_t;/**** CUPS file type ****/ * Prototypes... */ +extern char *_cupsFileGetConfAndComments(cups_file_t *fp, char *buf, size_t buflen, char **value, int *linenum) _CUPS_PRIVATE; extern int cupsFileClose(cups_file_t *fp) _CUPS_API_1_2; extern int cupsFileCompression(cups_file_t *fp) _CUPS_API_1_2; extern int cupsFileEOF(cups_file_t *fp) _CUPS_API_1_2; From d999b0d42e4794481d20f4a66b47821488c895d6 Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Sat, 1 Apr 2023 16:59:05 +0530 Subject: [PATCH 04/16] Fixed : Coding Style issues --- cups/adminutil.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/cups/adminutil.c b/cups/adminutil.c index d4a284c630..03b3955312 100644 --- a/cups/adminutil.c +++ b/cups/adminutil.c @@ -702,16 +702,6 @@ cupsAdminSetServerSettings( while (_cupsFileGetConfAndComments(cupsd, line, sizeof(line), &value, &linenum)) { - if (strchr(line, '#') != NULL) - { - if(linenum == linenum_check+1) - cupsFilePrintf(temp, "%s\n", line); - else - cupsFilePrintf(temp, "\n%s\n", line); - linenum_check = linenum; - } - - if ((!_cups_strcasecmp(line, "Port") || !_cups_strcasecmp(line, "Listen")) && (remote_admin >= 0 || remote_any >= 0 || share_printers >= 0)) { @@ -1036,8 +1026,16 @@ cupsAdminSetServerSettings( } else { - if (strchr(line, '#') == NULL) - cupsFilePrintf(temp, "%*s%s\n", indent, "", line); + if (strchr(line, '#') == NULL) + cupsFilePrintf(temp, "%*s%s\n", indent, "", line); + else + { + if(linenum == linenum_check+1) + cupsFilePrintf(temp, "%s\n", line); + else + cupsFilePrintf(temp, "\n%s\n", line); + linenum_check = linenum; + } } } From 1c0ae059d31bc26d813f916d4441108286872098 Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Mon, 3 Apr 2023 19:12:46 +0530 Subject: [PATCH 05/16] Updated: cupsd.conf.in --- conf/cupsd.conf.in | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/conf/cupsd.conf.in b/conf/cupsd.conf.in index 31ecacb110..cdbaeb3959 100644 --- a/conf/cupsd.conf.in +++ b/conf/cupsd.conf.in @@ -31,26 +31,35 @@ WebInterface @CUPS_WEBIF@ # Timeout after cupsd exits if idle (applied only if cupsd runs on-demand - with -l) IdleExitTimeout @EXIT_TIMEOUT@ -# Restrict access to the server... +# Access to the server root (/): +# - default action defined by 'Order', see 'man cupsd.conf' +# - use 'Allow'/'Deny' for configuring access +# - allowing access is required for printer sharing or remote administration Order allow,deny -# Restrict access to the admin pages... +# Access to the admin pages: +# - default action defined by Order', see 'man cupsd.conf' +# - use 'Allow'/'Deny' for configuring access AuthType Default Require user @SYSTEM Order allow,deny -# Restrict access to configuration files... +# Access to the configuration files: +# - default action defined by Order', see 'man cupsd.conf' +# - use 'Allow'/'Deny' for configuring access AuthType Default Require user @SYSTEM Order allow,deny -# Restrict access to log files... +# Access to the log files: +# - default action defined by Order', see 'man cupsd.conf' +# - use 'Allow'/'Deny' for configuring access AuthType Default Require user @SYSTEM From d08c54ffccc532d007d83d990fbaa7f9da83c39c Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Thu, 6 Apr 2023 02:34:15 +0530 Subject: [PATCH 06/16] Added: Support to read inline comments from cupsd.conf --- conf/cupsd.conf.in | 3 +- cups/adminutil.c | 20 +-- cups/file.c | 339 +++++++++++++++++++++++++++------------------ 3 files changed, 205 insertions(+), 157 deletions(-) diff --git a/conf/cupsd.conf.in b/conf/cupsd.conf.in index cdbaeb3959..fe1ec28fdb 100644 --- a/conf/cupsd.conf.in +++ b/conf/cupsd.conf.in @@ -3,8 +3,7 @@ # complete description of this file. # -# Log general information in error_log - change "@CUPS_LOG_LEVEL@" to "debug" -# for troubleshooting... +# Set LogLevel to debug for turning on troubleshooting LogLevel @CUPS_LOG_LEVEL@ @CUPS_PAGE_LOG_FORMAT@ diff --git a/cups/adminutil.c b/cups/adminutil.c index 03b3955312..fb4f075466 100644 --- a/cups/adminutil.c +++ b/cups/adminutil.c @@ -459,7 +459,6 @@ cupsAdminSetServerSettings( cups_option_t *cupsd_settings, /* New settings */ *setting; /* Current setting */ _cups_globals_t *cg = _cupsGlobals(); /* Global data */ - int linenum_check; /* same linenumber? */ /* @@ -668,7 +667,6 @@ cupsAdminSetServerSettings( * Copy the old file to the new, making changes along the way... */ - linenum_check = 0; cupsd_num_settings = 0; in_admin_location = 0; in_cancel_job = 0; @@ -715,8 +713,6 @@ cupsAdminSetServerSettings( } else { - cupsFilePuts(temp, "# Only listen for connections from the local " - "machine.\n"); cupsFilePrintf(temp, "Listen localhost:%d\n", server_port); } @@ -763,7 +759,6 @@ cupsAdminSetServerSettings( } else { - cupsFilePuts(temp, "# Disable printer sharing.\n"); cupsFilePuts(temp, "Browsing Off\n"); } } @@ -778,7 +773,6 @@ cupsAdminSetServerSettings( } else { - cupsFilePuts(temp, "# Show general information in error_log.\n"); cupsFilePuts(temp, "LogLevel " CUPS_DEFAULT_LOG_LEVEL "\n"); } } @@ -1025,19 +1019,7 @@ cupsAdminSetServerSettings( cupsFilePrintf(temp, "%*s%s %s\n", indent, "", line, value); } else - { - if (strchr(line, '#') == NULL) - cupsFilePrintf(temp, "%*s%s\n", indent, "", line); - else - { - if(linenum == linenum_check+1) - cupsFilePrintf(temp, "%s\n", line); - else - cupsFilePrintf(temp, "\n%s\n", line); - linenum_check = linenum; - } - } - + cupsFilePrintf(temp, "%*s%s\n", indent, "", line); } /* diff --git a/cups/file.c b/cups/file.c index 582af00869..6e7e4bfef4 100644 --- a/cups/file.c +++ b/cups/file.c @@ -22,6 +22,7 @@ #include "debug-internal.h" #include #include +#include # ifdef HAVE_LIBZ # include @@ -335,6 +336,208 @@ _cupsFileCheckFilter( #endif /* !_WIN32 */ +/* + * 'cupsFileGetConfAndComments()' - Get line and comments from a configuration file. + */ + +char * /* O - Line read or @code NULL@ on end of file or error */ +_cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ + char *buf, /* O - String buffer */ + size_t buflen, /* I - Size of string buffer */ + char **value, /* O - Pointer to value */ + int *linenum) /* IO - Current line number */ +{ + char *ptr; /* Pointer into line */ + + + /* + * Range check input... + */ + + DEBUG_printf(("2cupsFileGetConfAndComments(fp=%p, buf=%p, buflen=" CUPS_LLFMT + ", value=%p, linenum=%p)", (void *)fp, (void *)buf, CUPS_LLCAST buflen, (void *)value, (void *)linenum)); + + if (!fp || (fp->mode != 'r' && fp->mode != 's') || + !buf || buflen < 2 || !value) + { + if (value) + *value = NULL; + + return (NULL); + } + + /* + * Read the next line... + */ + + *value = NULL; + + while (cupsFileGets(fp, buf, buflen)) + { + (*linenum) ++; + + // Read the comment... + if ((ptr = strchr(buf, '#')) != NULL) + { + bool inline_comment = false; + int index = (int) (ptr - buf); + for(int i=0;i for lines that begin with <... + */ + + ptr += strlen(ptr) - 1; + + if (buf[0] == '<' && *ptr == '>') + *ptr-- = '\0'; + else if (buf[0] == '<' && *ptr != '>') + { + /* + * Syntax error... + */ + + *value = NULL; + return (buf); + } + + while (ptr > *value && _cups_isspace(*ptr)) + *ptr-- = '\0'; + } + + /* + * Return the line... + */ + + return (buf); + } + + } + else + { + /* + * Strip leading whitespace... + */ + + for (ptr = buf; _cups_isspace(*ptr); ptr ++); + + if (ptr > buf) + _cups_strcpy(buf, ptr); + return (buf); + } + + } + else + { + /* + * Strip leading whitespace... + */ + + for (ptr = buf; _cups_isspace(*ptr); ptr ++); + + if (ptr > buf) + _cups_strcpy(buf, ptr); + + /* + * See if there is anything left... + */ + + if (buf[0]) + { + /* + * Yes, grab any value and return... + */ + + for (ptr = buf; *ptr; ptr ++) + if (_cups_isspace(*ptr)) + break; + + if (*ptr) + { + /* + * Have a value, skip any other spaces... + */ + + while (_cups_isspace(*ptr)) + *ptr++ = '\0'; + + if (*ptr) + *value = ptr; + + /* + * Strip trailing whitespace and > for lines that begin with <... + */ + + ptr += strlen(ptr) - 1; + + if (buf[0] == '<' && *ptr == '>') + *ptr-- = '\0'; + else if (buf[0] == '<' && *ptr != '>') + { + /* + * Syntax error... + */ + + *value = NULL; + return (buf); + } + + while (ptr > *value && _cups_isspace(*ptr)) + *ptr-- = '\0'; + } + + /* + * Return the line... + */ + + return (buf); + } + return (buf); + } + } + + return (NULL); +} + + /* * 'cupsFileClose()' - Close a CUPS file. * @@ -842,142 +1045,6 @@ cupsFileGetConf(cups_file_t *fp, /* I - CUPS file */ } -/* - * 'cupsFileGetConfAndComments()' - Get line and comments from a configuration file. - */ - -char * /* O - Line read or @code NULL@ on end of file or error */ -_cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ - char *buf, /* O - String buffer */ - size_t buflen, /* I - Size of string buffer */ - char **value, /* O - Pointer to value */ - int *linenum) /* IO - Current line number */ -{ - char *ptr; /* Pointer into line */ - - - /* - * Range check input... - */ - - DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT - ", value=%p, linenum=%p)", (void *)fp, (void *)buf, CUPS_LLCAST buflen, (void *)value, (void *)linenum)); - - if (!fp || (fp->mode != 'r' && fp->mode != 's') || - !buf || buflen < 2 || !value) - { - if (value) - *value = NULL; - - return (NULL); - } - - /* - * Read the next non-comment line... - */ - - *value = NULL; - - while (cupsFileGets(fp, buf, buflen)) - { - (*linenum) ++; - - /* - * Strip any comments... - */ - - if ((ptr = strchr(buf, '#')) != NULL) - { - if (ptr > buf && ptr[-1] == '\\') - { - // Unquote the #... - _cups_strcpy(ptr - 1, ptr); - } - else - { - // Strip the comment and any trailing whitespace... - while (ptr > buf) - { - if (!_cups_isspace(ptr[-1])) - break; - - ptr --; - } - - return (buf); - *ptr = '\0'; - } - } - - /* - * Strip leading whitespace... - */ - - for (ptr = buf; _cups_isspace(*ptr); ptr ++); - - if (ptr > buf) - _cups_strcpy(buf, ptr); - - /* - * See if there is anything left... - */ - - if (buf[0]) - { - /* - * Yes, grab any value and return... - */ - - for (ptr = buf; *ptr; ptr ++) - if (_cups_isspace(*ptr)) - break; - - if (*ptr) - { - /* - * Have a value, skip any other spaces... - */ - - while (_cups_isspace(*ptr)) - *ptr++ = '\0'; - - if (*ptr) - *value = ptr; - - /* - * Strip trailing whitespace and > for lines that begin with <... - */ - - ptr += strlen(ptr) - 1; - - if (buf[0] == '<' && *ptr == '>') - *ptr-- = '\0'; - else if (buf[0] == '<' && *ptr != '>') - { - /* - * Syntax error... - */ - - *value = NULL; - return (buf); - } - - while (ptr > *value && _cups_isspace(*ptr)) - *ptr-- = '\0'; - } - - /* - * Return the line... - */ - - return (buf); - } - } - - return (NULL); -} - - /* * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may * contain binary data. From 0a6b3b3b3df8e7231cf13b96b7ac5f9ffd8bc2ab Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Thu, 6 Apr 2023 06:41:03 +0530 Subject: [PATCH 07/16] Simplified : _cupsFileGetConfAndComments function logic --- cups/adminutil.c | 6 +- cups/file.c | 191 ++++++++++++++--------------------------------- 2 files changed, 59 insertions(+), 138 deletions(-) diff --git a/cups/adminutil.c b/cups/adminutil.c index fb4f075466..b4e03fb44e 100644 --- a/cups/adminutil.c +++ b/cups/adminutil.c @@ -792,8 +792,7 @@ cupsAdminSetServerSettings( wrote_policy = 1; if (!user_cancel_any) - cupsFilePuts(temp, " # Only the owner or an administrator can " - "cancel a job...\n" + cupsFilePuts(temp, " \n" " Order deny,allow\n" " Require user @OWNER " @@ -952,8 +951,7 @@ cupsAdminSetServerSettings( wrote_policy = 1; if (!user_cancel_any) - cupsFilePuts(temp, " # Only the owner or an administrator can cancel " - "a job...\n" + cupsFilePuts(temp, " \n" " Order deny,allow\n" " Require user @OWNER " diff --git a/cups/file.c b/cups/file.c index 6e7e4bfef4..e23d7fd47e 100644 --- a/cups/file.c +++ b/cups/file.c @@ -375,98 +375,21 @@ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ while (cupsFileGets(fp, buf, buflen)) { (*linenum) ++; - - // Read the comment... - if ((ptr = strchr(buf, '#')) != NULL) - { - bool inline_comment = false; - int index = (int) (ptr - buf); - for(int i=0;i for lines that begin with <... - */ - - ptr += strlen(ptr) - 1; - - if (buf[0] == '<' && *ptr == '>') - *ptr-- = '\0'; - else if (buf[0] == '<' && *ptr != '>') - { - /* - * Syntax error... - */ - - *value = NULL; - return (buf); - } - - while (ptr > *value && _cups_isspace(*ptr)) - *ptr-- = '\0'; - } - - /* - * Return the line... - */ - - return (buf); - } - - } - else - { - /* - * Strip leading whitespace... - */ - for (ptr = buf; _cups_isspace(*ptr); ptr ++); - - if (ptr > buf) - _cups_strcpy(buf, ptr); - return (buf); - } + /* + * Strip the comment from inline comment... + */ + if ((ptr = strchr(buf, '#')) != NULL) + { + int index = (int) (ptr - buf); + for(int i=index-1; i>=0; i--) + if (!_cups_isspace(buf[i])) + { + buf[index] = '\0'; + break; + } + } - } - else - { /* * Strip leading whitespace... */ @@ -476,62 +399,62 @@ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ if (ptr > buf) _cups_strcpy(buf, ptr); - /* - * See if there is anything left... - */ + /* + * See if there is anything left... + */ - if (buf[0]) - { - /* - * Yes, grab any value and return... - */ + if (buf[0] != '#') + { + /* + * Yes, grab any value and return... + */ - for (ptr = buf; *ptr; ptr ++) - if (_cups_isspace(*ptr)) - break; + for (ptr = buf; *ptr; ptr ++) + if (_cups_isspace(*ptr)) + break; - if (*ptr) - { - /* - * Have a value, skip any other spaces... - */ + if (*ptr) + { + /* + * Have a value, skip any other spaces... + */ - while (_cups_isspace(*ptr)) - *ptr++ = '\0'; + while (_cups_isspace(*ptr)) + *ptr++ = '\0'; - if (*ptr) - *value = ptr; + if (*ptr) + *value = ptr; - /* - * Strip trailing whitespace and > for lines that begin with <... - */ + /* + * Strip trailing whitespace and > for lines that begin with <... + */ - ptr += strlen(ptr) - 1; + ptr += strlen(ptr) - 1; - if (buf[0] == '<' && *ptr == '>') - *ptr-- = '\0'; - else if (buf[0] == '<' && *ptr != '>') - { - /* - * Syntax error... - */ + if (buf[0] == '<' && *ptr == '>') + *ptr-- = '\0'; + else if (buf[0] == '<' && *ptr != '>') + { + /* + * Syntax error... + */ - *value = NULL; - return (buf); - } + *value = NULL; + return (buf); + } - while (ptr > *value && _cups_isspace(*ptr)) - *ptr-- = '\0'; - } + while (ptr > *value && _cups_isspace(*ptr)) + *ptr-- = '\0'; + } - /* - * Return the line... - */ + /* + * Return the line... + */ - return (buf); - } - return (buf); - } + return (buf); + } + else + return (buf); } return (NULL); From d3518673a0418c041b5a132148f146c565cc7e77 Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Thu, 13 Apr 2023 21:41:12 +0530 Subject: [PATCH 08/16] Fixed: Coding style issues --- cups/adminutil.c | 6 ++---- cups/file.c | 17 ++++++++--------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/cups/adminutil.c b/cups/adminutil.c index b4e03fb44e..6be33aa4bd 100644 --- a/cups/adminutil.c +++ b/cups/adminutil.c @@ -792,8 +792,7 @@ cupsAdminSetServerSettings( wrote_policy = 1; if (!user_cancel_any) - cupsFilePuts(temp, - " \n" + cupsFilePuts(temp, " \n" " Order deny,allow\n" " Require user @OWNER " CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n" @@ -951,8 +950,7 @@ cupsAdminSetServerSettings( wrote_policy = 1; if (!user_cancel_any) - cupsFilePuts(temp, - " \n" + cupsFilePuts(temp, " \n" " Order deny,allow\n" " Require user @OWNER " CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n" diff --git a/cups/file.c b/cups/file.c index e23d7fd47e..8a6f443979 100644 --- a/cups/file.c +++ b/cups/file.c @@ -22,7 +22,6 @@ #include "debug-internal.h" #include #include -#include # ifdef HAVE_LIBZ # include @@ -382,12 +381,8 @@ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ if ((ptr = strchr(buf, '#')) != NULL) { int index = (int) (ptr - buf); - for(int i=index-1; i>=0; i--) - if (!_cups_isspace(buf[i])) - { - buf[index] = '\0'; - break; - } + while (index > 0 && _cups_isspace(buf[index])) + buf[index--] = '\0'; } /* @@ -403,10 +398,14 @@ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ * See if there is anything left... */ - if (buf[0] != '#') + if (buf[0]) { + // return comment if any + if (buf[0] == '#') + return ptr; + /* - * Yes, grab any value and return... + * Otherwise grab any value and return... */ for (ptr = buf; *ptr; ptr ++) From d92236ac4735c21a7d791e2928677e7fd58676da Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Thu, 13 Apr 2023 22:01:59 +0530 Subject: [PATCH 09/16] Fixed: Coding Style Issues --- cups/adminutil.c | 44 +------------------------------------------- 1 file changed, 1 insertion(+), 43 deletions(-) diff --git a/cups/adminutil.c b/cups/adminutil.c index 6be33aa4bd..230c947679 100644 --- a/cups/adminutil.c +++ b/cups/adminutil.c @@ -1026,12 +1026,10 @@ cupsAdminSetServerSettings( { if (share_printers > 0) { - cupsFilePuts(temp, "# Share local printers on the local network.\n"); cupsFilePuts(temp, "Browsing On\n"); } else { - cupsFilePuts(temp, "# Disable printer sharing and shared printers.\n"); cupsFilePuts(temp, "Browsing Off\n"); } } @@ -1040,12 +1038,10 @@ cupsAdminSetServerSettings( { if (debug_logging) { - cupsFilePuts(temp, "# Show troubleshooting information in error_log.\n"); cupsFilePuts(temp, "LogLevel debug\n"); } else { - cupsFilePuts(temp, "# Show general information in error_log.\n"); cupsFilePuts(temp, "LogLevel " CUPS_DEFAULT_LOG_LEVEL "\n"); } } @@ -1055,13 +1051,10 @@ cupsAdminSetServerSettings( { if (remote_admin > 0 || remote_any > 0 || share_printers > 0) { - cupsFilePuts(temp, "# Allow remote access\n"); cupsFilePrintf(temp, "Port %d\n", ippPort()); } else { - cupsFilePuts(temp, - "# Only listen for connections from the local machine.\n"); cupsFilePrintf(temp, "Listen localhost:%d\n", ippPort()); } @@ -1074,18 +1067,6 @@ cupsAdminSetServerSettings( if (!wrote_root_location && (remote_admin >= 0 || remote_any >= 0 || share_printers >= 0)) { - if (remote_admin > 0 && share_printers > 0) - cupsFilePuts(temp, - "# Allow shared printing and remote administration...\n"); - else if (remote_admin > 0) - cupsFilePuts(temp, "# Allow remote administration...\n"); - else if (share_printers > 0) - cupsFilePuts(temp, "# Allow shared printing...\n"); - else if (remote_any > 0) - cupsFilePuts(temp, "# Allow remote access...\n"); - else - cupsFilePuts(temp, "# Restrict access to the server...\n"); - cupsFilePuts(temp, "\n" " Order allow,deny\n"); @@ -1097,11 +1078,6 @@ cupsAdminSetServerSettings( if (!wrote_admin_location && remote_admin >= 0) { - if (remote_admin) - cupsFilePuts(temp, "# Allow remote administration...\n"); - else - cupsFilePuts(temp, "# Restrict access to the admin pages...\n"); - cupsFilePuts(temp, "\n" " Order allow,deny\n"); @@ -1113,12 +1089,6 @@ cupsAdminSetServerSettings( if (!wrote_conf_location && remote_admin >= 0) { - if (remote_admin) - cupsFilePuts(temp, - "# Allow remote access to the configuration files...\n"); - else - cupsFilePuts(temp, "# Restrict access to the configuration files...\n"); - cupsFilePuts(temp, "\n" " AuthType Default\n" " Require user @SYSTEM\n" @@ -1132,12 +1102,6 @@ cupsAdminSetServerSettings( if (!wrote_log_location && remote_admin >= 0) { - if (remote_admin) - cupsFilePuts(temp, - "# Allow remote access to the log files...\n"); - else - cupsFilePuts(temp, "# Restrict access to the log files...\n"); - cupsFilePuts(temp, "\n" " AuthType Default\n" " Require user @SYSTEM\n" @@ -1152,8 +1116,6 @@ cupsAdminSetServerSettings( if (!wrote_policy && user_cancel_any >= 0) { cupsFilePuts(temp, "\n" - " # Job-related operations must be done by the owner " - "or an administrator...\n" " \n" - " # All administration operations require an " - "administrator to authenticate...\n" " \n"); if (!user_cancel_any) - cupsFilePuts(temp, " # Only the owner or an administrator can cancel " - "a job...\n" - " \n" + cupsFilePuts(temp, " \n" " Order deny,allow\n" " Require user @OWNER " CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n" From f241386832d76953472a609de4c5e8469e6ea3b3 Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Sun, 16 Apr 2023 18:56:20 +0530 Subject: [PATCH 10/16] Simplified: _cupsFileGetConfAndComments() --- cups/file.c | 95 +++++++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/cups/file.c b/cups/file.c index 8a6f443979..78d14995a8 100644 --- a/cups/file.c +++ b/cups/file.c @@ -336,7 +336,7 @@ _cupsFileCheckFilter( /* - * 'cupsFileGetConfAndComments()' - Get line and comments from a configuration file. + * '_cupsFileGetConfAndComments()' - Get line and comments from a configuration file. */ char * /* O - Line read or @code NULL@ on end of file or error */ @@ -376,13 +376,21 @@ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ (*linenum) ++; /* - * Strip the comment from inline comment... + * Remove the inline comment... */ if ((ptr = strchr(buf, '#')) != NULL) { - int index = (int) (ptr - buf); - while (index > 0 && _cups_isspace(buf[index])) - buf[index--] = '\0'; + ptr--; + while(ptr >= buf) + { + // Null-terminate the string after the last non-whitespace + if(!_cups_isspace(*ptr)) + { + *(ptr + 1) = '\0'; + break; + } + ptr--; + } } /* @@ -394,66 +402,59 @@ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ if (ptr > buf) _cups_strcpy(buf, ptr); + /* + * Return the comment if any... + */ + if (buf[0] == '#') + return buf; + /* - * See if there is anything left... + * Otherwise grab any value and return... */ - if (buf[0]) - { - // return comment if any - if (buf[0] == '#') - return ptr; + for (ptr = buf; *ptr; ptr ++) + if (_cups_isspace(*ptr)) + break; + if (*ptr) + { /* - * Otherwise grab any value and return... + * Have a value, skip any other spaces... */ - for (ptr = buf; *ptr; ptr ++) - if (_cups_isspace(*ptr)) - break; + while (_cups_isspace(*ptr)) + *ptr++ = '\0'; if (*ptr) - { - /* - * Have a value, skip any other spaces... - */ + *value = ptr; - while (_cups_isspace(*ptr)) - *ptr++ = '\0'; + /* + * Strip trailing whitespace and > for lines that begin with <... + */ - if (*ptr) - *value = ptr; + ptr += strlen(ptr) - 1; + if (buf[0] == '<' && *ptr == '>') + *ptr-- = '\0'; + else if (buf[0] == '<' && *ptr != '>') + { /* - * Strip trailing whitespace and > for lines that begin with <... + * Syntax error... */ - ptr += strlen(ptr) - 1; - - if (buf[0] == '<' && *ptr == '>') - *ptr-- = '\0'; - else if (buf[0] == '<' && *ptr != '>') - { - /* - * Syntax error... - */ - - *value = NULL; - return (buf); - } - - while (ptr > *value && _cups_isspace(*ptr)) - *ptr-- = '\0'; + *value = NULL; + return (buf); } - /* - * Return the line... - */ - - return (buf); + while (ptr > *value && _cups_isspace(*ptr)) + *ptr-- = '\0'; } - else - return (buf); + + /* + * Return the line... + */ + + return (buf); } return (NULL); From 952fd3fb47c049866123bbe4930d4d3ad1627095 Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Sun, 16 Apr 2023 21:52:25 +0530 Subject: [PATCH 11/16] Fixed: Missing handling of escaped # --- cups/file.c | 90 ++++++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 43 deletions(-) diff --git a/cups/file.c b/cups/file.c index 78d14995a8..aa08202a9c 100644 --- a/cups/file.c +++ b/cups/file.c @@ -378,19 +378,18 @@ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ /* * Remove the inline comment... */ - if ((ptr = strchr(buf, '#')) != NULL) + if ((ptr = strchr(buf, '#')) != NULL && _cups_isspace(*(ptr-1))) { - ptr--; - while(ptr >= buf) - { + while(ptr > buf) + { // Null-terminate the string after the last non-whitespace - if(!_cups_isspace(*ptr)) + if(!_cups_isspace(*(ptr-1))) { - *(ptr + 1) = '\0'; + *ptr = '\0'; break; } ptr--; - } + } } /* @@ -405,55 +404,60 @@ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ /* * Return the comment if any... */ - if (buf[0] == '#') - return buf; + if(buf[0]) + { + if (buf[0] == '#') + return buf; + + /* + * Otherwise grab any value and return... + */ - /* - * Otherwise grab any value and return... - */ + for (ptr = buf; *ptr; ptr ++) + if (_cups_isspace(*ptr)) + break; - for (ptr = buf; *ptr; ptr ++) - if (_cups_isspace(*ptr)) - break; + if (*ptr) + { + /* + * Have a value, skip any other spaces... + */ - if (*ptr) - { - /* - * Have a value, skip any other spaces... - */ + while (_cups_isspace(*ptr)) + *ptr++ = '\0'; - while (_cups_isspace(*ptr)) - *ptr++ = '\0'; + if (*ptr) + *value = ptr; - if (*ptr) - *value = ptr; + /* + * Strip trailing whitespace and > for lines that begin with <... + */ - /* - * Strip trailing whitespace and > for lines that begin with <... - */ + ptr += strlen(ptr) - 1; - ptr += strlen(ptr) - 1; + if (buf[0] == '<' && *ptr == '>') + *ptr-- = '\0'; + else if (buf[0] == '<' && *ptr != '>') + { + /* + * Syntax error... + */ + + *value = NULL; + return (buf); + } + + while (ptr > *value && _cups_isspace(*ptr)) + *ptr-- = '\0'; + } - if (buf[0] == '<' && *ptr == '>') - *ptr-- = '\0'; - else if (buf[0] == '<' && *ptr != '>') - { /* - * Syntax error... + * Return the line... */ - *value = NULL; return (buf); - } - - while (ptr > *value && _cups_isspace(*ptr)) - *ptr-- = '\0'; - } - - /* - * Return the line... - */ + } return (buf); } From 7470d739b772cbb3d8a5cd2e9a035c2c29fce73c Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Mon, 17 Apr 2023 19:35:15 +0530 Subject: [PATCH 12/16] Added: escaped # support --- cups/file.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cups/file.c b/cups/file.c index aa08202a9c..0c6c872e76 100644 --- a/cups/file.c +++ b/cups/file.c @@ -378,12 +378,12 @@ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ /* * Remove the inline comment... */ - if ((ptr = strchr(buf, '#')) != NULL && _cups_isspace(*(ptr-1))) + if ((ptr = strchr(buf, '#')) != NULL) { while(ptr > buf) { - // Null-terminate the string after the last non-whitespace - if(!_cups_isspace(*(ptr-1))) + // Null-terminate the string after the last non-whitespace, unless the '#' character is escaped by a backslash ('\') + if(!_cups_isspace(ptr[-1]) && (ptr == buf || ptr[-1] != '\\')) { *ptr = '\0'; break; @@ -458,7 +458,6 @@ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ return (buf); } - return (buf); } return (NULL); From d9c19862b51b61b6ffb9f4b30521dc5341463478 Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Wed, 19 Apr 2023 18:53:27 +0530 Subject: [PATCH 13/16] Fixed: Coding Style Issues --- cups/adminutil.c | 7 ++- cups/file.c | 160 ++++++++++++++++++++++++++++------------------- 2 files changed, 101 insertions(+), 66 deletions(-) diff --git a/cups/adminutil.c b/cups/adminutil.c index 230c947679..71cfe388cd 100644 --- a/cups/adminutil.c +++ b/cups/adminutil.c @@ -700,7 +700,12 @@ cupsAdminSetServerSettings( while (_cupsFileGetConfAndComments(cupsd, line, sizeof(line), &value, &linenum)) { - if ((!_cups_strcasecmp(line, "Port") || !_cups_strcasecmp(line, "Listen")) && + /* + * Preserve empty lines... + */ + if (!line[0]) + cupsFilePuts(temp, "\n"); + else if ((!_cups_strcasecmp(line, "Port") || !_cups_strcasecmp(line, "Listen")) && (remote_admin >= 0 || remote_any >= 0 || share_printers >= 0)) { if (!wrote_port_listen) diff --git a/cups/file.c b/cups/file.c index 0c6c872e76..01df83caf3 100644 --- a/cups/file.c +++ b/cups/file.c @@ -341,10 +341,10 @@ _cupsFileCheckFilter( char * /* O - Line read or @code NULL@ on end of file or error */ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ - char *buf, /* O - String buffer */ - size_t buflen, /* I - Size of string buffer */ - char **value, /* O - Pointer to value */ - int *linenum) /* IO - Current line number */ + char *buf, /* O - String buffer */ + size_t buflen, /* I - Size of string buffer */ + char **value, /* O - Pointer to value */ + int *linenum) /* IO - Current line number */ { char *ptr; /* Pointer into line */ @@ -378,86 +378,116 @@ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ /* * Remove the inline comment... */ - if ((ptr = strchr(buf, '#')) != NULL) - { - while(ptr > buf) + if ((ptr = strchr(buf, '#')) != NULL) + { + /* + * Check if the '#' character is escaped by a backslash... + */ + if (ptr != buf && ptr[-1] == '\\') + { + /* + * Remove the backslash and continue searching for unescaped '#'... + */ + _cups_strcpy(ptr-1, ptr); + ptr = strchr(ptr+1, '#'); + } + + /* + * Find the last non-whitespace character before the unescaped '#' character... + */ + while (ptr > buf) + { + if (!_cups_isspace(ptr[-1])) { - // Null-terminate the string after the last non-whitespace, unless the '#' character is escaped by a backslash ('\') - if(!_cups_isspace(ptr[-1]) && (ptr == buf || ptr[-1] != '\\')) - { - *ptr = '\0'; - break; - } - ptr--; + /* + * Null-terminate the string after the last non-whitespace character... + */ + *ptr = '\0'; + break; } - } - /* - * Strip leading whitespace... - */ + /* + * Check if the '#' character is escaped by a backslash... + */ + if (ptr != buf && *(ptr-1) == '\\') + { + /* + * Remove the backslash and continue searching for unescaped '#' + */ + _cups_strcpy(ptr-1, ptr); + ptr = strchr(ptr+1, '#'); + } + else + { + ptr--; + } + } + } - for (ptr = buf; _cups_isspace(*ptr); ptr ++); + /* + * Strip leading whitespace... + */ - if (ptr > buf) - _cups_strcpy(buf, ptr); + for (ptr = buf; _cups_isspace(*ptr); ptr ++); - /* - * Return the comment if any... - */ - if(buf[0]) - { - if (buf[0] == '#') - return buf; + if (ptr > buf) + _cups_strcpy(buf, ptr); - /* - * Otherwise grab any value and return... - */ + /* + * Return the comment if any... + */ - for (ptr = buf; *ptr; ptr ++) - if (_cups_isspace(*ptr)) - break; + if (!buf[0] || buf[0] == '#') + return buf; - if (*ptr) - { - /* - * Have a value, skip any other spaces... - */ + /* + * Otherwise grab any value and return... + */ + + for (ptr = buf; *ptr; ptr ++) + if (_cups_isspace(*ptr)) + break; - while (_cups_isspace(*ptr)) - *ptr++ = '\0'; + if (*ptr) + { + /* + * Have a value, skip any other spaces... + */ - if (*ptr) - *value = ptr; + while (_cups_isspace(*ptr)) + *ptr++ = '\0'; - /* - * Strip trailing whitespace and > for lines that begin with <... - */ + if (*ptr) + *value = ptr; - ptr += strlen(ptr) - 1; + /* + * Strip trailing whitespace and > for lines that begin with <... + */ - if (buf[0] == '<' && *ptr == '>') - *ptr-- = '\0'; - else if (buf[0] == '<' && *ptr != '>') - { - /* - * Syntax error... - */ + ptr += strlen(ptr) - 1; - *value = NULL; - return (buf); - } + if (buf[0] == '<' && *ptr == '>') + *ptr-- = '\0'; + else if (buf[0] == '<' && *ptr != '>') + { + /* + * Syntax error... + */ - while (ptr > *value && _cups_isspace(*ptr)) - *ptr-- = '\0'; - } + *value = NULL; + return (buf); + } - /* - * Return the line... - */ + while (ptr > *value && _cups_isspace(*ptr)) + *ptr-- = '\0'; + } + + /* + * Return the line... + */ - return (buf); + return (buf); - } } return (NULL); From 04b6730215feaafc3cd9974df104f2f486f08271 Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Thu, 20 Apr 2023 00:11:23 +0530 Subject: [PATCH 14/16] Fixed coding style issues --- cups/file.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cups/file.c b/cups/file.c index 01df83caf3..ecc3fba24d 100644 --- a/cups/file.c +++ b/cups/file.c @@ -341,10 +341,10 @@ _cupsFileCheckFilter( char * /* O - Line read or @code NULL@ on end of file or error */ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ - char *buf, /* O - String buffer */ - size_t buflen, /* I - Size of string buffer */ - char **value, /* O - Pointer to value */ - int *linenum) /* IO - Current line number */ + char *buf, /* O - String buffer */ + size_t buflen, /* I - Size of string buffer */ + char **value, /* O - Pointer to value */ + int *linenum) /* IO - Current line number */ { char *ptr; /* Pointer into line */ From 07e945068956ded765439f23ea7517c40a32cdcf Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Sat, 3 Jun 2023 01:51:52 +0530 Subject: [PATCH 15/16] Added : Private Api for Striping Inline Comment --- cups/file.c | 111 +++++++++++++++++++++++++++------------------------- cups/file.h | 1 + 2 files changed, 58 insertions(+), 54 deletions(-) diff --git a/cups/file.c b/cups/file.c index ecc3fba24d..d925deb52d 100644 --- a/cups/file.c +++ b/cups/file.c @@ -335,6 +335,53 @@ _cupsFileCheckFilter( #endif /* !_WIN32 */ +/* + * '_cupsFileStripComment' - Strip inline comments from a line of text.. + */ + +char *_cupsFileStripComment(char *buf) +{ + /* + * Find the first '#' character + */ + char *ptr = strchr(buf, '#'); + + if (!ptr) + return buf; + + if (ptr != buf && ptr[-1] == '\\') + { + /* + * Check if the '#' character is escaped by a backslash + */ + _cups_strcpy(ptr - 1, ptr); + return _cupsFileStripComment(buf); + } + + while (ptr > buf) + { + if (!_cups_isspace(ptr[-1])) + { + *ptr = '\0'; + break; + } + + if (ptr != buf && *(ptr - 1) == '\\') + { + /* + * Check if the '#' character is escaped by a backslash + */ + _cups_strcpy(ptr - 1, ptr); + return _cupsFileStripComment(buf); + } + else + ptr--; + } + + return buf; +} + + /* * '_cupsFileGetConfAndComments()' - Get line and comments from a configuration file. */ @@ -349,9 +396,9 @@ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ char *ptr; /* Pointer into line */ - /* - * Range check input... - */ + /* + * Range check input... + */ DEBUG_printf(("2cupsFileGetConfAndComments(fp=%p, buf=%p, buflen=" CUPS_LLFMT ", value=%p, linenum=%p)", (void *)fp, (void *)buf, CUPS_LLCAST buflen, (void *)value, (void *)linenum)); @@ -365,9 +412,9 @@ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ return (NULL); } - /* - * Read the next line... - */ + /* + * Read the next line... + */ *value = NULL; @@ -378,55 +425,11 @@ _cupsFileGetConfAndComments(cups_file_t *fp, /* I - CUPS file */ /* * Remove the inline comment... */ - if ((ptr = strchr(buf, '#')) != NULL) - { - /* - * Check if the '#' character is escaped by a backslash... - */ - if (ptr != buf && ptr[-1] == '\\') - { - /* - * Remove the backslash and continue searching for unescaped '#'... - */ - _cups_strcpy(ptr-1, ptr); - ptr = strchr(ptr+1, '#'); - } + _cupsFileStripComment(buf); - /* - * Find the last non-whitespace character before the unescaped '#' character... - */ - while (ptr > buf) - { - if (!_cups_isspace(ptr[-1])) - { - /* - * Null-terminate the string after the last non-whitespace character... - */ - *ptr = '\0'; - break; - } - - /* - * Check if the '#' character is escaped by a backslash... - */ - if (ptr != buf && *(ptr-1) == '\\') - { - /* - * Remove the backslash and continue searching for unescaped '#' - */ - _cups_strcpy(ptr-1, ptr); - ptr = strchr(ptr+1, '#'); - } - else - { - ptr--; - } - } - } - - /* - * Strip leading whitespace... - */ + /* + * Strip leading whitespace... + */ for (ptr = buf; _cups_isspace(*ptr); ptr ++); diff --git a/cups/file.h b/cups/file.h index 739ee1a4ea..644ac22f80 100644 --- a/cups/file.h +++ b/cups/file.h @@ -59,6 +59,7 @@ typedef struct _cups_file_s cups_file_t;/**** CUPS file type ****/ * Prototypes... */ +extern char *_cupsFileStripComment(char *buf) _CUPS_PRIVATE; extern char *_cupsFileGetConfAndComments(cups_file_t *fp, char *buf, size_t buflen, char **value, int *linenum) _CUPS_PRIVATE; extern int cupsFileClose(cups_file_t *fp) _CUPS_API_1_2; extern int cupsFileCompression(cups_file_t *fp) _CUPS_API_1_2; From 5150a4782f7fbe1c6375e106391390f95c663478 Mon Sep 17 00:00:00 2001 From: Ankit3002 Date: Sat, 3 Jun 2023 11:22:09 +0530 Subject: [PATCH 16/16] Added : Debug Statement for cupsFileStripComment --- cups/file.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cups/file.c b/cups/file.c index d925deb52d..f6fcda5b91 100644 --- a/cups/file.c +++ b/cups/file.c @@ -341,6 +341,11 @@ _cupsFileCheckFilter( char *_cupsFileStripComment(char *buf) { + /* + * Range check input... + */ + DEBUG_printf(("2cupsFileStripComment(buf=%p)", (void *)buf)); + /* * Find the first '#' character */