From owner-freebsd-questions@FreeBSD.ORG Fri Oct 21 16:53:47 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9FCDA106564A for ; Fri, 21 Oct 2011 16:53:47 +0000 (UTC) (envelope-from cjr@cruwe.de) Received: from cruwe.de (cruwe.de [188.40.164.98]) by mx1.freebsd.org (Postfix) with ESMTP id 309C78FC14 for ; Fri, 21 Oct 2011 16:53:46 +0000 (UTC) Received: from cruwe.de (unknown [127.0.0.4]) by cruwe.de (Postfix) with ESMTP id D038728E28 for ; Fri, 21 Oct 2011 16:53:45 +0000 (UTC) Received: by cruwe.de (Postfix, from userid 65534) id B2D3D28E26; Fri, 21 Oct 2011 16:53:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.cruwe.de X-Spam-Level: X-Spam-Status: No, score=-1.0 required=4.0 tests=ALL_TRUSTED autolearn=unavailable version=3.3.1 Received: from dijkstra (p57BDF4D9.dip.t-dialin.net [87.189.244.217]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by cruwe.de (Postfix) with ESMTPSA id AC06C28E20 for ; Fri, 21 Oct 2011 16:53:43 +0000 (UTC) Date: Fri, 21 Oct 2011 18:53:33 +0200 From: "Christopher J. Ruwe" To: Message-ID: <20111021185333.7c197a2a@dijkstra> X-Mailer: Claws Mail 3.7.10 (GTK+ 2.24.6; amd64-portbld-freebsd8.2) Mime-Version: 1.0 Content-Type: multipart/signed; micalg=PGP-SHA1; boundary="Sig_/KSJWPN/4p3V9fMgEx=oJYIa"; protocol="application/pgp-signature" X-Virus-Scanned: ClamAV on mail.cruwe.de using ClamSMTP Subject: trying to learn systems programming, fear I have not understood and thus messed up X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Oct 2011 16:53:47 -0000 --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 " 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--