00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00036 #include "OW_config.h"
00037 #include <iostream>
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include <string.h>
00041
00042 extern "C"
00043 {
00044 #if defined OW_HAVE_PAM_PAM_APPL_H
00045 #include <pam/pam_appl.h>
00046 #elif defined OW_HAVE_SECURITY_PAM_APPL_H
00047 #include <security/pam_appl.h>
00048 #endif
00049 #if defined OW_HAVE_PAM_PAM_MISC_H
00050 #include <pam/pam_misc.h>
00051 #elif defined OW_HAVE_SECURITY_PAM_MISC_H
00052 #include <security/pam_misc.h>
00053 #endif
00054 }
00055 using std::cin;
00056 using std::endl;
00057
00058 #if !defined(_pam_overwrite)
00059 #define _pam_overwrite(x) \
00060 do { \
00061 register char *__xx__; \
00062 if ((__xx__=(x))) \
00063 { \
00064 while (*__xx__) \
00065 { \
00066 *__xx__++ = '\0'; \
00067 } \
00068 } \
00069 } while (0)
00070
00071 #endif
00072
00073
00075 #if defined(OW_HPUX) || defined(OW_SOLARIS) || defined(OW_AIX)
00076 int
00077 MY_PAM_conv(int num_msg, struct pam_message **msgm, struct pam_response **response, void *appdata_ptr)
00078 #else
00079 int
00080 MY_PAM_conv(int num_msg, const struct pam_message **msgm, struct pam_response **response, void *appdata_ptr)
00081 #endif
00082 {
00083 int count=0;
00084 struct pam_response *reply;
00085 if (num_msg <= 0)
00086 {
00087 return PAM_CONV_ERR;
00088 }
00089
00090 reply = static_cast<struct pam_response *>(calloc(num_msg, sizeof(struct pam_response)));
00091 if (reply == NULL)
00092 {
00093
00094 return PAM_CONV_ERR;
00095 }
00096 bool failed(false);
00097
00098 for (count=0; count < num_msg; ++count)
00099 {
00100 char *string=NULL;
00101 if (failed == true)
00102 {
00103 break;
00104 }
00105 switch (msgm[count]->msg_style)
00106 {
00107 case PAM_PROMPT_ECHO_OFF:
00108 string = reinterpret_cast<char*>(appdata_ptr);
00109 if (string == NULL)
00110 {
00111 failed = true;
00112 }
00113 break;
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 default:
00146 fprintf(stderr, "erroneous conversation (%d)\n"
00147 ,msgm[count]->msg_style);
00148 failed = true;
00149 }
00150 if (string)
00151 {
00152
00153 reply[count].resp_retcode = 0;
00154 reply[count].resp = string;
00155 string = NULL;
00156 }
00157 }
00158
00159
00160 if (!failed)
00161 {
00162 *response = reply;
00163 reply = NULL;
00164 }
00165 else
00166 {
00167 if (reply)
00168 {
00169 for (count=0; count<num_msg; ++count)
00170 {
00171 if (reply[count].resp == NULL)
00172 {
00173 continue;
00174 }
00175 switch (msgm[count]->msg_style)
00176 {
00177
00178 case PAM_PROMPT_ECHO_OFF:
00179 _pam_overwrite(reply[count].resp);
00180 free(reply[count].resp);
00181 break;
00182
00183
00184
00185
00186
00187
00188
00189 }
00190 reply[count].resp = NULL;
00191 }
00192 free(reply);
00193 reply = NULL;
00194 }
00195 return PAM_CONV_ERR;
00196 }
00197 return PAM_SUCCESS;
00198 }
00199
00201 bool
00202 authenticate(const char* userName,
00203 const char* password)
00204 {
00205 char* pPasswd = strdup(password);
00206 char* pUserName = strdup(userName);
00207 struct pam_conv conv = {
00208 MY_PAM_conv,
00209 pPasswd
00210 };
00211 pam_handle_t *pamh=NULL;
00212 int rval;
00213 rval = pam_start(OW_PACKAGE_PREFIX"openwbem", pUserName, &conv, &pamh);
00214 if (rval == PAM_SUCCESS)
00215 {
00216 rval = pam_authenticate(pamh, 0);
00217 }
00218 if (rval == PAM_SUCCESS)
00219 {
00220 rval = pam_acct_mgmt(pamh, 0);
00221 }
00222 if (rval == PAM_CONV_ERR)
00223 {
00224 pam_end(pamh, rval);
00225 free(pUserName);
00226 return false;
00227 }
00228 if (pam_end(pamh,rval) != PAM_SUCCESS)
00229 {
00230 pamh = NULL;
00231 return false;
00232 }
00233 free(pUserName);
00234 return( rval == PAM_SUCCESS ? true : false );
00235 }
00237 int main()
00238 {
00239 char name[80];
00240 char passwd[80];
00241 memset(name, 0, sizeof(name));
00242 memset(passwd, 0, sizeof(passwd));
00243 cin >> name;
00244 cin >> passwd;
00245 bool rval = authenticate(name, passwd);
00246 return (rval == true) ? 0: 1;
00247 }
00248