Import Linux-PAM.
[profile/ivi/pam.git] / modules / pam_rootok / pam_rootok.c
1 /* pam_rootok module */
2
3 /*
4  * $Id$
5  *
6  * Written by Andrew Morgan <morgan@linux.kernel.org> 1996/3/11
7  */
8
9 #include "config.h"
10
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <syslog.h>
14 #include <stdarg.h>
15 #include <string.h>
16
17 /*
18  * here, we make a definition for the externally accessible function
19  * in this file (this definition is required for static a module
20  * but strongly encouraged generally) it is used to instruct the
21  * modules include file to define the function prototypes.
22  */
23
24 #define PAM_SM_AUTH
25
26 #include <security/pam_modules.h>
27 #include <security/pam_ext.h>
28
29 #ifdef WITH_SELINUX
30 #include <selinux/selinux.h>
31 #include <selinux/av_permissions.h>
32 #endif
33
34 /* argument parsing */
35
36 #define PAM_DEBUG_ARG       01
37
38 static int
39 _pam_parse (const pam_handle_t *pamh, int argc, const char **argv)
40 {
41     int ctrl=0;
42
43     /* step through arguments */
44     for (ctrl=0; argc-- > 0; ++argv) {
45
46         /* generic options */
47
48         if (!strcmp(*argv,"debug"))
49             ctrl |= PAM_DEBUG_ARG;
50         else {
51             pam_syslog(pamh, LOG_ERR, "unknown option: %s", *argv);
52         }
53     }
54
55     return ctrl;
56 }
57
58 static int
59 check_for_root (pam_handle_t *pamh, int ctrl)
60 {
61     int retval = PAM_AUTH_ERR;
62
63     if (getuid() == 0)
64 #ifdef WITH_SELINUX
65       if (is_selinux_enabled()<1 || checkPasswdAccess(PASSWD__ROOTOK)==0)
66 #endif
67         retval = PAM_SUCCESS;
68
69     if (ctrl & PAM_DEBUG_ARG) {
70        pam_syslog(pamh, LOG_DEBUG, "root check %s",
71                   (retval==PAM_SUCCESS) ? "succeeded" : "failed");
72     }
73
74     return retval;
75 }
76
77 /* --- management functions --- */
78
79 PAM_EXTERN int
80 pam_sm_authenticate (pam_handle_t *pamh, int flags UNUSED,
81                      int argc, const char **argv)
82 {
83     int ctrl;
84
85     ctrl = _pam_parse(pamh, argc, argv);
86
87     return check_for_root (pamh, ctrl);
88 }
89
90 PAM_EXTERN int
91 pam_sm_setcred (pam_handle_t *pamh UNUSED, int flags UNUSED,
92                 int argc UNUSED, const char **argv UNUSED)
93 {
94     return PAM_SUCCESS;
95 }
96
97 PAM_EXTERN int
98 pam_sm_acct_mgmt (pam_handle_t *pamh, int flags UNUSED,
99                   int argc, const char **argv)
100 {
101     int ctrl;
102
103     ctrl = _pam_parse(pamh, argc, argv);
104
105     return check_for_root (pamh, ctrl);
106 }
107
108 PAM_EXTERN int
109 pam_sm_chauthtok (pam_handle_t *pamh, int flags UNUSED,
110                   int argc, const char **argv)
111 {
112     int ctrl;
113
114     ctrl = _pam_parse(pamh, argc, argv);
115
116     return check_for_root (pamh, ctrl);
117 }
118
119 #ifdef PAM_STATIC
120
121 /* static module data */
122
123 struct pam_module _pam_rootok_modstruct = {
124     "pam_rootok",
125     pam_sm_authenticate,
126     pam_sm_setcred,
127     pam_sm_acct_mgmt,
128     NULL,
129     NULL,
130     pam_sm_chauthtok,
131 };
132
133 #endif
134
135 /* end of module definition */