Changeset 55417bb in freewrt


Ignore:
Timestamp:
Jun 28, 2007, 7:48:27 PM (18 years ago)
Author:
Thorsten Glaser <tg@…>
Children:
116879b
Parents:
5986dbb
Message:

implement actually working password change code (without fwcf commit). Yay!

git-svn-id: svn://www.freewrt.org/trunk/freewrt@3014 afb5a338-a214-0410-bd46-81f09a774fd1

Location:
package/fwwif/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • package/fwwif/src/Makefile

    r5986dbb r55417bb  
    3030chpw.o c_chpw.o: c_chpw.h common.h
    3131md5.o md5hl.o: md5.h common.h
    32 md5crypt.o pwd_gensalt.o: md5crypt.h md5.h common.h
     32md5crypt.o pwd_gensalt.o c_chpw.o: md5crypt.h md5.h common.h
    3333
    3434md5hl.c: helper.c
  • package/fwwif/src/c_chpw.c

    r5986dbb r55417bb  
    2323 */
    2424
     25#define _GNU_SOURCE
    2526#include "common.h"
     27#include <errno.h>
    2628#include <stdio.h>
     29#include <stdlib.h>
     30#include <string.h>
     31#include <unistd.h>
    2732#include "c_chpw.h"
     33#include "md5crypt.h"
    2834
    2935int
    3036c_chpw(const char *oldpw, const char *newpw)
    3137{
    32         fprintf(stdout, "test: oldpw=<%s>\n", oldpw);
    33         fprintf(stderr, "test: newpw=<%s>\n", newpw);
    34         return (1);
     38        FILE *fi, *fo;
     39        char *lp, *cp, *rootline = NULL, *adminline = NULL, *ap;
     40        char salt[16];
     41        size_t n;
     42        int rv = 1;
     43
     44        if ((fi = fopen("/etc/passwd", "rb")) == NULL) {
     45                printf("Error opening /etc/passwd for reading: %s\n",
     46                    strerror(errno));
     47                return (1);
     48        }
     49        if ((fo = fopen("/etc/passwd.new", "wb")) == NULL) {
     50                printf("Error opening /etc/passwd.new for writing: %s\n",
     51                    strerror(errno));
     52                fclose(fi);
     53                return (1);
     54        }
     55
     56        lp = NULL;
     57        while (getline(&lp, &n, fi) > 0) {
     58                if ((cp = strchr(lp, '\n')))
     59                        *cp = '\0';
     60                if (!strncmp(lp, "root:", 5)) {
     61                        if (rootline == NULL)
     62                                rootline = strdup(lp);
     63                } else if (!strncmp(lp, "admin:", 6)) {
     64                        if (adminline == NULL)
     65                                adminline = strdup(lp);
     66                } else
     67                        fprintf(fo, "%s\n", lp);
     68        }
     69        if (lp)
     70                free(lp);
     71        lp = NULL;
     72
     73        if (rootline == NULL) {
     74                printf("No root account found!\n");
     75                goto out;
     76        }
     77        lp = rootline + /* "root:" */ 5;
     78        cp = strchr(lp, ':');
     79        if (cp == NULL) {
     80                printf("No root password found!\n");
     81                goto out;
     82        }
     83        *cp++ = '\0';
     84        rootline = cp;
     85
     86        /* lp = root password */
     87        cp = md5crypt(oldpw, lp);
     88        if (strcmp(cp, lp)) {
     89                printf("root pw mismatch: '%s' != '%s'\n", cp, lp);
     90                goto out;
     91        }
     92
     93        /* we are authenticated, now change root and admin password */
     94        if (!pwd_gensalt(salt, sizeof (salt)))
     95                strlcpy(salt, lp, sizeof (salt));
     96        cp = md5crypt(newpw, salt);
     97        fprintf(fo, "root:%s:%s\n", cp, rootline);
     98        if (adminline)
     99                if ((ap = strchr(adminline + /* admin: */ 6, ':')) == NULL) {
     100                        free(adminline);
     101                        adminline = NULL;
     102                }
     103        fprintf(fo, "admin:%s:%s\n", cp, adminline ? ap + 1 :
     104            "100:100:admin:/tmp:/bin/sh");
     105
     106        if (fclose(fo)) {
     107                printf("Error closing new password file: %s\n",
     108                    strerror(errno));
     109                goto out;
     110        }
     111        fo = NULL;
     112        fclose(fi);
     113        fi = NULL;
     114        if (rename("/etc/passwd", "/etc/passwd.old")) {
     115                printf("Error renaming old password file: %s\n",
     116                    strerror(errno));
     117                goto out;
     118        }
     119        if (rename("/etc/passwd.new", "/etc/passwd")) {
     120                printf("Error renaming new password file: %s\n",
     121                    strerror(errno));
     122                rename("/etc/passwd.old", "/etc/passwd");
     123                goto out;
     124        }
     125        rv = 0;
     126
     127 out:
     128        if (adminline)
     129                free(adminline);
     130        if (lp)
     131                free(lp - /* "root:" */ 5);
     132        if (fo) {
     133                fclose(fo);
     134                unlink("/etc/passwd.new");
     135        }
     136        if (fi)
     137                fclose(fi);
     138
     139        return (rv);
    35140}
  • package/fwwif/src/chpw.ecpp

    r5986dbb r55417bb  
    4040</%cpp>
    4141<h1>Change Password</h1>
     42<p>You can change the password for your FreeWRT box here. The old password
     43 you must enter has to match against the current root password. The new
     44 passwords must match each other and will set the new passwords for the
     45 root user, the admin user <!-- and the web interface --> to their value.</p>
    4246<%cpp>
    4347        } else if (newpw1 != newpw2) {
     
    5559        if (i) {
    5660</%cpp>
    57 <form action="/chpw.htm" method="get">
     61<form action="/chpw.htm" method="post">
    5862<table>
    5963 <tr>
Note: See TracChangeset for help on using the changeset viewer.