From c0a2db87396fac59c5d85ece655f145415ebb93e Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sun, 10 Nov 2024 18:44:05 +0100 Subject: [PATCH] lib/shadow/, lib/: sgetsgent(): Move to separate file Signed-off-by: Alejandro Colomar --- lib/Makefile.am | 2 + lib/gshadow.c | 51 ----------------------- lib/gshadow_.h | 1 - lib/sgroupio.c | 1 + lib/shadow/gshadow/fgetsgent.c | 3 +- lib/shadow/gshadow/sgetsgent.c | 75 ++++++++++++++++++++++++++++++++++ lib/shadow/gshadow/sgetsgent.h | 24 +++++++++++ 7 files changed, 104 insertions(+), 53 deletions(-) create mode 100644 lib/shadow/gshadow/sgetsgent.c create mode 100644 lib/shadow/gshadow/sgetsgent.h diff --git a/lib/Makefile.am b/lib/Makefile.am index c6a2ce02f..dee5e095a 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -184,6 +184,8 @@ libshadow_la_SOURCES = \ shadow/gshadow/putsgent.h \ shadow/gshadow/setsgent.c \ shadow/gshadow/setsgent.h \ + shadow/gshadow/sgetsgent.c \ + shadow/gshadow/sgetsgent.h \ shadow/gshadow/sgrp.c \ shadow/gshadow/sgrp.h \ shadowio.c \ diff --git a/lib/gshadow.c b/lib/gshadow.c index 8f638f7f3..c6e1ff3ac 100644 --- a/lib/gshadow.c +++ b/lib/gshadow.c @@ -18,8 +18,6 @@ #include #include -#include "alloc/malloc.h" -#include "alloc/realloc.h" #include "defines.h" #include "prototypes.h" #include "shadow/gshadow/fgetsgent.h" @@ -27,57 +25,8 @@ #include "shadow/gshadow/setsgent.h" #include "shadow/gshadow/sgrp.h" #include "string/strcmp/streq.h" -#include "string/strtok/stpsep.h" -#include "string/strtok/strsep2arr.h" -#include "string/strtok/xastrsep2ls.h" -static struct sgrp sgroup = {}; - - -static /*@null@*/char ** -build_list(char *s) -{ - char **l; - size_t n; - - l = xastrsep2ls(s, ",", &n); - - if (streq(l[n-1], "")) - l[n-1] = NULL; - - return l; -} - -/*@observer@*//*@null@*/struct sgrp * -sgetsgent(const char *s) -{ - static char *dup = NULL; - - char *fields[4]; - - free(dup); - dup = strdup(s); - if (dup == NULL) - return NULL; - - stpsep(dup, "\n"); - - if (STRSEP2ARR(dup, ":", fields) == -1) - return NULL; - - sgroup.sg_namp = fields[0]; - sgroup.sg_passwd = fields[1]; - - free(sgroup.sg_adm); - free(sgroup.sg_mem); - - sgroup.sg_adm = build_list(fields[2]); - sgroup.sg_mem = build_list(fields[3]); - - return &sgroup; -} - /* * getsgent - get a single shadow group entry */ diff --git a/lib/gshadow_.h b/lib/gshadow_.h index 007652b08..ac5813610 100644 --- a/lib/gshadow_.h +++ b/lib/gshadow_.h @@ -21,7 +21,6 @@ /*@observer@*//*@null@*/struct sgrp *getsgent (void); /*@observer@*//*@null@*/struct sgrp *getsgnam (const char *); -/*@observer@*//*@null@*/struct sgrp *sgetsgent (const char *); #define GSHADOW "/etc/gshadow" diff --git a/lib/sgroupio.c b/lib/sgroupio.c index f191d7d06..5c1096579 100644 --- a/lib/sgroupio.c +++ b/lib/sgroupio.c @@ -24,6 +24,7 @@ #include "sgroupio.h" #include "shadow/gshadow/gshadow.h" #include "shadow/gshadow/putsgent.h" +#include "shadow/gshadow/sgetsgent.h" #include "shadow/gshadow/sgrp.h" #include "string/memset/memzero.h" diff --git a/lib/shadow/gshadow/fgetsgent.c b/lib/shadow/gshadow/fgetsgent.c index 9424ab940..57e45fb5e 100644 --- a/lib/shadow/gshadow/fgetsgent.c +++ b/lib/shadow/gshadow/fgetsgent.c @@ -18,6 +18,7 @@ #include "alloc/realloc.h" #include "defines.h" #include "prototypes.h" +#include "shadow/gshadow/sgetsgent.h" #include "shadow/gshadow/sgrp.h" #include "string/strtok/stpsep.h" @@ -72,6 +73,6 @@ fgetsgent(FILE *fp) } } stpsep(buf, "\n"); - return (sgetsgent (buf)); + return sgetsgent(buf); } #endif diff --git a/lib/shadow/gshadow/sgetsgent.c b/lib/shadow/gshadow/sgetsgent.c new file mode 100644 index 000000000..c15bb5831 --- /dev/null +++ b/lib/shadow/gshadow/sgetsgent.c @@ -0,0 +1,75 @@ +// SPDX-FileCopyrightText: 1990-1994, Julianne Frances Haugh +// SPDX-FileCopyrightText: 1996-1998, Marek Michałkiewicz +// SPDX-FileCopyrightText: 2005, Tomasz Kłoczko +// SPDX-FileCopyrightText: 2008-2009, Nicolas François +// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include "config.h" + +#include "shadow/gshadow/sgetsgent.h" + +#include +#include +#include + +#include "shadow/gshadow/sgrp.h" +#include "string/strcmp/streq.h" +#include "string/strtok/stpsep.h" +#include "string/strtok/strsep2arr.h" +#include "string/strtok/xastrsep2ls.h" + + +#if defined(SHADOWGRP) && !__has_include() +static struct sgrp sgroup = {}; + + +static char **build_list(char *s); + + +// from-string get shadow group entry +struct sgrp * +sgetsgent(const char *s) +{ + static char *dup = NULL; + + char *fields[4]; + + free(dup); + dup = strdup(s); + if (dup == NULL) + return NULL; + + stpsep(dup, "\n"); + + if (STRSEP2ARR(dup, ":", fields) == -1) + return NULL; + + sgroup.sg_namp = fields[0]; + sgroup.sg_passwd = fields[1]; + + free(sgroup.sg_adm); + free(sgroup.sg_mem); + + sgroup.sg_adm = build_list(fields[2]); + sgroup.sg_mem = build_list(fields[3]); + + return &sgroup; +} + + +static char ** +build_list(char *s) +{ + char **l; + size_t n; + + l = xastrsep2ls(s, ",", &n); + + if (streq(l[n-1], "")) + l[n-1] = NULL; + + return l; +} +#endif diff --git a/lib/shadow/gshadow/sgetsgent.h b/lib/shadow/gshadow/sgetsgent.h new file mode 100644 index 000000000..65fda800d --- /dev/null +++ b/lib/shadow/gshadow/sgetsgent.h @@ -0,0 +1,24 @@ +// SPDX-FileCopyrightText: 1988-1994, Julianne Frances Haugh +// SPDX-FileCopyrightText: 1996-1997, Marek Michałkiewicz +// SPDX-FileCopyrightText: 2003-2005, Tomasz Kłoczko +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_SHADOW_GSHADOW_SGETSGENT_H_ +#define SHADOW_INCLUDE_LIB_SHADOW_GSHADOW_SGETSGENT_H_ + + +#include "config.h" + +#include "shadow/gshadow/sgrp.h" + + +#if __has_include() +# include +#else +struct sgrp *sgetsgent(const char *); +#endif + + +#endif // include guard -- 2.47.3