]> granicus.if.org Git - linux-pam/blob - modules/pam_filter/upperLOWER/upperLOWER.c
5aa0e2c084bce408af3c1917034863aec0ee77b9
[linux-pam] / modules / pam_filter / upperLOWER / upperLOWER.c
1 /*
2  * $Id$
3  *
4  * This is a sample filter program, for use with pam_filter (a module
5  * provided with Linux-PAM). This filter simply transposes upper and
6  * lower case letters, it is intended for demonstration purposes and
7  * it serves no purpose other than to annoy the user...
8  */
9
10 #include "config.h"
11
12 #ifdef MEMORY_DEBUG
13 # undef exit
14 #endif /* MEMORY_DEBUG */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <syslog.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22
23 #include <security/pam_filter.h>
24 #include <security/pam_modules.h>
25 #include <security/_pam_macros.h>
26 #include <security/_pam_modutil.h>
27
28 /* ---------------------------------------------------------------- */
29
30 #include <stdarg.h>
31 #ifdef hpux
32 # define log_this syslog
33 #else
34 static void log_this(int err, const char *format, ...)
35 {
36     va_list args;
37
38     va_start(args, format);
39     openlog("upperLOWER", LOG_CONS|LOG_PID, LOG_AUTH);
40     vsyslog(err, format, args);
41     va_end(args);
42     closelog();
43 }
44 #endif
45
46 #include <ctype.h>
47
48 static void do_transpose(char *buffer,int len)
49 {
50      int i;
51      for (i=0; i<len; ++i) {
52           if (islower(buffer[i])) {
53                buffer[i] = toupper(buffer[i]);
54           } else {
55                buffer[i] = tolower(buffer[i]);
56           }
57      }
58 }
59
60 extern char **environ;
61
62 int main(int argc, char **argv) 
63 {
64      char buffer[BUFSIZ];
65      fd_set readers;
66      void (*before_user)(char *,int);
67      void (*before_app)(char *,int);
68
69 #ifdef DEBUG
70      {
71           int i;
72
73           fprintf(stderr,"environment :[\r\n");
74           for (i=0; environ[i]; ++i) {
75                fprintf(stderr,"-> %s\r\n",environ[i]);
76           }
77           fprintf(stderr,"]: end\r\n");
78      }
79 #endif
80
81      if (argc != 1) {
82 #ifdef DEBUG
83           fprintf(stderr,"filter invoked as conventional executable\n");
84 #else
85           log_this(LOG_ERR, "filter invoked as conventional executable");
86 #endif
87           exit(1);
88      }
89
90      before_user = before_app = do_transpose;   /* assign filter functions */
91
92      /* enter a loop that deals with the input and output of the
93         user.. passing it to and from the application */
94
95      FD_ZERO(&readers);                    /* initialize reading mask */
96
97      for (;;) {
98
99           FD_SET(APPOUT_FILENO, &readers);              /* wake for output */
100           FD_SET(APPERR_FILENO, &readers);               /* wake for error */
101           FD_SET(STDIN_FILENO, &readers);                /* wake for input */
102
103           if ( select(APPTOP_FILE,&readers,NULL,NULL,NULL) < 0 ) {
104 #ifdef DEBUG
105                fprintf(stderr,"select failed\n");
106 #else
107                log_this(LOG_WARNING,"select failed");
108 #endif
109                break;
110           }
111
112           /* application errors */
113
114           if ( FD_ISSET(APPERR_FILENO,&readers) ) {
115                int got = _pammodutil_read(APPERR_FILENO, buffer, BUFSIZ);
116                if (got <= 0) {
117                     break;
118                } else {
119                     /* translate to give to real terminal */
120                     if (before_user != NULL)
121                          before_user(buffer, got);
122                     if (_pammodutil_write(STDERR_FILENO, buffer, got) != got ) {
123                          log_this(LOG_WARNING,"couldn't write %d bytes?!",got);
124                          break;
125                     }
126                }
127           } else if ( FD_ISSET(APPOUT_FILENO,&readers) ) {    /* app output */
128                int got = _pammodutil_read(APPOUT_FILENO, buffer, BUFSIZ);
129                if (got <= 0) {
130                     break;
131                } else {
132                     /* translate to give to real terminal */
133                     if (before_user != NULL)
134                          before_user(buffer, got);
135                     if (_pammodutil_write(STDOUT_FILENO, buffer, got) != got ) {
136                          log_this(LOG_WARNING,"couldn't write %d bytes!?",got);
137                          break;
138                     }
139                }
140           }
141
142           if ( FD_ISSET(STDIN_FILENO, &readers) ) {  /* user input */
143                int got = _pammodutil_read(STDIN_FILENO, buffer, BUFSIZ);
144                if (got < 0) {
145                     log_this(LOG_WARNING,"user input junked");
146                     break;
147                } else if (got) {
148                     /* translate to give to application */
149                     if (before_app != NULL)
150                          before_app(buffer, got);
151                     if (_pammodutil_write(APPIN_FILENO, buffer, got) != got ) {
152                          log_this(LOG_WARNING,"couldn't pass %d bytes!?",got);
153                          break;
154                     }
155                } else {
156                     /* nothing received -- an error? */
157                     log_this(LOG_WARNING,"user input null?");
158                     break;
159                }
160           }
161      }
162
163      exit(0);
164 }
165
166
167