1/* 2 * Copyright (c) 1999-2008 Apple Computer, Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23#include <stdio.h> 24 25#include <security/pam_appl.h> 26#include <security/openpam.h> /* for openpam_ttyconv() */ 27 28extern char* progname; 29static pam_handle_t *pamh; 30static struct pam_conv pamc; 31 32//------------------------------------------------------------------------------------- 33// pam_check_passwd 34//------------------------------------------------------------------------------------- 35 36int pam_check_passwd(char* uname) 37{ 38 int retval = PAM_SUCCESS; 39 40 /* Initialize PAM. */ 41 pamc.conv = &openpam_ttyconv; 42 pam_start(progname, uname, &pamc, &pamh); 43 44 printf("Checking password for %s.\n", uname); 45 46 /* Authenticate. */ 47 if (PAM_SUCCESS != (retval = pam_authenticate(pamh, 0))) 48 goto pamerr; 49 50 /* Authorize. */ 51 if (PAM_SUCCESS != (retval = pam_acct_mgmt(pamh, 0)) && PAM_NEW_AUTHTOK_REQD != retval) 52 goto pamerr; 53 54 /* Change the password. */ 55 if (PAM_NEW_AUTHTOK_REQD == retval && PAM_SUCCESS != (retval = pam_chauthtok(pamh, 0))) 56 goto pamerr; 57 58 /* Set the credentials. */ 59 if (PAM_SUCCESS != (retval = pam_setcred(pamh, PAM_ESTABLISH_CRED))) 60 goto pamerr; 61 62 /* Open the session. */ 63 if (PAM_SUCCESS != (retval = pam_open_session(pamh, 0))) 64 goto pamerr; 65 66 /* Close the session. */ 67 if (PAM_SUCCESS != (retval = pam_close_session(pamh, 0))) 68 goto pamerr; 69 70pamerr: 71 /* Print an error, if needed. */ 72 if (PAM_SUCCESS != retval) 73 fprintf(stderr, "Sorry\n"); 74 75 /* Terminate PAM. */ 76 pam_end(pamh, retval); 77 return retval; 78} 79