~ubuntu-branches/ubuntu/breezy/pam/breezy

« back to all changes in this revision

Viewing changes to Linux-PAM/modules/pam_securetty/pam_securetty.c

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hartman
  • Date: 2004-06-28 14:28:08 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040628142808-adikk7vtfg3pzcjw
Tags: 0.76-22
* Add uploaders
* Document location of repository
* Fix options containing arguments in pam_unix, Closes: #254904

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* pam_securetty module */
 
2
 
 
3
#define SECURETTY_FILE "/etc/securetty"
 
4
#define TTY_PREFIX     "/dev/"
 
5
 
 
6
/*
 
7
 * by Elliot Lee <sopwith@redhat.com>, Red Hat Software.
 
8
 * July 25, 1996.
 
9
 * This code shamelessly ripped from the pam_rootok module.
 
10
 * Slight modifications AGM. 1996/12/3
 
11
 */
 
12
 
 
13
#define _GNU_SOURCE
 
14
 
 
15
#include <stdio.h>
 
16
#include <stdlib.h>
 
17
#include <sys/types.h>
 
18
#include <sys/stat.h>
 
19
#include <unistd.h>
 
20
#include <syslog.h>
 
21
#include <stdarg.h>
 
22
#include <pwd.h>
 
23
#include <string.h>
 
24
 
 
25
#define PAM_SM_AUTH
 
26
 
 
27
/*
 
28
 * here, we make a definition for the externally accessible function
 
29
 * in this file (this definition is required for static a module
 
30
 * but strongly encouraged generally) it is used to instruct the
 
31
 * modules include file to define the function prototypes.
 
32
 */
 
33
 
 
34
#define PAM_SM_AUTH
 
35
 
 
36
#include <security/pam_modules.h>
 
37
#include <security/_pam_modutil.h>
 
38
 
 
39
/* some syslogging */
 
40
 
 
41
static void _pam_log(int err, const char *format, ...)
 
42
{
 
43
    va_list args;
 
44
 
 
45
    va_start(args, format);
 
46
    openlog("PAM-securetty", LOG_CONS|LOG_PID, LOG_AUTH);
 
47
    vsyslog(err, format, args);
 
48
    va_end(args);
 
49
    closelog();
 
50
}
 
51
 
 
52
/* argument parsing */
 
53
 
 
54
#define PAM_DEBUG_ARG       0x0001
 
55
 
 
56
static int _pam_parse(int argc, const char **argv)
 
57
{
 
58
    int ctrl=0;
 
59
 
 
60
    /* step through arguments */
 
61
    for (ctrl=0; argc-- > 0; ++argv) {
 
62
 
 
63
        /* generic options */
 
64
 
 
65
        if (!strcmp(*argv,"debug"))
 
66
            ctrl |= PAM_DEBUG_ARG;
 
67
        else {
 
68
            _pam_log(LOG_ERR,"pam_parse: unknown option; %s",*argv);
 
69
        }
 
70
    }
 
71
 
 
72
    return ctrl;
 
73
}
 
74
 
 
75
/* --- authentication management functions (only) --- */
 
76
 
 
77
PAM_EXTERN
 
78
int pam_sm_authenticate(pam_handle_t *pamh,int flags,int argc
 
79
                        ,const char **argv)
 
80
{
 
81
    int retval = PAM_AUTH_ERR;
 
82
    const char *username;
 
83
    char *uttyname;
 
84
    struct passwd *user_pwd;
 
85
 
 
86
    int ctrl;
 
87
 
 
88
    /* parse the arguments */
 
89
    ctrl = _pam_parse(argc, argv);
 
90
 
 
91
    retval = pam_get_user(pamh, &username, NULL);
 
92
    if (retval != PAM_SUCCESS || username == NULL) {
 
93
        if (ctrl & PAM_DEBUG_ARG) {
 
94
            _pam_log(LOG_WARNING, "cannot determine username");
 
95
        }
 
96
        return (retval == PAM_CONV_AGAIN
 
97
                ? PAM_INCOMPLETE:PAM_SERVICE_ERR);
 
98
    }
 
99
 
 
100
    retval = pam_get_item(pamh, PAM_TTY, (const void **)&uttyname);
 
101
    if (retval != PAM_SUCCESS || uttyname == NULL) {
 
102
        if (ctrl & PAM_DEBUG_ARG) {
 
103
            _pam_log(LOG_WARNING, "cannot determine user's tty");
 
104
        }
 
105
        return PAM_SERVICE_ERR;
 
106
    }
 
107
 
 
108
    user_pwd = getpwnam(username);
 
109
    if (user_pwd == NULL) {
 
110
        return PAM_IGNORE;
 
111
    } else if (user_pwd->pw_uid != 0) { /* If the user is not root,
 
112
                                           securetty's does not apply
 
113
                                           to them */
 
114
        return PAM_SUCCESS;
 
115
    }
 
116
 
 
117
    retval = _pammodutil_tty_secure( uttyname);
 
118
    if ((retval == PAM_SUCCESS) && (ctrl & PAM_DEBUG_ARG))
 
119
        _pam_log(LOG_DEBUG, "access allowed for '%s' on '%s'",
 
120
                 username, uttyname);
 
121
    return retval;
 
122
}
 
123
 
 
124
PAM_EXTERN
 
125
int pam_sm_setcred(pam_handle_t *pamh,int flags,int argc
 
126
                   ,const char **argv)
 
127
{
 
128
     return PAM_SUCCESS;
 
129
}
 
130
 
 
131
 
 
132
#ifdef PAM_STATIC
 
133
 
 
134
/* static module data */
 
135
 
 
136
struct pam_module _pam_securetty_modstruct = {
 
137
     "pam_securetty",
 
138
     pam_sm_authenticate,
 
139
     pam_sm_setcred,
 
140
     NULL,
 
141
     NULL,
 
142
     NULL,
 
143
     NULL,
 
144
};
 
145
 
 
146
#endif
 
147
 
 
148
/* end of module definition */