I have found a lot of information about PAM recently, although there are many, but they are just dynamically interacting verification. Because of the need, static verification is required, so the following code is written. The following is verified on Mac OS X. If #include
#include
#include
INT su_conv (int Num_msg, const struct pam_message ** msgm, // callback function, very important !!!! struct pam_response ** response, void * appdata_ptr);
// static struct PAM_CONV CONV = {Misc_Conv, NULL};
INT Main (int Argc, const char * argv []) {// INSERT code Here ... PAM_HANDLE_T * PAMH; const char * mytty, * p; char * password = "718412"; char * user = "root"; Struct Pam_conv conv = {su_conv, password}; // where password is directly passed to the Su_CONV callback function void * appdata_ptr pointer int RetVal = 100; Printf ("UID:% D / N", getUID ()); retval = PAM_START "SU", "root", & conv, & piers; if (retval = pam_success) // PAM_GET_ITEM (Pamh, Pam_user, (const void **) & p); PAM_SET_ITEM (Pamh, Pam_Conv, getLogin ());
Mytty = TTYNAME (stderr_fileno); if (! mytty) mytty = "tty"; PAM_SET_ITEM (Pamh, Pam_tty, Mytty);
Retval = PAM_AUTHENTICATE (PAMH, PAM_SILENT); Printf ("Return Code:% D / N", RetVal); if (RetVal! = PAM_SUCCESS) {Printf ("Invalid Password / N"); Return-1;} RetVal = PAM_END (PAMH, PAM_SUCCESS); Printf ("UID:% D / N", getuid ()); setuid (0); Printf ("UID:% D / N", getuid ()); Printf ("THE RETVAL IS NOW % D ", RETVAL); RETURN 0;} INT SU_CONV (INT NUM_MSG, Const Struct Pam_MESSAGE ** MSGM, STRUCT PAM_RESPONSE ** RESPONSE, VOID * AppData_ptr) {
char * rec; struct pam_message * m = (struct pam_message *) * msgm; struct pam_response * r; while (num_msg--) {switch (m-> msg_style) {case PAM_PROMPT_ECHO_OFF: // r-> resp = strdup (getpass (m-> msg)); r = (Struct Pam_RESPONSE *) Malloc (Struct Pam_RESPONSE)); r-> Resp = (char *) malloc (100); r-> resp_retcode = 0; strcpy (r-> Resp, appdata_ptr); * response = r; break; case pam_prompt_echo_on: // r-> resp = (char *) getPass (M-> msg); // fprintf (stdout, "% s", m-> msg) ; R-> resp = malloc (PAM_MAX_RESP_SIZE); FGETS (r-> Resp, PAM_MAX_RESP_SIZE, STDIN); // Gets (REC); Break; Case Pam_ERROR_MSG: (Void) FPUTS (M-> MSG, STDOUT); BREAK; Case Pam_Text_info: fprintf (stdout, "% s / n", m-> msg); Break; default: Break;}} Return Pam_suCcess;}