Date: Sun, 7 Jun 2015 19:33:25 +0000 (UTC) From: Baptiste Daroussin <bapt@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r284129 - in head/usr.sbin/pw: . tests Message-ID: <201506071933.t57JXPvl054850@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: bapt Date: Sun Jun 7 19:33:25 2015 New Revision: 284129 URL: https://svnweb.freebsd.org/changeset/base/284129 Log: In case of rename validate the length of the new name Check early that the new name fits MAXLOGNAME and store it in pwconf Modified: head/usr.sbin/pw/pw.c head/usr.sbin/pw/pw_group.c head/usr.sbin/pw/pw_user.c head/usr.sbin/pw/pwupd.h head/usr.sbin/pw/tests/pw_groupmod.sh head/usr.sbin/pw/tests/pw_usermod.sh Modified: head/usr.sbin/pw/pw.c ============================================================================== --- head/usr.sbin/pw/pw.c Sun Jun 7 19:03:41 2015 (r284128) +++ head/usr.sbin/pw/pw.c Sun Jun 7 19:33:25 2015 (r284129) @@ -234,6 +234,11 @@ main(int argc, char *argv[]) case 'N': conf.dryrun = true; break; + case 'l': + if (strlen(optarg) >= MAXLOGNAME) + errx(EX_USAGE, "new name too long: %s", optarg); + conf.newname = optarg; + break; case 'P': conf.pretty = true; break; Modified: head/usr.sbin/pw/pw_group.c ============================================================================== --- head/usr.sbin/pw/pw_group.c Sun Jun 7 19:03:41 2015 (r284128) +++ head/usr.sbin/pw/pw_group.c Sun Jun 7 19:33:25 2015 (r284129) @@ -51,7 +51,6 @@ int pw_group(int mode, char *name, long id, struct cargs * args) { int rc; - struct carg *a_newname = getarg(args, 'l'); struct carg *arg; struct group *grp = NULL; int grmembers = 0; @@ -133,8 +132,8 @@ pw_group(int mode, char *name, long id, if (id > 0) grp->gr_gid = (gid_t) id; - if (a_newname != NULL) - grp->gr_name = pw_checkname(a_newname->val, 0); + if (conf.newname != NULL) + grp->gr_name = pw_checkname(conf.newname, 0); } else { if (name == NULL) /* Required */ errx(EX_DATAERR, "group name required"); @@ -262,8 +261,8 @@ pw_group(int mode, char *name, long id, err(EX_IOERR, "group update"); } - if (a_newname != NULL) - name = a_newname->val; + if (conf.newname != NULL) + name = conf.newname; /* grp may have been invalidated */ if ((grp = GETGRNAM(name)) == NULL) errx(EX_SOFTWARE, "group disappeared during update"); Modified: head/usr.sbin/pw/pw_user.c ============================================================================== --- head/usr.sbin/pw/pw_user.c Sun Jun 7 19:03:41 2015 (r284128) +++ head/usr.sbin/pw/pw_user.c Sun Jun 7 19:33:25 2015 (r284129) @@ -382,10 +382,10 @@ pw_user(int mode, char *name, long id, s /* * The rest is edit code */ - if ((arg = getarg(args, 'l')) != NULL) { + if (conf.newname != NULL) { if (strcmp(pwd->pw_name, "root") == 0) errx(EX_DATAERR, "can't rename `root' account"); - pwd->pw_name = pw_checkname(arg->val, 0); + pwd->pw_name = pw_checkname(conf.newname, 0); edited = 1; } @@ -676,8 +676,8 @@ pw_user(int mode, char *name, long id, s pwd = GETPWNAM(name); if (pwd == NULL) { /* This will fail when we rename, so special case that */ - if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) { - name = arg->val; /* update new name */ + if (mode == M_UPDATE && conf.newname != NULL) { + name = conf.newname; /* update new name */ pwd = GETPWNAM(name); /* refetch renamed rec */ } } Modified: head/usr.sbin/pw/pwupd.h ============================================================================== --- head/usr.sbin/pw/pwupd.h Sun Jun 7 19:03:41 2015 (r284128) +++ head/usr.sbin/pw/pwupd.h Sun Jun 7 19:33:25 2015 (r284129) @@ -83,6 +83,7 @@ struct userconf { struct pwconf { char rootdir[MAXPATHLEN]; char etcpath[MAXPATHLEN]; + char *newname; bool dryrun; bool pretty; bool v7; Modified: head/usr.sbin/pw/tests/pw_groupmod.sh ============================================================================== --- head/usr.sbin/pw/tests/pw_groupmod.sh Sun Jun 7 19:03:41 2015 (r284128) +++ head/usr.sbin/pw/tests/pw_groupmod.sh Sun Jun 7 19:33:25 2015 (r284129) @@ -71,10 +71,21 @@ do_not_duplicate_group_on_gid_change_bod atf_check -o inline:"testgroup:*:12345:\n" -s exit:0 -x grep "^testgroup" ${HOME}/group } +atf_test_case groupmod_rename +groupmod_rename_body() { + populate_etc_skel + + atf_check -s exit:0 ${PW} groupadd foo + atf_check -s exit:0 ${PW} groupmod foo -l bar + atf_check -s exit:0 -o match:"^bar:.*" \ + grep "^bar:.*" ${HOME}/group +} + atf_init_test_cases() { atf_add_test_case groupmod_user atf_add_test_case groupmod_invalid_user atf_add_test_case groupmod_bug_193704 atf_add_test_case usermod_bug_185666 atf_add_test_case do_not_duplicate_group_on_gid_change + atf_add_test_case groupmod_rename } Modified: head/usr.sbin/pw/tests/pw_usermod.sh ============================================================================== --- head/usr.sbin/pw/tests/pw_usermod.sh Sun Jun 7 19:03:41 2015 (r284128) +++ head/usr.sbin/pw/tests/pw_usermod.sh Sun Jun 7 19:33:25 2015 (r284129) @@ -100,13 +100,34 @@ user_mod_name_noupdate_body() { grep "^foo:.*" $HOME/master.passwd } +atf_test_case user_mod_rename +user_mod_rename_body() { + populate_etc_skel + + atf_check -s exit:0 ${PW} useradd foo + atf_check -s exit:0 ${PW} usermod foo -l bar + atf_check -s exit:0 -o match:"^bar:.*" \ + grep "^bar:.*" ${HOME}/master.passwd +} + +atf_test_case user_mod_rename_too_long +user_mod_rename_too_long_body() { + populate_etc_skel + + atf_check -s exit:0 ${PW} useradd foo + atf_check -s exit:64 -e match:"too long" ${PW} usermod foo \ + -l name_very_very_very_very_very_long +} + atf_init_test_cases() { atf_add_test_case user_mod atf_add_test_case user_mod_noupdate atf_add_test_case user_mod_comments atf_add_test_case user_mod_comments_noupdate - atf_add_test_case user_mod_comments_invalid - atf_add_test_case user_mod_comments_invalid_noupdate - atf_add_test_case user_mod_name + atf_add_test_case user_mod_comments_invalid + atf_add_test_case user_mod_comments_invalid_noupdate + atf_add_test_case user_mod_rename atf_add_test_case user_mod_name_noupdate + atf_add_test_case user_mod_rename + atf_add_test_case user_mod_rename_too_long }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201506071933.t57JXPvl054850>