Initial import from FreeBSD RELENG_4:
[games.git] / contrib / libpam / 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: pam_motd.c,v 1.1.1.1 2000/06/20 22:11:46 agmorgan Exp $
8  * $FreeBSD: src/contrib/libpam/modules/pam_motd/pam_motd.c,v 1.1.1.1.2.2 2001/06/11 15:28:20 markm Exp $
9  * 
10  * Written by Michael K. Johnson <johnsonm@redhat.com> 1996/10/24
11  *
12  */
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <pwd.h>
22
23 #include <security/_pam_macros.h>
24 /*
25  * here, we make a definition for the externally accessible function
26  * in this file (this definition is required for static a module
27  * but strongly encouraged generally) it is used to instruct the
28  * modules include file to define the function prototypes.
29  */
30
31 #define PAM_SM_SESSION
32 #define DEFAULT_MOTD    "/etc/motd"
33
34 #include <security/pam_modules.h>
35
36 /* --- session management functions (only) --- */
37
38 PAM_EXTERN
39 int pam_sm_close_session(pam_handle_t *pamh, int flags, int argc,
40                         const char **argv)
41 {
42      return PAM_IGNORE;
43 }
44
45 PAM_EXTERN
46 int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc,
47                    const char **argv)
48 {
49      int retval = PAM_IGNORE;
50      int fd;
51      char *mtmp=NULL, *motd_path=NULL;
52      struct pam_conv *conversation;
53      struct pam_message message;
54      struct pam_message *pmessage = &message;
55      struct pam_response *resp = NULL;
56      struct stat st;
57
58      if (flags & PAM_SILENT) {
59         return retval;
60      }
61
62     for (; argc-- > 0; ++argv) {
63         if (!strncmp(*argv,"motd=",5)) {
64             motd_path = (char *) strdup(5+*argv);
65             if (motd_path != NULL) {
66                 D(("set motd path: %s", motd_path));
67             } else {
68                 D(("failed to duplicate motd path - ignored"));
69             }
70         }
71      }
72
73      if (motd_path == NULL)
74         motd_path = DEFAULT_MOTD;
75
76      message.msg_style = PAM_TEXT_INFO;
77
78      if ((fd = open(motd_path, O_RDONLY, 0)) >= 0) {
79        /* fill in message buffer with contents of motd */
80        if ((fstat(fd, &st) < 0) || !st.st_size)
81          return retval;
82        message.msg = mtmp = malloc(st.st_size+1);
83        /* if malloc failed... */
84        if (!message.msg) return retval;
85        read(fd, mtmp, st.st_size);
86        if (mtmp[st.st_size-1] == '\n')
87           mtmp[st.st_size-1] = '\0';
88        else
89           mtmp[st.st_size] = '\0';
90        close(fd);
91        /* Use conversation function to give user contents of motd */
92        pam_get_item(pamh, PAM_CONV, (const void **)&conversation);
93        conversation->conv(1, (const struct pam_message **)&pmessage,
94                           &resp, conversation->appdata_ptr);
95        free(mtmp);
96        if (resp)
97            _pam_drop_reply(resp, 1);
98      }
99
100      return retval;
101 }
102
103
104 #ifdef PAM_STATIC
105
106 /* static module data */
107
108 struct pam_module _pam_motd_modstruct = {
109      "pam_motd",
110      NULL,
111      NULL,
112      NULL,
113      pam_sm_open_session,
114      pam_sm_close_session,
115      NULL,
116 };
117
118 #endif
119
120 /* end of module definition */