Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 21 Oct 2011 18:53:33 +0200
From:      "Christopher J. Ruwe" <cjr@cruwe.de>
To:        <freebsd-questions@freebsd.org>
Subject:   trying to learn systems programming, fear I have not understood and thus messed up
Message-ID:  <20111021185333.7c197a2a@dijkstra>

next in thread | raw e-mail | index | archive | help
--Sig_/KSJWPN/4p3V9fMgEx=oJYIa
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: quoted-printable

To improve on my C and to learn something about systems programming, I
have begun to pick out "bite-size bits" from the bin-PRs. Currently, I
am dispairing about bin/149972, which is about 1) adding error handling
to pw being invoced "pw -u <username>" and 2) trying to get a uid from
the (incorrectly) passed username. Currently, I cannot accomplish want
I wanted to do, so I turn here for hints or help.

Three developers which I have failed to reach by mail (gpf@, kibab@,
bcr@) have added the necessary code


if ((arg =3D getarg(args, 'u')) !=3D NULL) {
   if(isdigit((unsigned char)*arg->val)) {
   pwd->pw_uid =3D (uid_t) atol(arg->val);
   edited =3D 1;
   if (pwd->pw_uid !=3D 0 && strcmp(pwd->pw_name, "root") =3D=3D 0)
      errx(EX_DATAERR, "can't change uid of `root' account");
   if (pwd->pw_uid =3D=3D 0 && strcmp(pwd->pw_name, "root") !=3D 0)
      warnx("WARNING: account `%s' will have a uid of 0 (superuser
access!)", pwd->pw_name); } else {
  /* Found something, but not a number */
  /*
   * XXX Shouldn't we try to map the passed string to the username?
   * man page however says that we're expecting numeric uid...
   */
  errx(EX_DATAERR, "Expected numeric user id as an argument to -u\n");
  }
}

I have tried to follow the suggestion from the comment by modifiying
the else-statement thus:

} 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);

   pwd->pw_uid =3D (uid_t) (trgpwd->pw_uid);		   =20
   edited =3D 1;
}=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.

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.

I am grateful for any suggestions and/or correction.=20
--=20
Christopher J. Ruwe
TZ GMT + 2


--Sig_/KSJWPN/4p3V9fMgEx=oJYIa
Content-Type: application/pgp-signature; name=signature.asc
Content-Disposition: attachment; filename=signature.asc

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

iQIcBAEBAgAGBQJOoaOUAAoJEJTIKW/o3iwUtaUP/jR5wTY21F+WfbHAlgIowlDS
3S97t0Ak9j7SzM2+zmcRY0KH/5c41UzEygRhUnw0VZS/m19QUvLDKKsf5tU8ZlZq
JjJR4KpIZfDFuST2picmwegw4rYaZ/1yK9gauN5uQLqjRVgNw8m7mY+PRW+h9+Sn
oVEQUbYrwsMpqMBZ5ySEuqnJ6vdbnJdW2EVvtdqQoVLS66XTZUbD5fyEJJq2mvPr
OmHQvKrradej/2IwlvkboJ1sLVr8TY2CceqBXWLsEQoktNqY8JljrOB7+f1m6f2l
wApUHkd9JMWVM3BFRhVUWL9vyHEzsI7PTIeYXgECPLjwIhTJcuC7RYFMfdUgyUCI
Pwp3ll93MToOWh8RiFmuNJ1R4LfvOuSo0whDMf9QcL0xxTdWS/gHwS45sg9Jlr0b
HWpYRQPwvVItEiRUplqisvdlibOotQMEFe4JG1E7EDVUjoS0xYfAapCt64Czytmy
l78UJ5EHGhCS2aIjTQw9ozWqlpztm9x78nObONPW/8v2HNTet3szIQgrd0v8YmHE
jRC8bLT7W/K6LW6+hYAQ3bduvhckc9p//8hy7FKl1w0K1bjcuPXOIOXFR2EGi2Fc
DdESoIpvCCr9ZdvA8jCcBkwI6IY8c+TwpnHNpxnuHp4i4E51fnHoLo/H+a39In/1
uxUF0jh2yFgfZsOyPdXL
=++oG
-----END PGP SIGNATURE-----

--Sig_/KSJWPN/4p3V9fMgEx=oJYIa--



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