Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 22 Oct 2011 16:04:53 +0200
From:      "Christopher J. Ruwe" <cjr@cruwe.de>
To:        freebsd-questions@freebsd.org
Subject:   Re: trying to learn systems programming, fear I have not understood and thus messed up
Message-ID:  <20111022160453.35dac1f3@dijkstra>
In-Reply-To: <20111021185333.7c197a2a@dijkstra>
References:  <20111021185333.7c197a2a@dijkstra>

next in thread | previous in thread | raw e-mail | index | archive | help
--Sig_/1rLdDmL7vN6lqRD_ouhCIdb
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: quoted-printable

On Fri, 21 Oct 2011 18:53:33 +0200
"Christopher J. Ruwe" <cjr@cruwe.de> wrote:

> [...]
>=20
> I have tried to follow the suggestion from the comment by modifiying
> the else-statement thus:
>=20
> } else {
>   struct passwd *trgpwd;
>   if (!(trgpwd =3D GETPWNAM(arg->val)))
>      errx(EX_DATAERR, "User %s does not exist", arg->val);
>   =20
>    if (strcmp(a_name->val,"root") =3D=3D 0)
>       errx(EX_DATAERR, "can't change uid of `root' account");
>    if (strcmp(trgpwd->pw_name, "root") =3D=3D 0)
>       warnx("WARNING: account `%s' will have a uid of 0 (superuser
> access!)", pwd->pw_name);
>=20
>    pwd->pw_uid =3D (uid_t) (trgpwd->pw_uid);		   =20
>    edited =3D 1;
> }=20
>=20
> What happens is not what I intended. I invoke as "sudo ./pw usermod
> testuser1 -u testuser2". I can get testuser2's pwd-entry by GETPWNAM
> allright, but when I assign the pw_uid, so as to make testuser2's uid
> the same as testuser1's and imgaining to retain all other values, ./pw
> reports "pw: user 'testuser2' disappeared during update" and the
> testuser2's /etc/passwd entry is replaced by testuser1's.
>=20
> I fear I have not understood GETPWNAM correctly, as it seems to
> replace the struct pwd as some sort of sideeffect. I could manually
> set all pwd-members to the correct ones (those of testuser2), but I
> fear that I have messed something up beforehand.
>=20
> I am grateful for any suggestions and/or correction.=20

It seems I have indeed not understood GETPWNAM correctly. I have worked
out a method which works by calling GETPWNAM twice:

   else {
   /*
    * operation as follows:
    * a_name->val is passed as usermod <uname>
    * arg->val is passed as -u <uname>
    *
    * first check if we do someting stupid, i.e., want
    * to set root uid to some other users uid or
    * to set some user accout's uid to root uid.
    * then get pwd to that of uname passed as -u <uname>.
    * store uid from that pwdent.
    * get pwd to that of uname passed as usermod <uname>
    * change uid of that latter uid to the one stored
    */
		   =20
    if(strcmp(a_name->val,"root") =3D=3D 0)
      errx(EX_DATAERR, "can't change uid of `root' account");

    if(strcmp(arg->val, "root") =3D=3D 0)
      warnx("WARNING: account `%s' will have a uid of 0 (superuser
    access!)", pwd->pw_name);

    if(!(pwd =3D GETPWNAM(arg->val))) /* -u <uname>*/
      errx(EX_DATAERR, "User %s does not exist", arg->val);
    int alias_uid =3D pwd->pw_uid;=20

    if(!(pwd =3D GETPWNAM(a_name->val))) /*usermod <uname>*/
      errx(EX_DATAERR, "User %s does not exist", a_name->val);

    pwd->pw_uid =3D (uid_t) alias_uid;
        warnx("User %s's uid changed to %d", pwd->pw_name, pwd->pw_uid);
    edited =3D 1;
  }

As I stil do not know why the latter variant of my code worked and the
former does not, I would still appreciate any comment or explanation which =
would help me understanding GETPWNAM and getpwnam.

Thanks and cheers,
--=20
Christopher J. Ruwe
TZ GMT + 2


--Sig_/1rLdDmL7vN6lqRD_ouhCIdb
Content-Type: application/pgp-signature; name=signature.asc
Content-Disposition: attachment; filename=signature.asc

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.18 (FreeBSD)

iQIcBAEBAgAGBQJOos2MAAoJEJTIKW/o3iwUYOUQALnWqq2URFPsb1lZ2+iEN0vs
k4Azt7/lPHUlXuU8mLm5eOR1rHrCujsTpSUm0N3aGR4z++6gGHX7GTEZyL+Shqvn
mFXBIAA9ClsTOX1GfZcHVXC2PgPAbXfs4dnLmlhDxrfkvWeFDFaM4QCxeJCBcFjP
11Lgw6lHvCS7G0/x6CIWsJrE3FKAWkBLB1HTK7esBlKBc4VjaA+QCMtN0o90MsPR
j8VmY10OAFaItKkzinxay/BEEuOYO5h14wv5ljL7R63uXUUpX04XWusy7DhoActl
gOibI4MkH6O/YgjgGkaxmf1lnOWC6PFqcb2kY2O0Q1mIIoU8a7eUpP41Ll7TGros
MxuRNQ7LYG+DYX4rBMIpPHRzOhkxp9ddNXzJfZqRReCgIOrgIJO2GN7vuYnfmPmU
dZm5TSgclOjxiSyEe/lRFji26WTTGMd00Kj7Xc7EfN3My4WSDhQawDIvWjDAryJa
xWtPFlhXnlteL2XdEPzeD77PvXd4p04EZWpGObMmFngzis39gBp2Y2BvyOWpkWug
vHVrS2uh8VHPyN1m/zErRu81MNVMVFAhJBj87Vz/Dog0KZIrfotKZ9cmVYDcFhRh
SpmZIhl+MJjhyqJ1h2Q/fbkcz8aiGHmpUTmw4mhdMBUb93nEyYCb1iFAdaNRcUgp
f8xOalnXSewlkzrw/V7R
=OiE0
-----END PGP SIGNATURE-----

--Sig_/1rLdDmL7vN6lqRD_ouhCIdb--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20111022160453.35dac1f3>