Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ libshadow_la_SOURCES = \
spawn.c \
sssd.c \
sssd.h \
io/fgets/fgets.c \
io/fgets/fgets.h \
string/ctype/strchrisascii/strchriscntrl.c \
string/ctype/strchrisascii/strchriscntrl.h \
string/ctype/strisascii/strisdigit.c \
Expand Down
3 changes: 2 additions & 1 deletion lib/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "defines.h"
#include "getdef.h"
#include "io/fgets/fgets.h"
#include "prototypes.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
Expand Down Expand Up @@ -76,7 +77,7 @@ is_listed(const char *cfgin, const char *tty, bool def)
* See if this tty is listed in the console file.
*/

while (fgets(buf, sizeof(buf), fp) != NULL) {
while (fgets_a(buf, fp) != NULL) {
stpsep(buf, "\n");
if (streq(buf, tty)) {
(void) fclose (fp);
Expand Down
10 changes: 4 additions & 6 deletions lib/fields.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/param.h>

#include "io/fgets/fgets.h"
#include "prototypes.h"
#include "string/ctype/strisascii/strisprint.h"
#include "string/ctype/strchrisascii/strchriscntrl.h"
Expand Down Expand Up @@ -60,16 +62,12 @@ valid_field_(const char *field, const char *illegal)
void
change_field(char *buf, size_t maxsize, const char *prompt)
{
char newf[200];
char *cp;

if (maxsize > sizeof(newf)) {
maxsize = sizeof(newf);
}
char newf[MIN(200, maxsize)];

printf ("\t%s [%s]: ", prompt, buf);
(void) fflush (stdout);
if (fgets(newf, maxsize, stdin) == NULL)
if (fgets_a(newf, stdin) == NULL)
return;

if (stpsep(newf, "\n") == NULL)
Expand Down
3 changes: 2 additions & 1 deletion lib/getdef.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "atoi/a2i.h"
#include "defines.h"
#include "getdef.h"
#include "io/fgets/fgets.h"
#include "prototypes.h"
#include "shadowlog_internal.h"
#include "sizeof.h"
Expand Down Expand Up @@ -553,7 +554,7 @@ static void def_load (void)
/*
* Go through all of the lines in the file.
*/
while (fgets(buf, sizeof(buf), fp) != NULL) {
while (fgets_a(buf, fp) != NULL) {

/*
* Trim trailing whitespace.
Expand Down
3 changes: 2 additions & 1 deletion lib/hushed.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "defines.h"
#include "getdef.h"
#include "io/fgets/fgets.h"
#include "prototypes.h"
#include "string/sprintf/snprintf.h"
#include "string/strcmp/streq.h"
Expand Down Expand Up @@ -74,7 +75,7 @@ bool hushed (const char *username)
if (NULL == fp) {
return false;
}
for (found = false; !found && (fgets(buf, sizeof(buf), fp) != NULL);) {
for (found = false; !found && (fgets_a(buf, fp) != NULL);) {
stpsep(buf, "\n");
found = streq(buf, pw->pw_shell) ||
streq(buf, pw->pw_name);
Expand Down
7 changes: 7 additions & 0 deletions lib/io/fgets/fgets.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#include "config.h"

#include "io/fgets/fgets.h"
20 changes: 20 additions & 0 deletions lib/io/fgets/fgets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_IO_FGETS_FGETS_H_
#define SHADOW_INCLUDE_LIB_IO_FGETS_FGETS_H_


#include "config.h"

#include <stdio.h>

#include "sizeof.h"


// fgets_a - FILE get string array-safe
#define fgets_a(buf, stream) fgets(buf, countof(buf), stream)


#endif // include guard
3 changes: 2 additions & 1 deletion lib/limits.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <sys/resource.h>

#include "atoi/a2i.h"
#include "io/fgets/fgets.h"
#include "string/memset/memzero.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
Expand Down Expand Up @@ -366,7 +367,7 @@ static int setup_user_limits (const char *uname)
*
* FIXME: a better (smarter) checking should be done
*/
while (fgets (buf, 1024, fil) != NULL) {
while (fgets_a(buf, fil) != NULL) {
if (strprefix(buf, "#") || strprefix(buf, "\n")) {
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/loginprompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "attr.h"
#include "defines.h"
#include "getdef.h"
#include "io/fgets/fgets.h"
#include "prototypes.h"
#include "string/memset/memzero.h"
#include "string/strcpy/strtcpy.h"
Expand Down Expand Up @@ -85,7 +86,7 @@ login_prompt(char *name, int namesize)
*/

memzero_a(buf);
if (fgets(buf, sizeof(buf), stdin) == NULL)
if (fgets_a(buf, stdin) == NULL)
exit (EXIT_FAILURE);

if (stpsep(buf, "\n") == NULL)
Expand Down
3 changes: 2 additions & 1 deletion lib/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <string.h>

#include "defines.h"
#include "io/fgets/fgets.h"
#include "port.h"
#include "prototypes.h"
#include "string/strcmp/streq.h"
Expand Down Expand Up @@ -139,7 +140,7 @@ getportent(void)
*/

next:
if (fgets(buf, sizeof(buf), ports) == NULL) {
if (fgets_a(buf, ports) == NULL) {
errno = saveerr;
return NULL;
}
Expand Down
10 changes: 6 additions & 4 deletions lib/setupenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@

#ident "$Id$"

#include <assert.h>
#include <ctype.h>
#include <pwd.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <ctype.h>

#include "prototypes.h"
#include "defines.h"
#include <pwd.h>
#include "getdef.h"
#include "io/fgets/fgets.h"
#include "shadowlog.h"
#include "string/sprintf/aprintf.h"
#include "string/strcmp/streq.h"
Expand All @@ -34,6 +34,8 @@
#include "string/strspn/stpspn.h"
#include "string/strtok/stpsep.h"

#include <assert.h>


#ifndef USE_PAM
static void
Expand All @@ -56,7 +58,7 @@ static void read_env_file (const char *filename)
if (NULL == fp) {
return;
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
while (fgets_a(buf, fp) != NULL) {
if (stpsep(buf, "\n") == NULL)
break;

Expand Down
3 changes: 2 additions & 1 deletion lib/ttytype.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "defines.h"
#include "getdef.h"
#include "io/fgets/fgets.h"
#include "prototypes.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
Expand Down Expand Up @@ -48,7 +49,7 @@ void ttytype (const char *line)
perror (typefile);
return;
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
while (fgets_a(buf, fp) != NULL) {
if (strprefix(buf, "#")) {
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/tz.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "defines.h"
#include "getdef.h"
#include "io/fgets/fgets.h"
#include "prototypes.h"
#include "string/strtok/stpsep.h"

Expand All @@ -37,7 +38,7 @@

fp = fopen (fname, "r");
if ( (NULL == fp)
|| (fgets(tzbuf, sizeof(tzbuf), fp) == NULL)) {
|| (fgets_a(tzbuf, fp) == NULL)) {
result = "TZ=CST6CDT";
} else {
stpsep(tzbuf, "\n");
Expand Down
3 changes: 2 additions & 1 deletion lib/user_busy.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "atoi/getnum.h"
#include "defines.h"
#include "fs/readlink/readlinknul.h"
#include "io/fgets/fgets.h"
#include "prototypes.h"
#ifdef ENABLE_SUBIDS
#include "subordinateio.h"
Expand Down Expand Up @@ -128,7 +129,7 @@ static int check_status (const char *name, const char *sname, uid_t uid)
if (NULL == sfile) {
return 0;
}
while (fgets(line, sizeof(line), sfile) != NULL) {
while (fgets_a(line, sfile) != NULL) {
if (strprefix(line, "Uid:\t")) {
unsigned long ruid, euid, suid;

Expand Down
3 changes: 2 additions & 1 deletion src/chgpasswd.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#endif
/*@-exitarg@*/
#include "exitcodes.h"
#include "io/fgets/fgets.h"
#include "shadow/gshadow/sgrp.h"
#include "shadowlog.h"
#include "string/strcmp/streq.h"
Expand Down Expand Up @@ -476,7 +477,7 @@ int main (int argc, char **argv)
* group entry for each group will be looked up in the appropriate
* file (gshadow or group) and the password changed.
*/
while (fgets(buf, sizeof(buf), stdin) != NULL) {
while (fgets_a(buf, stdin) != NULL) {
line++;
if (stpsep(buf, "\n") == NULL) {
fprintf (stderr, _("%s: line %jd: line too long\n"),
Expand Down
5 changes: 3 additions & 2 deletions src/chpasswd.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "shadowio.h"
/*@-exitarg@*/
#include "exitcodes.h"
#include "io/fgets/fgets.h"
#include "shadowlog.h"
#include "string/strcmp/streq.h"
#include "string/strerrno.h"
Expand Down Expand Up @@ -522,14 +523,14 @@ int main (int argc, char **argv)
* last change date is set in the age only if aging information is
* present.
*/
while (fgets(buf, sizeof(buf), stdin) != NULL) {
while (fgets_a(buf, stdin) != NULL) {
char *cp;

line++;
if (stpsep(buf, "\n") == NULL) {
if (feof (stdin) == 0) {
// Drop all remaining characters on this line.
while (fgets(buf, sizeof(buf), stdin) != NULL) {
while (fgets_a(buf, stdin) != NULL) {
if (strchr(buf, '\n'))
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/login_nopam.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include <unistd.h>

#include "defines.h"
#include "io/fgets/fgets.h"
#include "prototypes.h"
#include "sizeof.h"
#include "string/strcmp/strcaseeq.h"
Expand Down Expand Up @@ -100,7 +101,7 @@ login_access(const char *user, const char *from)
if (NULL != fp) {
intmax_t lineno = 0; /* for diagnostics */
while ( !match
&& (fgets(line, sizeof(line), fp) != NULL))
&& (fgets_a(line, fp) != NULL))
{
char *p;

Expand Down
3 changes: 2 additions & 1 deletion src/newusers.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "defines.h"
#include "getdef.h"
#include "groupio.h"
#include "io/fgets/fgets.h"
#include "nscd.h"
#include "prototypes.h"
#include "pwio.h"
Expand Down Expand Up @@ -1111,7 +1112,7 @@ int main (int argc, char **argv)
* over 100 is allocated. The pw_gid field will be updated with that
* value.
*/
while (fgets(buf, sizeof(buf), stdin) != NULL) {
while (fgets_a(buf, stdin) != NULL) {
line++;
if (stpsep(buf, "\n") == NULL && feof(stdin) == 0) {
fprintf (stderr, _("%s: line %jd: line too long\n"),
Expand Down
3 changes: 2 additions & 1 deletion src/suauth.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <sys/types.h>

#include "defines.h"
#include "io/fgets/fgets.h"
#include "prototypes.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
Expand Down Expand Up @@ -73,7 +74,7 @@ check_su_auth(const char *actual_id, const char *wanted_id, bool su_to_root)
return DENY;
}

while (fgets(temp, sizeof(temp), authfile_fd) != NULL) {
while (fgets_a(temp, authfile_fd) != NULL) {
char *p;

lines++;
Expand Down
5 changes: 3 additions & 2 deletions src/useradd.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "fs/mkstemp/fmkomstemp.h"
#include "getdef.h"
#include "groupio.h"
#include "io/fgets/fgets.h"
#include "nscd.h"
#include "prototypes.h"
#include "pwauth.h"
Expand Down Expand Up @@ -356,7 +357,7 @@ get_defaults(const struct option_flags *flags)
* Read the file a line at a time. Only the lines that have relevant
* values are used, everything else can be ignored.
*/
while (fgets(buf, sizeof(buf), fp) != NULL) {
while (fgets_a(buf, fp) != NULL) {
stpsep(buf, "\n");

cp = stpsep(buf, "=");
Expand Down Expand Up @@ -588,7 +589,7 @@ set_defaults(void)
goto skip;
}

while (fgets(buf, sizeof(buf), ifp) != NULL) {
while (fgets_a(buf, ifp) != NULL) {
char *val;

if (stpsep(buf, "\n") == NULL) {
Expand Down
Loading