Import Linux-PAM.
[profile/ivi/pam.git] / modules / pam_motd / pam_motd.c
1 /* pam_motd module */
2
3 /*
4  * Modified for pam_motd by Ben Collins <bcollins@debian.org>
5  *
6  * Based off of:
7  * $Id$
8  *
9  * Written by Michael K. Johnson <johnsonm@redhat.com> 1996/10/24
10  *
11  */
12
13 #include "config.h"
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <pwd.h>
23 #include <syslog.h>
24
25 #include <security/_pam_macros.h>
26 #include <security/pam_ext.h>
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_SESSION
35 #define DEFAULT_MOTD    "/etc/motd"
36
37 #include <security/pam_modules.h>
38 #include <security/pam_modutil.h>
39
40 /* --- session management functions (only) --- */
41
42 PAM_EXTERN int
43 pam_sm_close_session (pam_handle_t *pamh UNUSED, int flags UNUSED,
44                       int argc UNUSED, const char **argv UNUSED)
45 {
46      return PAM_IGNORE;
47 }
48
49 static char default_motd[] = DEFAULT_MOTD;
50
51 PAM_EXTERN
52 int pam_sm_open_session(pam_handle_t *pamh, int flags,
53                         int argc, const char **argv)
54 {
55     int retval = PAM_IGNORE;
56     int fd;
57     const char *motd_path = NULL;
58     char *mtmp = NULL;
59
60     if (flags & PAM_SILENT) {
61         return retval;
62     }
63
64     for (; argc-- > 0; ++argv) {
65         if (!strncmp(*argv,"motd=",5)) {
66
67             motd_path = 5 + *argv;
68             if (*motd_path != '\0') {
69                 D(("set motd path: %s", motd_path));
70             } else {
71                 motd_path = NULL;
72                 pam_syslog(pamh, LOG_ERR,
73                            "motd= specification missing argument - ignored");
74             }
75         }
76         else
77             pam_syslog(pamh, LOG_ERR, "unknown option: %s", *argv);
78     }
79
80     if (motd_path == NULL)
81         motd_path = default_motd;
82
83     while ((fd = open(motd_path, O_RDONLY, 0)) >= 0) {
84         struct stat st;
85
86         /* fill in message buffer with contents of motd */
87         if ((fstat(fd, &st) < 0) || !st.st_size || st.st_size > 0x10000)
88             break;
89
90         if (!(mtmp = malloc(st.st_size+1)))
91             break;
92
93         if (pam_modutil_read(fd, mtmp, st.st_size) != st.st_size)
94             break;
95
96         if (mtmp[st.st_size-1] == '\n')
97             mtmp[st.st_size-1] = '\0';
98         else
99             mtmp[st.st_size] = '\0';
100
101         pam_info (pamh, "%s", mtmp);
102         break;
103     }
104
105     _pam_drop (mtmp);
106
107     if (fd >= 0)
108         close(fd);
109
110      return retval;
111 }
112
113
114 #ifdef PAM_STATIC
115
116 /* static module data */
117
118 struct pam_module _pam_motd_modstruct = {
119      "pam_motd",
120      NULL,
121      NULL,
122      NULL,
123      pam_sm_open_session,
124      pam_sm_close_session,
125      NULL,
126 };
127
128 #endif
129
130 /* end of module definition */