123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- #include "config.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <security/pam_appl.h>
- #include <security/pam_misc.h>
- #include <pwd.h>
- #include <sys/types.h>
- #include <unistd.h>
- static void bail_out(pam_handle_t *pamh,int really, int code, const char *fn)
- {
- fprintf(stderr,"==> called %s()\n got: `%s'\n", fn,
- pam_strerror(pamh,code));
- if (really && code)
- exit (1);
- }
- static struct pam_conv conv = {
- misc_conv,
- NULL
- };
- int main(int argc, char **argv)
- {
- pam_handle_t *pamh=NULL;
- const void *username=NULL;
- const char *service="xsh";
- int retcode;
-
- if (argc > 3) {
- fprintf(stderr,"usage: %s [username [service-name]]\n",argv[0]);
- }
- if ((argc >= 2) && (argv[1][0] != '-')) {
- username = argv[1];
- }
- if (argc == 3) {
- service = argv[2];
- }
-
- retcode = pam_start(service, username, &conv, &pamh);
- bail_out(pamh,1,retcode,"pam_start");
-
- {
- char buffer[100];
- struct passwd *pw;
- const char *tty;
- pw = getpwuid(getuid());
- if (pw != NULL) {
- retcode = pam_set_item(pamh, PAM_RUSER, pw->pw_name);
- bail_out(pamh,1,retcode,"pam_set_item(PAM_RUSER)");
- }
- retcode = gethostname(buffer, sizeof(buffer)-1);
- if (retcode) {
- perror("failed to look up hostname");
- retcode = pam_end(pamh, PAM_ABORT);
- bail_out(pamh,1,retcode,"pam_end");
- }
- retcode = pam_set_item(pamh, PAM_RHOST, buffer);
- bail_out(pamh,1,retcode,"pam_set_item(PAM_RHOST)");
- tty = ttyname(fileno(stdin));
- if (tty) {
- retcode = pam_set_item(pamh, PAM_TTY, tty);
- bail_out(pamh,1,retcode,"pam_set_item(PAM_RHOST)");
- }
- }
-
- for (;;) {
-
- retcode = pam_authenticate(pamh, 0);
- bail_out(pamh,0,retcode,"pam_authenticate");
-
- if (retcode != PAM_SUCCESS) {
- fprintf(stderr,"%s: invalid request\n",argv[0]);
- break;
- }
-
- retcode = pam_acct_mgmt(pamh, 0);
- bail_out(pamh,0,retcode,"pam_acct_mgmt");
- if (retcode == PAM_NEW_AUTHTOK_REQD) {
- fprintf(stderr,"Application must request new password...\n");
- retcode = pam_chauthtok(pamh,PAM_CHANGE_EXPIRED_AUTHTOK);
- bail_out(pamh,0,retcode,"pam_chauthtok");
- }
- if (retcode != PAM_SUCCESS) {
- fprintf(stderr,"%s: invalid request\n",argv[0]);
- break;
- }
-
- retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED);
- bail_out(pamh,0,retcode,"pam_setcred");
- if (retcode != PAM_SUCCESS) {
- fprintf(stderr,"%s: problem setting user credentials\n"
- ,argv[0]);
- break;
- }
-
- retcode = pam_open_session(pamh,0);
- bail_out(pamh,0,retcode,"pam_open_session");
- if (retcode != PAM_SUCCESS) {
- fprintf(stderr,"%s: problem opening a session\n",argv[0]);
- break;
- }
- pam_get_item(pamh, PAM_USER, &username);
- fprintf(stderr,
- "The user [%s] has been authenticated and `logged in'\n",
- (const char *)username);
-
- retcode = system("/bin/sh");
-
- retcode = pam_close_session(pamh,0);
- bail_out(pamh,0,retcode,"pam_close_session");
- if (retcode != PAM_SUCCESS) {
- fprintf(stderr,"%s: problem closing a session\n",argv[0]);
- break;
- }
-
- retcode = pam_setcred(pamh, PAM_DELETE_CRED);
- bail_out(pamh,0,retcode,"pam_setcred");
- if (retcode != PAM_SUCCESS) {
- fprintf(stderr,"%s: problem deleting user credentials\n"
- ,argv[0]);
- break;
- }
- break;
- }
-
- retcode = pam_end(pamh, PAM_SUCCESS);
- pamh = NULL;
- bail_out(pamh,1,retcode,"pam_end");
- return (0);
- }
|