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