aboutsummaryrefslogtreecommitdiff
path: root/libpam
diff options
context:
space:
mode:
authorTobias Stoeckmann <tobias@stoeckmann.org>2024-01-31 20:12:58 +0100
committerDmitry V. Levin <ldv@strace.io>2024-02-06 08:00:00 +0000
commit644dc6f0c15bccc7401e47785753e9c8d01018d3 (patch)
tree32afca676f02af3385fbd5feec7b43899358d3e7 /libpam
parent9fa68ae79c213026d9621d6a70defb381e5a0533 (diff)
downloadpam-644dc6f0c15bccc7401e47785753e9c8d01018d3.tar.gz
pam-644dc6f0c15bccc7401e47785753e9c8d01018d3.tar.bz2
pam-644dc6f0c15bccc7401e47785753e9c8d01018d3.zip
libpam_internal: introduce pam_line
The pam_assemble_line function is renamed to pam_line_assemble and moved into libpam_internal so it can be shared across libpam and the pam_env module. Applied renaming to all other relevant functions and data structures so it is easier to locate them in files. Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Diffstat (limited to 'libpam')
-rw-r--r--libpam/Makefile.am4
-rw-r--r--libpam/include/pam_assemble_line.h267
-rw-r--r--libpam/pam_handlers.c8
3 files changed, 6 insertions, 273 deletions
diff --git a/libpam/Makefile.am b/libpam/Makefile.am
index e3f4a3bc..505900b5 100644
--- a/libpam/Makefile.am
+++ b/libpam/Makefile.am
@@ -3,7 +3,7 @@
#
AM_CFLAGS = -DDEFAULT_MODULE_PATH=\"$(SECUREDIR)/\" -DLIBPAM_COMPILE \
- -DLOCALEDIR=\"$(localedir)\" \
+ -DLOCALEDIR=\"$(localedir)\" -I$(top_srcdir)/libpam_internal/include \
-I$(srcdir)/include $(LIBPRELUDE_CFLAGS) $(ECONF_CFLAGS) \
-DPAM_VERSION=\"$(VERSION)\" -DSYSCONFDIR=\"$(sysconfdir)\" \
$(WARN_CFLAGS)
@@ -18,7 +18,7 @@ include_HEADERS = include/security/_pam_compat.h \
include/security/pam_ext.h include/security/pam_modutil.h
noinst_HEADERS = pam_prelude.h pam_private.h pam_tokens.h \
- pam_modutil_private.h include/pam_assemble_line.h \
+ pam_modutil_private.h \
include/pam_cc_compat.h include/pam_inline.h \
include/test_assert.h
diff --git a/libpam/include/pam_assemble_line.h b/libpam/include/pam_assemble_line.h
deleted file mode 100644
index 651776e9..00000000
--- a/libpam/include/pam_assemble_line.h
+++ /dev/null
@@ -1,267 +0,0 @@
-/* pam_assemble_line.h -- routine to parse configuration lines */
-
-#ifndef PAM_ASSEMBLE_LINE_H
-#define PAM_ASSEMBLE_LINE_H
-
-#include "pam_inline.h"
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
-struct line_buffer {
- char *assembled;
- char *chunk;
- size_t chunk_size;
- size_t len;
- size_t size;
-};
-
-static int _pam_buffer_add(struct line_buffer *buffer, char *start, char *end)
-{
- size_t len = end - start;
-
- D(("assembled: [%zu/%zu] '%s', adding [%zu] '%s'",
- buffer->len, buffer->size,
- buffer->assembled == NULL ? "" : buffer->assembled, len, start));
-
- if (start == end)
- return 0;
-
- if (buffer->assembled == NULL && buffer->chunk == start) {
- /* no extra allocation needed, just move chunk to assembled */
- buffer->assembled = buffer->chunk;
- buffer->len = len;
- buffer->size = buffer->chunk_size;
-
- buffer->chunk = NULL;
- buffer->chunk_size = 0;
-
- D(("exiting with quick exchange"));
- return 0;
- }
-
- if (buffer->len + len + 1 > buffer->size) {
- size_t size;
- char *p;
-
- size = buffer->len + len + 1;
- if ((p = realloc(buffer->assembled, size)) == NULL)
- return -1;
-
- buffer->assembled = p;
- buffer->size = size;
- }
-
- memcpy(buffer->assembled + buffer->len, start, len);
- buffer->len += len;
- buffer->assembled[buffer->len] = '\0';
-
- D(("exiting"));
- return 0;
-}
-
-static inline int _pam_buffer_add_eol(struct line_buffer *buffer,
- char *start, char *end)
-{
- if (buffer->assembled != NULL || (*start != '\0' && *start != '\n'))
- return _pam_buffer_add(buffer, start, end);
- return 0;
-}
-
-static void _pam_buffer_clear(struct line_buffer *buffer)
-{
- pam_overwrite_n(buffer->assembled, buffer->size);
- _pam_drop(buffer->assembled);
- pam_overwrite_n(buffer->chunk, buffer->chunk_size);
- _pam_drop(buffer->chunk);
- buffer->chunk_size = 0;
- buffer->len = 0;
- buffer->size = 0;
-}
-
-static void _pam_buffer_init(struct line_buffer *buffer)
-{
- buffer->assembled = NULL;
- buffer->chunk = NULL;
- _pam_buffer_clear(buffer);
-}
-
-static void _pam_buffer_purge(struct line_buffer *buffer)
-{
- pam_overwrite_n(buffer->chunk, buffer->chunk_size);
- _pam_drop(buffer->chunk);
- buffer->chunk_size = 0;
-}
-
-static void _pam_buffer_shift(struct line_buffer *buffer)
-{
- if (buffer->assembled == NULL)
- return;
-
- _pam_buffer_purge(buffer);
- buffer->chunk = buffer->assembled;
- buffer->chunk_size = buffer->size;
-
- buffer->assembled = NULL;
- buffer->size = 0;
- buffer->len = 0;
-}
-
-static inline int _pam_buffer_valid(struct line_buffer *buffer)
-{
- return buffer->assembled != NULL && *buffer->assembled != '\0';
-}
-
-/*
- * Trim string to relevant parts of a configuration line.
- *
- * Preceding whitespaces are skipped and comment (#) marks the end of
- * configuration line.
- *
- * Returns start of configuration line.
- */
-static inline char *_pam_str_trim(char *str)
-{
- /* skip leading spaces */
- str += strspn(str, " \t");
- /*
- * we are only interested in characters before the first '#'
- * character
- */
- str[strcspn(str, "#")] = '\0';
-
- return str;
-}
-
-/*
- * Remove escaped newline from end of string.
- *
- * Configuration lines may span across multiple lines in a file
- * by ending a line with a backslash (\).
- *
- * If an escaped newline is encountered, the backslash will be
- * replaced with "repl" and the newline itself removed.
- * Then the variable "end" will point to the new end of line.
- *
- * Returns 0 if escaped newline was found and replaced, 1 otherwise.
- */
-static inline int _pam_str_unescnl(char *start, char **end, char repl)
-{
- int ret = 1;
- char *p = *end;
-
- /*
- * Check for backslash by scanning back from the end of
- * the entered line, the '\n' should be included since
- * normally a line is terminated with this character.
- */
- while (p > start && ((*--p == ' ') || (*p == '\t') || (*p == '\n')))
- ;
- if (*p == '\\') {
- *p = repl; /* replace backslash with replacement char */
- if (repl != '\0') {
- *++p = '\0'; /* truncate the line here if repl is not NUL */
- }
- *end = p;
- ret = 0;
- }
-
- return ret;
-}
-
-/*
- * Prepare line from file for configuration line parsing.
- *
- * A configuration line may span across multiple lines in a file.
- * Remove comments and skip preceding whitespaces.
- *
- * Returns 0 if line spans across multiple lines, 1 if
- * end of line is encountered.
- */
-static inline int _pam_str_prepare(char *line, ssize_t len,
- char **start, char **end, char repl)
-{
- int ret;
-
- *start = line;
- *end = line + len;
-
- ret = _pam_str_unescnl(*start, end, repl) || strchr(*start, '#') != NULL;
-
- *start = _pam_str_trim(*start);
-
- return ret;
-}
-
-/*
- * This is where we read a line of the PAM config file. The line may be
- * preceded by lines of comments and also extended with "\\\n"
- *
- * The "repl" argument is used as replacement char for the backslash used
- * in newline escaping, i.e. in "\\\n".
- *
- * Returns 0 on EOF, 1 on successful line parsing, or -1 on error.
- */
-
-static int _pam_assemble_line(FILE *f, struct line_buffer *buffer, char repl)
-{
- int ret = 0;
-
- /* loop broken with a 'break' when a non-'\\n' ended line is read */
-
- D(("called."));
-
- _pam_buffer_shift(buffer);
-
- for (;;) {
- char *start, *end;
- ssize_t n;
- int eol;
-
- if ((n = getline(&buffer->chunk, &buffer->chunk_size, f)) == -1) {
- if (ret) {
- /* Incomplete read */
- ret = -1;
- } else {
- /* EOF */
- ret = 0;
- }
- break;
- }
-
- eol = _pam_str_prepare(buffer->chunk, n, &start, &end, repl);
-
- if (eol) {
- if (_pam_buffer_add_eol(buffer, start, end)) {
- ret = -1;
- break;
- }
- if (_pam_buffer_valid(buffer)) {
- /* Successfully parsed a line */
- ret = 1;
- break;
- }
- /* Start parsing next line */
- _pam_buffer_shift(buffer);
- ret = 0;
- } else {
- /* Configuration line spans across multiple lines in file */
- if (_pam_buffer_add(buffer, start, end)) {
- ret = -1;
- break;
- }
- /* Keep parsing line */
- ret = 1;
- }
- }
-
- if (ret == 1)
- _pam_buffer_purge(buffer);
- else
- _pam_buffer_clear(buffer);
-
- return ret;
-}
-
-#endif /* PAM_ASSEMBLE_LINE_H */
diff --git a/libpam/pam_handlers.c b/libpam/pam_handlers.c
index 55d5b2fa..7fd6ce84 100644
--- a/libpam/pam_handlers.c
+++ b/libpam/pam_handlers.c
@@ -18,7 +18,7 @@
#include <fcntl.h>
#include <unistd.h>
-#include "pam_assemble_line.h"
+#include "pam_line.h"
#define MODULE_CHUNK 4
#define UNKNOWN_MODULE "<*unknown module*>"
@@ -61,14 +61,14 @@ static int _pam_parse_conf_file(pam_handle_t *pamh, FILE *f
#endif /* PAM_READ_BOTH_CONFS */
)
{
- struct line_buffer buffer;
+ struct pam_line_buffer buffer;
int x; /* read a line from the FILE *f ? */
- _pam_buffer_init(&buffer);
+ _pam_line_buffer_init(&buffer);
/*
* read a line from the configuration (FILE *) f
*/
- while ((x = _pam_assemble_line(f, &buffer, ' ')) > 0) {
+ while ((x = _pam_line_assemble(f, &buffer, ' ')) > 0) {
char *buf = buffer.assembled;
char *tok, *nexttok=NULL;
const char *this_service;