2004/Dec/28

#include <security/pam_appl.h>
#include <security/pam_misc.h>
#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/types.h>

static struct pam_conv conv = {
misc_conv,
NULL
};
int get_file(char *name,char **image)
{
FILE *file;
char *buffer;
long Lsize;
file = fopen(name,"rb");
if(file==NULL)
exit(1);
fseek(file,0,SEEK_END);
Lsize = ftell(file);
printf("Lsize = %d\n",Lsize);
rewind(file);
buffer = (char*)malloc(Lsize);
if(buffer == NULL)
exit(2);
fread(buffer,1,Lsize,file);
*image = buffer;
fclose(file);
return 0;
}
int main(int argc, char *argv[])
{
pam_handle_t *pamh=NULL;
int retval;
const char *uid = "nobody";
const char *ttyn = "tty3";
char device[16] = "";

if(argc == 3) {
uid = argv[1];
ttyn = argv[2];
strcat(device,"/dev/");
strcat(device,ttyn);
}

if(argc != 3) {
fprintf(stderr, "Usage: llfslogin [uid] [ttyn]\n");
exit(1);
}
fprintf(stdout,"uid before = %s\n",uid);
/* pam_start() initialize the pam_handle_t for llfslogin */
retval = pam_start("llfslogin", uid, &conv, &pamh);


/* pam_authenticate() attempt to authenticate the user */
if(retval == PAM_SUCCESS)
retval = pam_authenticate(pamh, 0);

if(retval == PAM_SUCCESS) {
fprintf(stdout, "AUthenticated retval = %d\n",retval);
/* pam_get_time() retrieve the username */

retval = pam_get_item(pamh, PAM_USER, (const void **) &uid);
if(retval != PAM_SUCCESS)
fprintf(stderr," pam get time error \n");

/* getpwuid(3) retrieve group , shell , etc.*/
struct passwd *pw;
int p_uid;
char *dir;
char *uname;
p_uid = atoi (uid);
pw = getpwuid(p_uid);
dir = pw->pw_dir;
uname = pw->pw_name;
/* chdir to home */
if(chdir(dir)==-1)
fprintf(stdout,"change dir fail \n");
/* chown ower and group for terminal device */
if(chown(device,pw->pw_uid,pw->pw_gid)==-1)
fprintf(stdout,"change owner fail \n");
/* set gid */
if(setgid(pw->pw_gid)==-1)
fprintf(stdout,"set gid fail \n");
/*initgroups */
if(initgroups(uname,pw->pw_gid)==-1)
fprintf(stdout,"initgroups fail \n");
/*initialized environment */
setenv("HOME",pw->pw_dir, 1);
setenv("SHELL",pw->pw_shell, 1);
setenv("LOGNAME",pw->pw_name, 1);
/* set uid */
if(setuid(pw->pw_uid)==-1)
fprintf(stdout,"set uid fail \n");

fprintf(stdout,"UID passwd entry: \n name=%s, uid=%d, gid=%d, home=%s, shell=%s\n",pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);
/* pam_end() free pam handle */
if(pam_end(pamh,retval) != PAM_SUCCESS) {
pamh = NULL;
fprintf(stderr, "llfslogin: failed to release authenticator\n");
exit(1);
}


/* fork(2) */

/* execvp(3) launch shell (bash(1) , tcsh(1), etc.) */
char *cmd[] = { "bash", (char *)0 };
execvp("bash",cmd);
return (retval == PAM_SUCCESS ? 0:1 );
}
if(retval == PAM_AUTH_ERR){
/* syslog(3) */
fprintf(stdout, "Not Authenticated retval = %d \n",retval);
sleep(3);
exit(1);
}

}

Comment

Comment:

Tweet


hi all !!
#3 by sears parts (124.157.191.197) At 2009-12-04 07:03,
Que se ra
#2 by scratch and dent (124.157.236.175) At 2009-11-13 12:54,
ขอบคุณค่ะ
#1 by โหลดเพลง (124.157.236.176) At 2009-10-06 14:41,