aboutsummaryrefslogtreecommitdiff
path: root/toys/lsb/passwd.c
blob: 687d4c09b33ed3e2eff919080a23652812b18862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* passwd.c - Program to update user password.
 *
 * Copyright 2012 Ashwini Kumar <ak.ashwini@gmail.com>
 * Modified 2012 Jason Kyungwan Han <asura321@gmail.com>
 *
 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/passwd.html

USE_PASSWD(NEWTOY(passwd, ">1a:dlu", TOYFLAG_STAYROOT|TOYFLAG_USR|TOYFLAG_BIN))

config PASSWD
  bool "passwd"
  default y
  depends on TOYBOX_SHADOW
  help
    usage: passwd [-a ALGO] [-dlu] <account name>

    update user's authentication tokens. Default : current user

    -a ALGO	Encryption method (des, md5, sha256, sha512) default: des
    -d		Set password to ''
    -l		Lock (disable) account
    -u		Unlock (enable) account

config PASSWD_SAD
  bool "Add sad password checking heuristics"
  default n
  depends on PASSWD
  help
    Password changes are checked to make sure they don't include the entire
    username (but not a subset of it), and the entire previous password
    (but changing password1, password2, password3 is fine). This heuristic
    accepts "aaaaaa" as a password.
*/

#define FOR_passwd
#include "toys.h"

GLOBALS(
  char *algo;
)

static int str_check(char *s, char *p)
{
  if (strnstr(s, p) || strnstr(p, s)) return 1;
  return 0;
}

// Insane heuristic won't find password1 password2 password3...?
static void strength_check(char *newp, char *oldp, char *user)
{
  char *msg = NULL;

  if (strlen(newp) < 6) { //Min passwd len
    msg = "too short";
    xprintf("BAD PASSWORD: %s\n",msg);
  }
  if (!newp[0]) return; //passwd is empty

  if (str_check(newp, user)) {
    msg = "user based password";
    xprintf("BAD PASSWORD: %s\n",msg);
  }

  if (oldp[0] && str_check(newp, oldp)) {
    msg = "based on old passwd";
    xprintf("BAD PASSWORD: %s\n",msg);
  }
}

static int verify_passwd(char * pwd)
{
  char * pass;

  if (!pwd) return 1;
  if (pwd[0] == '!' || pwd[0] == '*') return 1;

  pass = crypt(toybuf, pwd);
  if (pass  && !strcmp(pass, pwd)) return 0;

  return 1;
}

static char *new_password(char *oldp, char *user)
{
  char *newp = NULL;

  if (read_password(toybuf, sizeof(toybuf), "New password:"))
    return NULL; //may be due to Ctrl-C

  newp = xstrdup(toybuf);
  if (CFG_PASSWD_SAD) strength_check(newp, oldp, user);
  if (read_password(toybuf, sizeof(toybuf), "Retype password:")) {
    free(newp);
    return NULL; //may be due to Ctrl-C
  }

  if (!strcmp(newp, toybuf)) return newp;
  else error_msg("Passwords do not match.\n");
  // Failure Case
  free(newp);
  return NULL;
}

void passwd_main(void)
{
  uid_t myuid;
  struct passwd *pw;
  struct spwd *sp;
  char *name = NULL, *pass = NULL, *encrypted = NULL, *newp = NULL,
       *orig = (char *)"", salt[MAX_SALT_LEN];
  int ret = -1;

  myuid = getuid();
  if (myuid && (toys.optflags & (FLAG_l | FLAG_u | FLAG_d)))
    error_exit("Not root");

  pw = xgetpwuid(myuid);

  if (*toys.optargs) name = toys.optargs[0];
  else name = xstrdup(pw->pw_name);

  pw = xgetpwnam(name);

  if (myuid && (myuid != pw->pw_uid)) error_exit("Not root");

  pass = pw->pw_passwd;
  if (pw->pw_passwd[0] == 'x') {
    //get shadow passwd
    sp = getspnam(name);
    if (sp) pass = sp->sp_pwdp;
  }


  if (!(toys.optflags & (FLAG_l | FLAG_u | FLAG_d))) {
    
    if (!(toys.optflags & FLAG_a)) TT.algo = "des";
    if (get_salt(salt, TT.algo) == -1)
      error_exit("Error: Unkown encryption algorithm\n");

    printf("Changing password for %s\n",name);
    if (myuid && pass[0] == '!')
      error_exit("Can't change, password is locked for %s",name);
    if (myuid) {
      //Validate user 

      if (read_password(toybuf, sizeof(toybuf), "Origial password:")) {
        if (!toys.optargs[0]) free(name);
        return;
      }
      orig = toybuf;
      if (verify_passwd(pass)) error_exit("Authentication failed\n");
    }

    orig = xstrdup(orig);

    // Get new password
    newp = new_password(orig, name);
    if (!newp) {
      free(orig);
      if (!toys.optargs[0]) free(name);
      return; //new password is not set well.
    }

    encrypted = crypt(newp, salt);
    free(newp);
    free(orig);
  } else if (toys.optflags & FLAG_l) {
    if (pass[0] == '!') error_exit("password is already locked for %s",name);
    printf("Locking password for %s\n",name);
    encrypted = xmprintf("!%s",pass);
  } else if (toys.optflags & FLAG_u) {
    if (pass[0] != '!') error_exit("password is already unlocked for %s",name);

    printf("Unlocking password for %s\n",name);
    encrypted = xstrdup(&pass[1]);
  } else if (toys.optflags & FLAG_d) {
    printf("Deleting password for %s\n",name);
    encrypted = xstrdup(""); //1 = "", 2 = '\0'
  }

  // Update the passwd
  if (pw->pw_passwd[0] == 'x')
    ret = update_password("/etc/shadow", name, encrypted);
  else ret = update_password("/etc/passwd", name, encrypted);

  if ((toys.optflags & (FLAG_l | FLAG_u | FLAG_d))) free(encrypted);

  if (!toys.optargs[0]) free(name);
  if (!ret) error_msg("Success");
  else error_msg("Failure");
}