[hackers] [ubase] passwd: fix /etc/passwd support, ... || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Mon, 14 Jul 2014 12:14:42 +0200

commit 8fdc7d70bd76b43ed760a4ab7e113a9720da9ca1
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sun Jul 13 19:55:46 2014 +0000

    passwd: fix /etc/passwd support, ...
    
    ... rewrite parts to use libc functions (supported by musl and glibc).

diff --git a/passwd.c b/passwd.c
index 2e7627a..113ecf8 100644
--- a/passwd.c
+++ b/passwd.c
_AT_@ -25,29 +25,124 @@ usage(void)
         eprintf("usage: %s [username]
", argv0);
 }
 
+static FILE *
+spw_get_file(const char *user)
+{
+ FILE *fp = NULL;
+ char file[PATH_MAX];
+ int r;
+
+ r = snprintf(file, sizeof(file), "/etc/tcb/%s/shadow", user);
+ if (r < 0 || (size_t)r >= sizeof(file))
+ eprintf("snprintf:");
+ fp = fopen(file, "r+");
+ if (!fp)
+ fp = fopen("/etc/shadow", "r+");
+ return fp;
+}
+
 static int
-gettempfile(char *template)
+spw_write_file(FILE *fp, const struct spwd *spw, char *pwhash)
 {
- int fd;
+ struct spwd *spwent;
+ int r = -1, w = 0;
+ FILE *tfp = NULL;
 
- umask(077);
- fd = mkostemp(template, O_RDWR);
- if (fd < 0)
- weprintf("mkstemp:");
- return fd;
+ /* write to temporary file. */
+ tfp = tmpfile();
+ if (!tfp) {
+ weprintf("tmpfile:");
+ goto cleanup;
+ }
+ while ((spwent = fgetspent(fp))) {
+ /* update entry on name match */
+ if (strcmp(spwent->sp_namp, spw->sp_namp) == 0) {
+ spwent->sp_pwdp = pwhash;
+ w++;
+ }
+ errno = 0;
+ if (putspent(spwent, tfp) == -1) {
+ weprintf("putspent:");
+ goto cleanup;
+ }
+ }
+ if (!w) {
+ weprintf("shadow: no matching entry to write to
");
+ goto cleanup;
+ }
+ fflush(tfp);
+
+ if (fseek(fp, 0, SEEK_SET) == -1 || fseek(tfp, 0, SEEK_SET) == -1) {
+ weprintf("fseek:");
+ goto cleanup;
+ }
+
+ /* write temporary file to (tcb) shadow file */
+ concat(tfp, "tmpfile", fp, "shadow");
+ ftruncate(fileno(fp), ftell(tfp));
+
+ r = 0; /* success */
+cleanup:
+ if (tfp)
+ fclose(tfp);
+ return r;
+}
+
+static
+int pw_write_file(FILE *fp, const struct passwd *pw, char *pwhash) {
+ struct passwd *pwent;
+ int r = -1, w = 0;
+ FILE *tfp = NULL;
+
+ /* write to temporary file. */
+ tfp = tmpfile();
+ if (!tfp) {
+ weprintf("tmpfile:");
+ goto cleanup;
+ }
+ while ((pwent = fgetpwent(fp))) {
+ /* update entry on name match */
+ if (strcmp(pwent->pw_name, pw->pw_name) == 0) {
+ pwent->pw_passwd = pwhash;
+ w++;
+ }
+ errno = 0;
+ if (putpwent(pwent, tfp) == -1) {
+ weprintf("putpwent:");
+ goto cleanup;
+ }
+ }
+ if (!w) {
+ weprintf("passwd: no matching entry to write to
");
+ goto cleanup;
+ }
+ fflush(tfp);
+
+ if (fseek(fp, 0, SEEK_SET) == -1 || fseek(tfp, 0, SEEK_SET) == -1) {
+ weprintf("fseek:");
+ goto cleanup;
+ }
+
+ /* write to passwd file. */
+ concat(tfp, "tmpfile", fp, "passwd");
+ ftruncate(fileno(fp), ftell(tfp));
+
+ r = 0; /* success */
+cleanup:
+ if (tfp)
+ fclose(tfp);
+ return r;
 }
 
 int
 main(int argc, char *argv[])
 {
         char *cryptpass1 = NULL, *cryptpass2 = NULL, *cryptpass3 = NULL;
- char shadowfile[PATH_MAX], *inpass, *p, *pwd = NULL;
- char template[] = "/tmp/pw.XXXXXX";
+ char *inpass, *p, *salt = PW_CIPHER, *prevhash = NULL;
         struct passwd *pw;
- struct spwd *spw = NULL, *spwent;
- uid_t uid;
- FILE *fp = NULL, *tfp = NULL;
- int ffd = -1, tfd = -1, r, status = EXIT_FAILURE;
+ struct spwd *spw = NULL;
+ FILE *fp = NULL;
+ int r = -1, status = EXIT_FAILURE;
 
         ARGBEGIN {
         default:
_AT_@ -55,6 +150,7 @@ main(int argc, char *argv[])
         } ARGEND;
 
         pw_init();
+ umask(077);
 
         errno = 0;
         if (argc == 0)
_AT_@ -69,7 +165,7 @@ main(int argc, char *argv[])
         }
 
         /* is using shadow entry ? */
- if (pw->pw_passwd[0] == 'x') {
+ if (pw->pw_passwd[0] == 'x' && pw->pw_passwd[1] == '
Received on Mon Jul 14 2014 - 12:14:42 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 14 2014 - 12:24:08 CEST