summaryrefslogtreecommitdiff
path: root/modules/pam_pwdb/pam_unix_passwd.-c
blob: 0949af7fed1bbd4c6cc760f722fe420d006fec4f (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* $Id$ */

static const char rcsid_pass[] =
"$Id$\n"
" - PAM_PWDB password module <morgan@parc.power.net>"
;

#include "pam_unix_pwupd.-c"

/* passwd/salt conversion macros */

#define ascii_to_bin(c) ((c)>='a'?(c-59):(c)>='A'?((c)-53):(c)-'.')
#define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')

/* data tokens */

#define _UNIX_OLD_AUTHTOK  "-UN*X-OLD-PASS"
#define _UNIX_NEW_AUTHTOK  "-UN*X-NEW-PASS"

/* Implementation */

/*
 * i64c - convert an integer to a radix 64 character
 */
static int i64c(int i)
{
    if (i < 0)
        return ('.');
    else if (i > 63)
        return ('z');
    if (i == 0)
        return ('.');
    if (i == 1)
        return ('/');
    if (i >= 2 && i <= 11)
        return ('0' - 2 + i);
    if (i >= 12 && i <= 37)
        return ('A' - 12 + i);
    if (i >= 38 && i <= 63)
        return ('a' - 38 + i);
    return ('\0');
}

/*
 * FUNCTION: _pam_unix_chauthtok() 
 *
 * this function works in two passes. The first, when UNIX__PRELIM is
 * set, obtains the previous password. It sets the PAM_OLDAUTHTOK item
 * or stores it as a data item. The second function obtains a new
 * password (verifying if necessary, that the user types it the same a
 * second time.) depending on the 'ctrl' flags this new password may
 * be stored in the PAM_AUTHTOK item or a private data item.
 *
 * Having obtained a new password. The function updates the
 * /etc/passwd (and optionally the /etc/shadow) file(s).
 *
 * Provision is made for the creation of a blank shadow file if none
 * is available, but one is required to update the shadow file -- the
 * intention being for shadow passwords to be seamlessly implemented
 * from the generic UNIX scheme. -- THIS BIT IS PRE-ALPHA.. and included
 * in this release (.52) mostly for the purpose of discussion.
 */

static int _unix_chauthtok(pam_handle_t *pamh, unsigned int ctrl)
{
    int retval;
    unsigned int lctrl;

    /* <DO NOT free() THESE> */
    const char *user;
    const char *pass_old, *pass_new;
    /* </DO NOT free() THESE> */

    D(("called"));

    /*
     * First get the name of a user
     */

    retval = _unix_get_user( pamh, ctrl, "Username: ", &user );
    if ( retval != PAM_SUCCESS ) {
	if ( on(UNIX_DEBUG,ctrl) ) {
	    _log_err(LOG_DEBUG, "password - could not identify user");
	}
	return retval;
    }

    if ( on(UNIX__PRELIM, ctrl) ) {
	/*
	 * obtain and verify the current password (OLDAUTHTOK) for
	 * the user.
	 */

	char *Announce;

	D(("prelim check"));

	if ( _unix_blankpasswd(ctrl, user) ) {

	    return PAM_SUCCESS;

	} else if ( off(UNIX__IAMROOT, ctrl) ) {

	    /* instruct user what is happening */
#define greeting "Changing password for "
	    Announce = (char *) malloc(sizeof(greeting)+strlen(user));
	    if (Announce == NULL) {
		_log_err(LOG_CRIT, "password - out of memory");
		return PAM_BUF_ERR;
	    }
	    (void) strcpy(Announce, greeting);
	    (void) strcpy(Announce+sizeof(greeting)-1, user);
#undef greeting

	    lctrl = ctrl;
	    set(UNIX__OLD_PASSWD, lctrl);
	    retval = _unix_read_password( pamh, lctrl
					  , Announce
					  , "(current) UNIX password: "
					  , NULL
					  , _UNIX_OLD_AUTHTOK
					  , &pass_old );
	    free(Announce);

	    if ( retval != PAM_SUCCESS ) {
		_log_err(LOG_NOTICE
			 , "password - (old) token not obtained");
		return retval;
	    }

	    /* verify that this is the password for this user */

	    retval = _unix_verify_password(pamh, user, pass_old, ctrl);
	} else {
	    D(("process run by root so do nothing this time around"));
	    pass_old = NULL;
	    retval = PAM_SUCCESS;           /* root doesn't have too */
	}

	if ( retval != PAM_SUCCESS ) {
	    D(("Authentication failed"));
	    pass_old = NULL;
	    return retval;
	}

	retval = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
	pass_old = NULL;
	if ( retval != PAM_SUCCESS ) {
	    _log_err(LOG_CRIT, "failed to set PAM_OLDAUTHTOK");
	}

    } else if ( on( UNIX__UPDATE, ctrl ) ) {
	/* tpass is used below to store the _pam_md() return; it
	 * should be _pam_delete()'d. */

	char *tpass=NULL;     

	/*
	 * obtain the proposed password
	 */

	D(("do update"));

	/*
	 * get the old token back. NULL was ok only if root [at this
	 * point we assume that this has already been enforced on a
	 * previous call to this function].
	 */

	if ( off(UNIX_NOT_SET_PASS, ctrl) ) {
	    retval = pam_get_item(pamh, PAM_OLDAUTHTOK
				  , (const void **)&pass_old);
	} else {
	    retval = pam_get_data(pamh, _UNIX_OLD_AUTHTOK
				  , (const void **)&pass_old);
	    if (retval == PAM_NO_MODULE_DATA) {
		retval = PAM_SUCCESS;
		pass_old = NULL;
	    }
	}

	if (retval != PAM_SUCCESS) {
	    _log_err(LOG_NOTICE, "user not authenticated");
	    return retval;
	}

	D(("get new password now"));

	lctrl = ctrl;

	/*
	 * use_authtok is to force the use of a previously entered
	 * password -- needed for pluggable password strength checking
	 */

	if ( on(UNIX_USE_AUTHTOK, lctrl) ) {
	    set(UNIX_USE_FIRST_PASS, lctrl);
	}

	retval = _unix_read_password( pamh, lctrl
				      , NULL
				      , "Enter new UNIX password: "
				      , "Retype new UNIX password: "
				      , _UNIX_NEW_AUTHTOK
				      , &pass_new );

	if ( retval != PAM_SUCCESS ) {
	    if ( on(UNIX_DEBUG,ctrl) ) {
		_log_err(LOG_ALERT
			 , "password - new password not obtained");
	    }
	    pass_old = NULL;                               /* tidy up */
	    return retval;
	}

	D(("returned to _unix_chauthtok"));

	/*
	 * At this point we know who the user is and what they
	 * propose as their new password. Verify that the new
	 * password is acceptable.
	 */

	if (pass_new[0] == '\0') {     /* "\0" password = NULL */
	    pass_new = NULL;
	}

	retval = _pam_unix_approve_pass(pamh, ctrl, pass_old, pass_new);

	if (retval != PAM_SUCCESS) {
	    _log_err(LOG_NOTICE, "new password not acceptable");
	    pass_new = pass_old = NULL;	              /* tidy up */
	    return retval;
	}

	/*
	 * By reaching here we have approved the passwords and must now
	 * rebuild the password database file.
	 *
	 * This includes the fact that the password is _not_ NULL.
	 */

	/*
	 * First we encrypt the new password.
	 *
	 * XXX - this is where we might need some code for RADIUS types
	 *       of password handling... no encryption needed..
	 */

	if ( on(UNIX_MD5_PASS, ctrl) ) {

	    /*
	     * Code lifted from Marek Michalkiewicz's shadow suite. (CG)
	     * removed use of static variables (AGM)
	     */

	    struct timeval tv;
	    MD5_CTX ctx;
	    unsigned char result[16];
	    char *cp = (char *)result;
	    unsigned char tmp[16];
	    int i;

	    GoodMD5Init(&ctx);
	    gettimeofday(&tv, (struct timezone *) 0);
	    GoodMD5Update(&ctx, (void *) &tv, sizeof tv);
	    i = getpid();
	    GoodMD5Update(&ctx, (void *) &i, sizeof i);
	    i = clock();
	    GoodMD5Update(&ctx, (void *) &i, sizeof i);
	    GoodMD5Update(&ctx, result, sizeof result);
	    GoodMD5Final(tmp, &ctx);
	    strcpy(cp, "$1$");  /* magic for the MD5 */
	    cp += strlen(cp);
	    for (i = 0; i < 8; i++)
		*cp++ = i64c(tmp[i] & 077);
	    *cp = '\0';

	    /* no longer need cleartext */
	    pass_new = tpass = _pam_md(pass_new, (const char *)result);

	} else {
	    /*
	     * Salt manipulation is stolen from Rick Faith's passwd
	     * program.  Sorry Rick :) -- alex
	     */

	    time_t tm;
	    char salt[3];

	    time(&tm);
	    salt[0] = bin_to_ascii(tm & 0x3f);
	    salt[1] = bin_to_ascii((tm >> 6) & 0x3f);
	    salt[2] = '\0';

	    if ( off(UNIX_BIGCRYPT, ctrl) && strlen(pass_new) > 8 ) {
		/* to avoid using the _extensions_ of the bigcrypt()
		   function we truncate the newly entered password */
		char *temp = malloc(9);

		if (temp == NULL) {
		    _log_err(LOG_CRIT, "out of memory for password");
		    pass_new = pass_old = NULL;	         /* tidy up */
		    return PAM_BUF_ERR;
		}

		/* copy first 8 bytes of password */
		strncpy(temp, pass_new, 8);
		temp[8] = '\0';

		/* no longer need cleartext */
		pass_new = tpass = _pam_md( temp, salt );

		_pam_delete(temp);                       /* tidy up */
	    } else {
		/* no longer need cleartext */
		pass_new = tpass = _pam_md( pass_new, salt );
	    }
	}

	D(("password processed"));

	/* update the password database(s) -- race conditions..? */

	retval = unix_update_db(pamh, ctrl, user, pass_old, pass_new);
	pass_old = pass_new = NULL;

    } else {            /* something has broken with the module */

	_log_err(LOG_ALERT, "password received unknown request");
	retval = PAM_ABORT;

    }

    return retval;
}

/* ******************************************************************
 * Copyright (c) Alexander O. Yuriev (alex@bach.cis.temple.edu), 1996.
 * Copyright (c) Andrew Morgan <morgan@parc.power.net> 1996, 1997.
 * Copyright (c) Cristian Gafton, <gafton@redhat.com> 1996, 1997.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, and the entire permission notice in its entirety,
 *    including the disclaimer of warranties.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 * 
 * ALTERNATIVELY, this product may be distributed under the terms of
 * the GNU Public License, in which case the provisions of the GPL are
 * required INSTEAD OF the above restrictions.  (This clause is
 * necessary due to a potential bad interaction between the GPL and
 * the restrictions contained in a BSD-style copyright.)
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */