]> git.sur5r.net Git - openldap/blob - clients/rcpt500/main.c
346c5fc10d3e4e22a8ddd5885d92c93cf9564de3
[openldap] / clients / rcpt500 / main.c
1 /*
2  * main.c: for rcpt500 (X.500 email query responder)
3  *
4  * 16 June 1992 by Mark C Smith
5  * Copyright (c) 1992 The Regents of The University of Michigan
6  * All Rights Reserved
7  */
8
9 #include "portable.h"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #include <ac/ctype.h>
15 #include <ac/string.h>
16 #include <ac/syslog.h>
17 #include <ac/unistd.h>
18
19 #include "ldapconfig.h"
20 #include "rcpt500.h"
21
22 int dosyslog = 0;
23 #ifdef LDAP_CONNECTIONLESS
24 int do_cldap = 0;
25 #endif /* LDAP_CONNECTIONLESS */
26
27 int derefaliases = 1;
28 int sizelimit = RCPT500_SIZELIMIT;
29 int rdncount = RCPT500_RDNCOUNT;
30 int ldapport = 0;
31 char *ldaphost = NULL;
32 char *searchbase = NULL;
33 char *dapuser = NULL;
34 char *filterfile = FILTERFILE;
35 char *templatefile = TEMPLATEFILE;
36 static char reply[ MAXSIZE * RCPT500_LISTLIMIT ];
37
38
39 /*
40  * functions
41  */
42 static int  read_msg(FILE *fp, struct msginfo *msgp);
43 static char *read_hdr(FILE *fp, int off, char *buf, int MAXSIZEe, char **ln_p);
44 static int  send_reply(struct msginfo *msgp, char *body);
45 static int  find_command(char *text, char **argp);
46
47 /*
48  * main is invoked by sendmail via the alias file
49  * the entire incoming message gets piped to our standard input
50  */
51 int
52 main( int argc, char **argv )
53 {
54     char                *prog, *usage = "%s [-l] [-U] [-h ldaphost] [-p ldapport] [-b searchbase] [-a] [-z sizelimit] [-u dapuser] [-f filterfile] [-t templatefile] [-c rdncount]\n";
55     struct msginfo      msg;
56     int                 c, errflg;
57     char                *replytext;
58
59     *reply = '\0';
60
61     if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
62         prog = strdup( argv[ 0 ] );
63     } else {
64         prog = strdup( prog + 1 );
65     }
66
67     errflg = 0;
68     while (( c = getopt( argc, argv, "alUh:b:s:z:f:t:p:c:" )) != EOF ) {
69         switch( c ) {
70         case 'a':
71             derefaliases = 0;
72             break;
73         case 'l':
74             dosyslog = 1;
75             break;
76         case 'U':
77 #ifdef LDAP_CONNECTIONLESS
78             do_cldap = 1;
79 #else /* LDAP_CONNECTIONLESS */
80             fprintf( stderr,
81                         "Compile with -DLDAP_CONNECTIONLESS for -U support\n" );
82 #endif /* LDAP_CONNECTIONLESS */
83             break;      
84         case 'b':
85             searchbase = optarg;
86             break;
87         case 'h':
88             ldaphost = optarg;
89             break;
90         case 'p':
91             ldapport = atoi( optarg );
92             break;
93         case 'z':
94             sizelimit = atoi( optarg );
95             break;
96         case 'u':
97             dapuser = optarg;
98             break;
99         case 'f':
100             filterfile = optarg;
101             break;
102         case 't':
103             templatefile = optarg;
104             break;
105         case 'c':
106             rdncount = atoi( optarg );
107             break;
108         default:
109             ++errflg;
110         }
111     }
112     if ( errflg || optind < argc ) {
113         fprintf( stderr, usage, prog );
114         exit( 1 );
115     }
116
117     if ( dosyslog ) {
118         /*
119          * if syslogging requested, initialize
120          */
121 #ifdef LOG_DAEMON
122         openlog( prog, OPENLOG_OPTIONS, LOG_DAEMON );
123 #else
124         openlog( prog, OPENLOG_OPTIONS );
125 #endif
126     }
127
128     if ( read_msg( stdin, &msg ) < 0 ) {
129         if ( dosyslog ) {
130             syslog( LOG_INFO, "unparseable message ignored" );
131         }
132         exit( 0 );      /* so as not to give sendmail an error */
133     }
134
135     if ( dosyslog ) {
136         syslog( LOG_INFO, "processing command \"%s %s\" from %s",
137                 ( msg.msg_command < 0 ) ? "Unknown" :
138                 cmds[ msg.msg_command ].cmd_text,
139                 ( msg.msg_arg == NULL ) ? "" : msg.msg_arg, msg.msg_replyto );
140     }
141
142     if ( msg.msg_command < 0 ) {
143         msg.msg_command = 0;    /* unknown command == help command */
144     }
145
146 /*
147     sprintf( reply, "Your request was interpreted as: %s %s\n\n",
148             cmds[ msg.msg_command ].cmd_text, msg.msg_arg );
149 */
150
151     (*cmds[ msg.msg_command ].cmd_handler)( &msg, reply );
152
153     if ( send_reply( &msg, reply ) < 0 ) {
154         if ( dosyslog ) {
155             syslog( LOG_INFO, "reply failed: %m" );
156         }
157         exit( 0 );      /* so as not to give sendmail an error */
158     }
159
160     if ( dosyslog ) {
161         syslog( LOG_INFO, "reply OK" );
162     }
163
164     exit( 0 );
165 }
166
167
168 static int
169 read_msg( FILE *fp, struct msginfo *msgp )
170 {
171     char        buf[ MAXSIZE ], *line;
172     int         command = -1;
173
174     msgp->msg_replyto = msgp->msg_date = msgp->msg_subject = NULL;
175
176     line = NULL;
177     while( 1 ) {
178         if ( line == NULL ) {
179             if (( line = fgets( buf, MAXSIZE, fp )) == NULL ) {
180                 break;
181             }
182             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
183         }
184
185         if ( *buf == '\0' ) {   /* start of message body */
186             break;
187         }
188         if ( strncasecmp( buf, "Reply-To:", 9 ) == 0 ) {
189             if ( msgp->msg_replyto != NULL ) {
190                 free( msgp->msg_replyto );
191             }
192              msgp->msg_replyto = read_hdr( fp, 9, buf, MAXSIZE, &line );
193         } else if ( strncasecmp( buf, "From:", 5 ) == 0 &&
194                     msgp->msg_replyto == NULL ) {
195              msgp->msg_replyto = read_hdr( fp, 5, buf, MAXSIZE, &line );
196         } else if ( strncasecmp( buf, "Date:", 5 ) == 0 ) {
197              msgp->msg_date = read_hdr( fp, 5, buf, MAXSIZE, &line );
198         } else if ( strncasecmp( buf, "Message-ID:", 5 ) == 0 ) {
199              msgp->msg_messageid = read_hdr( fp, 11, buf, MAXSIZE, &line );
200         } else if ( strncasecmp( buf, "Subject:", 8 ) == 0 ) {
201              if (( msgp->msg_subject =
202                     read_hdr( fp, 8, buf, MAXSIZE, &line )) != NULL ) {
203                 command = find_command( msgp->msg_subject, &msgp->msg_arg );
204             }
205         } else {
206             line = NULL;        /* discard current line */
207         }
208     }
209
210     while ( command < 0 && line != NULL ) {
211         /*
212          * read the body of the message, looking for commands
213          */
214         if (( line = fgets( buf, MAXSIZE, fp )) != NULL ) {
215             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
216             command = find_command( buf, &msgp->msg_arg );
217         }
218     }
219
220     if ( msgp->msg_replyto == NULL ) {
221         return( -1 );
222     }
223
224     msgp->msg_command = command;
225     return( 0 );
226 }
227
228
229 static char *
230 read_hdr( FILE *fp, int offset, char *buf, int MAXSIZEe, char **linep )
231 {
232     char        *hdr;
233
234     for ( hdr = buf + offset; isspace( *hdr ); ++hdr ) {
235         ;
236     }
237     if (( hdr = strdup( hdr )) == NULL ) {
238         if ( dosyslog ) {
239             syslog( LOG_ERR, "strdup: %m" );
240         }
241         exit( 1 );
242     }
243
244     while ( 1 ) {
245         *linep = fgets( buf, MAXSIZE, fp );
246         buf[ strlen( buf ) - 1 ] = '\0';        /* remove trailing newline */
247         if ( *linep == NULL || !isspace( **linep )) {
248             break;
249         }
250         if (( hdr = realloc( hdr, strlen( hdr ) +
251                     strlen( *linep ) + 3 )) == NULL) {
252             if ( dosyslog ) {
253                 syslog( LOG_ERR, "realloc: %m" );
254             }
255             exit( 1 );
256         }
257         strcat( hdr, "\n" );
258         strcat( hdr, *linep );
259     }
260
261     return( hdr );
262 }
263
264
265 static int
266 send_reply( struct msginfo *msgp, char *body )
267 {
268     char        buf[ MAXSIZE ];
269     FILE        *cmdpipe;
270     int         rc;
271     
272     if (( cmdpipe = popen( RCPT500_PIPEMAILCMD, "w" )) == NULL ) {
273         if ( dosyslog ) {
274             syslog( LOG_ERR, "popen pipemailcmd failed: %m" );
275         }
276         return( -1 );
277     }
278
279     /*
280      * send the headers
281      */
282
283     sprintf( buf, "From: %s\n", RCPT500_FROM );
284     rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
285
286     if ( rc == 1 ) {
287         if ( msgp->msg_subject != NULL ) {
288             sprintf( buf, "Subject: Re: %s\n", msgp->msg_subject );
289         } else {
290             sprintf( buf, "Subject: query response\n" );
291         }
292         rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
293     }
294
295     if ( rc == 1 && msgp->msg_date != NULL ) {
296         /*
297          * add "In-reply-to:" header
298          */
299         if ( msgp->msg_messageid == NULL ) {
300             sprintf( buf, "In-reply-to: Your message of \"%s\"\n",
301                     msgp->msg_date );
302         } else {
303             sprintf( buf,
304                     "In-reply-to: Your message of \"%s\"\n             %s\n",
305                     msgp->msg_date, msgp->msg_messageid );
306         }
307         rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
308     }
309
310     if ( rc == 1 ) {
311         sprintf( buf, "To: %s\n", msgp->msg_replyto );
312         rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
313     }
314
315     /*
316      * send the header/body separator (blank line)
317      */
318     if ( rc == 1 ) {
319         rc = fwrite( "\n", 1, 1, cmdpipe );
320     }
321
322     /*
323      * send the body
324      */
325     if ( rc == 1 ) {
326         rc = fwrite( body, strlen( body ), 1, cmdpipe );
327     }
328
329     if ( rc != 1 && dosyslog ) {
330         syslog( LOG_ERR, "write to binmail failed: %m" );
331     }
332
333     if ( pclose( cmdpipe ) < 0 ) {
334         if ( dosyslog ) {
335             syslog( LOG_ERR, "pclose binmail failed: %m" );
336         }
337         return( -1 );
338     }
339
340     return( rc == 1 ? 0 : -1 );
341 }
342
343
344 static int
345 find_command( char *text, char **argp )
346 {
347     int         i;
348     char        *s, *p;
349     static char argbuf[ MAXSIZE ];
350
351     p = text;
352     for ( s = argbuf; *p != '\0'; ++p ) {
353         *s++ = TOLOWER( *p );
354     }
355     *s = '\0';
356
357     for ( i = 0; cmds[ i ].cmd_text != NULL; ++i ) {
358         if (( s = strstr( argbuf, cmds[ i ].cmd_text )) != NULL
359                     && isspace( *(s + strlen( cmds[ i ].cmd_text )))) {
360             strcpy( argbuf, text + (s - argbuf) + strlen( cmds[ i ].cmd_text ));
361             *argp = argbuf;
362             while ( isspace( **argp )) {
363                 ++(*argp);
364             }
365             return( i );
366         }
367     }
368
369     return( -1 );
370 }