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
3 changes: 2 additions & 1 deletion lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ libshadow_la_SOURCES = \
getdef.h \
getgr_nam_gid.c \
getrange.c \
gettime.c \
groupio.c \
groupmem.c \
groupio.h \
Expand Down Expand Up @@ -272,6 +271,8 @@ libshadow_la_SOURCES = \
subordinateio.h \
subordinateio.c \
sulog.c \
time/date.c \
time/date.h \
time/day_to_str.c \
time/day_to_str.h \
ttytype.c \
Expand Down
47 changes: 0 additions & 47 deletions lib/gettime.c

This file was deleted.

3 changes: 0 additions & 3 deletions lib/prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,6 @@ extern int getrange (const char *range,
unsigned long *min, bool *has_min,
unsigned long *max, bool *has_max);

/* gettime.c */
extern time_t gettime (void);

/* fputsx.c */
ATTR_ACCESS(write_only, 1, 2)
extern /*@null@*/char *fgetsx(/*@returned@*/char *restrict, int, FILE *restrict);
Expand Down
15 changes: 5 additions & 10 deletions lib/pwd2spwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

#include "config.h"

#ident "$Id$"

#include <pwd.h>
#include <sys/types.h>
#include "prototypes.h"

#include "defines.h"
#include <pwd.h>
#include "prototypes.h"
#include "time/date.h"

/*
* pwd_to_spwd - create entries for new spwd structure
Expand All @@ -40,12 +40,7 @@ struct spwd *pwd_to_spwd (const struct passwd *pw)
*/
sp.sp_min = 0;
sp.sp_max = 10000;
sp.sp_lstchg = gettime () / DAY;
if (0 == sp.sp_lstchg) {
/* Better disable aging than requiring a password
* change */
sp.sp_lstchg = -1;
}
sp.sp_lstchg = date_or_SDE();
}

/*
Expand Down
45 changes: 45 additions & 0 deletions lib/time/date.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-FileCopyrightText: 2017, Chris Lamb
// SPDX-FileCopyrightText: 2023-2025, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#include "config.h"

#include "time/date.h"

#include <errno.h>
#include <limits.h>
#include <stdio.h>

#include "atoi/a2i/a2i.h"
#include "defines.h"
#include "prototypes.h"
#include "shadowlog.h"


extern inline long date_or_SDE(void);


// time(2) or SOURCE_DATE_EPOCH
time_t
time_or_SDE(void)
{
char *env;
FILE *shadow_logfd = log_get_logfd();
time_t t, sde;

t = time(NULL);

env = shadow_getenv("SOURCE_DATE_EPOCH");
if (env == NULL)
return t;

if (a2i(time_t, &sde, env, NULL, 10, 0, t) == -1) {
fprintf(shadow_logfd,
_("Environment variable $SOURCE_DATE_EPOCH: a2i(\"%s\"): %s"),
env, strerror(errno));
return t;
}

return sde;
}
28 changes: 28 additions & 0 deletions lib/time/date.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_TIME_DATE_H_
#define SHADOW_INCLUDE_LIB_TIME_DATE_H_


#include "config.h"

#include <time.h>

#include "defines.h"


inline long date_or_SDE(void);
time_t time_or_SDE(void);


// Like time_or_SDE(), but return a date, not a time.
inline long
date_or_SDE(void)
{
return time_or_SDE() / DAY;
}


#endif // include guard
10 changes: 2 additions & 8 deletions src/chpasswd.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#include "config.h"

#ident "$Id$"

#include <fcntl.h>
#include <getopt.h>
#include <pwd.h>
Expand All @@ -36,6 +34,7 @@
#include "shadowlog.h"
#include "string/strcmp/streq.h"
#include "string/strtok/stpsep.h"
#include "time/date.h"


#define IS_CRYPT_METHOD(str) ((crypt_method != NULL && streq(crypt_method, str)) ? true : false)
Expand Down Expand Up @@ -653,12 +652,7 @@ int main (int argc, char **argv)
if (NULL != sp) {
newsp = *sp;
newsp.sp_pwdp = cp;
newsp.sp_lstchg = gettime () / DAY;
if (0 == newsp.sp_lstchg) {
/* Better disable aging than requiring a
* password change */
newsp.sp_lstchg = -1;
}
newsp.sp_lstchg = date_or_SDE();
}

if ( (NULL == sp)
Expand Down
16 changes: 3 additions & 13 deletions src/newusers.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

#include "config.h"

#ident "$Id$"

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
Expand Down Expand Up @@ -60,6 +58,7 @@
#include "string/strdup/strdup.h"
#include "string/strtok/stpsep.h"
#include "string/strtok/strsep2arr.h"
#include "time/date.h"

struct option_flags {
bool chroot;
Expand Down Expand Up @@ -550,12 +549,7 @@ static int add_passwd (struct passwd *pwd, const char *password)
}
spent.sp_pwdp = cp;
}
spent.sp_lstchg = gettime () / DAY;
if (0 == spent.sp_lstchg) {
/* Better disable aging than requiring a password
* change */
spent.sp_lstchg = -1;
}
spent.sp_lstchg = date_or_SDE();
return (spw_update (&spent) == 0);
}

Expand Down Expand Up @@ -607,11 +601,7 @@ static int add_passwd (struct passwd *pwd, const char *password)
*/
spent.sp_pwdp = "!";
#endif
spent.sp_lstchg = gettime () / DAY;
if (0 == spent.sp_lstchg) {
/* Better disable aging than requiring a password change */
spent.sp_lstchg = -1;
}
spent.sp_lstchg = date_or_SDE();
spent.sp_min = getdef_num ("PASS_MIN_DAYS", 0);
/* 10000 is infinity this week */
spent.sp_max = getdef_num ("PASS_MAX_DAYS", 10000);
Expand Down
13 changes: 3 additions & 10 deletions src/passwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#include "config.h"

#ident "$Id$"

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
Expand Down Expand Up @@ -38,6 +36,7 @@
#include "string/strcmp/strprefix.h"
#include "string/strcpy/strtcpy.h"
#include "string/strdup/strdup.h"
#include "time/date.h"
#include "time/day_to_str.h"


Expand Down Expand Up @@ -668,14 +667,8 @@ static void update_shadow (struct option_flags *flags)
}
if (!use_pam)
{
if (do_update_age) {
nsp->sp_lstchg = gettime () / DAY;
if (0 == nsp->sp_lstchg) {
/* Better disable aging than requiring a password
* change */
nsp->sp_lstchg = -1;
}
}
if (do_update_age)
nsp->sp_lstchg = date_or_SDE();
}

/*
Expand Down
11 changes: 2 additions & 9 deletions src/pwck.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

#include "config.h"

#ident "$Id$"

#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
Expand All @@ -30,6 +28,7 @@
#include "sssd.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
#include "time/date.h"
#ifdef WITH_TCB
#include "tcbfuncs.h"
#endif /* WITH_TCB */
Expand Down Expand Up @@ -634,13 +633,7 @@ static void check_pw_file (bool *errors, bool *changed, struct option_flags *fla
sp.sp_inact = -1;
sp.sp_expire = -1;
sp.sp_flag = SHADOW_SP_FLAG_UNSET;
sp.sp_lstchg = gettime () / DAY;
if (0 == sp.sp_lstchg) {
/* Better disable aging than
* requiring a password change
*/
sp.sp_lstchg = -1;
}
sp.sp_lstchg = date_or_SDE();
*changed = true;

if (spw_update (&sp) == 0) {
Expand Down
10 changes: 2 additions & 8 deletions src/pwconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

#include "config.h"

#ident "$Id$"

#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
Expand All @@ -54,6 +52,7 @@
#include "shadowio.h"
#include "shadowlog.h"
#include "string/strcmp/streq.h"
#include "time/date.h"


/*
Expand Down Expand Up @@ -259,12 +258,7 @@ int main (int argc, char **argv)
spent.sp_flag = SHADOW_SP_FLAG_UNSET;
}
spent.sp_pwdp = pw->pw_passwd;
spent.sp_lstchg = gettime () / DAY;
if (0 == spent.sp_lstchg) {
/* Better disable aging than requiring a password
* change */
spent.sp_lstchg = -1;
}
spent.sp_lstchg = date_or_SDE();
if (spw_update (&spent) == 0) {
fprintf (stderr,
_("%s: failed to prepare the new %s entry '%s'\n"),
Expand Down
9 changes: 2 additions & 7 deletions src/useradd.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#include "config.h"

#ident "$Id$"

#include <assert.h>
#include <ctype.h>
#include <errno.h>
Expand Down Expand Up @@ -75,6 +73,7 @@
#include "string/strcmp/strprefix.h"
#include "string/strdup/strdup.h"
#include "string/strtok/stpsep.h"
#include "time/date.h"


#ifndef SKEL_DIR
Expand Down Expand Up @@ -962,11 +961,7 @@ static void new_spent (struct spwd *spent)
memzero (spent, sizeof *spent);
spent->sp_namp = (char *) user_name;
spent->sp_pwdp = (char *) user_pass;
spent->sp_lstchg = gettime () / DAY;
if (0 == spent->sp_lstchg) {
/* Better disable aging than requiring a password change */
spent->sp_lstchg = -1;
}
spent->sp_lstchg = date_or_SDE();
if (!rflg) {
spent->sp_min = getdef_num ("PASS_MIN_DAYS", -1);
spent->sp_max = getdef_num ("PASS_MAX_DAYS", -1);
Expand Down
Loading
Loading