How to reproduce& # pw useradd -D -e +1y # grep expire_days /etc/pw.conf expire_days = 1541353753 # WTF is 1541353753 days? It's four million years!
Created attachment 187753 [details] fix -e with -D
Hmm, even with a numeric argument it sets the expiry date incorrectly in newly created accounts, see the "Expire" line below: root@gw:~ # pw useradd -D -e 365 root@gw:~ # grep expire_days /etc/pw.conf expire_days = 365 root@gw:~ # root@gw:~ # pw useradd sobaka Password for 'sobaka' is: rcv8n2PJVP9dt root@gw:~ # root@gw:~ # chsh sobaka Changing user information for sobaka. Login: sobaka Password: $6$KwbJbuUKt03pocQG$nC5mirikOyzF1oFbZacRpn2GXBT1scOBvvtJ8Jfsyw7YPpsR0hW8wDSwLK9uuzAXFNt7aTcFgylweMTXtx1Np. Uid [#]: 1873 Gid [# or name]: 1000 Change [month day year]: Expire [month day year]: January 1, 1970 Class: russian Home directory: /home/sobaka Shell: /bin/tcsh Full Name: User & Office Location: Office Phone: Home Phone: Other information:
Even if I treat expire_days as seconds, new accounts are still created with the expiration date in the past (seconds since epoch, not since the moment of account creation).
Created attachment 187757 [details] fix -e and -p with -D, properly use the config values - move the checks to mix_config() as we need to know if we are writing the config (-D) - if writing config, use the values specified on the command line (checking them for validity) - if we are adding the user, first try the value on the command line, then the value in the config, and finally just disable the expiration
Created attachment 187758 [details] fix -e and -p with -D, properly use the config values, update man page Same as above, plus update '-p days' description in the man page.
Created attachment 187759 [details] proposed fix This patch corrects expiration period handling and command line overrides to preconfigured values for -e, -p and -w flags.
(In reply to vas from comment #3) passwd(5) manual page specifies: The expire field is the number of seconds from the epoch, UTC, until the account expires.
Nice catch. You should also add a testcase to usr.sbin/pw/tests/pw_useradd_test.sh
(In reply to Alan Somers from comment #8) Is there any guide about writing such tests?
The only relevant guides are the tests(7) and atf(7) man pages, and everything they link to. For this PR it should be sufficient to copy one of the existing tests in that file as a template. Make sure you include the PR number in a comment.
(In reply to Eugene Grosbein from comment #7) Yes, but expire_days form pw.conf should somehow translate to now()+expire_days when actually placing the value into the passwd(5) expire field, which is not happening.
Created attachment 187765 [details] proposed fix
(In reply to vas from comment #11) You are right. Please try updated patch.
(In reply to Eugene Grosbein from comment #13) Looks like the patch works: root@gw:~ # pw useradd -D -e 0 root@gw:~ # grep expire_days /etc/pw.conf expire_days = 0 root@gw:~ # pw useradd -D -e 365 root@gw:~ # grep expire_days /etc/pw.conf expire_days = 365 root@gw:~ # pw useradd sobaka root@gw:~ # chsh sobaka [...] Expire [month day year]: November 6, 2018
A commit references this bug: Author: eugen Date: Sat Dec 9 23:34:01 UTC 2017 New revision: 326738 URL: https://svnweb.freebsd.org/changeset/base/326738 Log: pw(8): correct expiration period handling and command line overrides to preconfigured values for -e, -p and -w flags. Use non-negative symbols instead of magic values in passwd_val/pw_password functions. PR: 223431 Submitted by: Yuri Pankov (in part, patch for the manual) Reported by: mav (mentor) MFC after: 3 days Relnotes: yes Changes: head/usr.sbin/pw/psdate.c head/usr.sbin/pw/psdate.h head/usr.sbin/pw/pw.8 head/usr.sbin/pw/pw.h head/usr.sbin/pw/pw_conf.c head/usr.sbin/pw/pw_user.c
A commit references this bug: Author: eugen Date: Thu Dec 14 13:06:42 UTC 2017 New revision: 326848 URL: https://svnweb.freebsd.org/changeset/base/326848 Log: MFC r326738: pw(8): correct expiration period handling and command line overrides to preconfigured values for -e, -p and -w flags. Use non-negative symbols instead of magic values in passwd_val/pw_password functions. PR: 223431 Submitted by: Yuri Pankov (in part, patch for the manual) Approved by: mav (mentor) Relnotes: yes Changes: _U stable/11/ stable/11/usr.sbin/pw/psdate.c stable/11/usr.sbin/pw/psdate.h stable/11/usr.sbin/pw/pw.8 stable/11/usr.sbin/pw/pw.h stable/11/usr.sbin/pw/pw_conf.c stable/11/usr.sbin/pw/pw_user.c
A commit references this bug: Author: eugen Date: Thu Dec 14 13:10:23 UTC 2017 New revision: 326849 URL: https://svnweb.freebsd.org/changeset/base/326849 Log: MFC r326738: pw(8): correct expiration period handling and command line overrides to preconfigured values for -e, -p and -w flags. Use non-negative symbols instead of magic values in passwd_val/pw_password functions. PR: 223431 Submitted by: Yuri Pankov (in part, patch for the manual) Approved by: mav (mentor) Relnotes: yes Changes: _U stable/10/ stable/10/usr.sbin/pw/psdate.c stable/10/usr.sbin/pw/psdate.h stable/10/usr.sbin/pw/pw.8 stable/10/usr.sbin/pw/pw.h stable/10/usr.sbin/pw/pw_conf.c stable/10/usr.sbin/pw/pw_user.c
Fixed, thank you for the report!