3 * main.c: for rcpt500 (X.500 email query responder)
5 * 16 June 1992 by Mark C Smith
6 * Copyright (c) 1992 The Regents of The University of Michigan
14 #include <ac/stdlib.h>
17 #include <ac/signal.h>
18 #include <ac/string.h>
19 #include <ac/syslog.h>
20 #include <ac/unistd.h>
22 #include "ldap_defaults.h"
26 #ifdef LDAP_CONNECTIONLESS
28 #endif /* LDAP_CONNECTIONLESS */
31 int sizelimit = RCPT500_SIZELIMIT;
32 int rdncount = RCPT500_RDNCOUNT;
34 char *ldaphost = NULL;
35 char *searchbase = NULL;
37 char *filterfile = FILTERFILE;
38 char *templatefile = TEMPLATEFILE;
39 static char reply[ MAXSIZE * RCPT500_LISTLIMIT ];
45 static int read_msg(FILE *fp, struct msginfo *msgp);
46 static char *read_hdr(FILE *fp, int off, char *buf, int MAXSIZEe, char **ln_p);
47 static int send_reply(struct msginfo *msgp, char *body);
48 static int find_command(char *text, char **argp);
51 * main is invoked by sendmail via the alias file
52 * the entire incoming message gets piped to our standard input
55 main( int argc, char **argv )
57 char *prog, *usage = "%s [-l] [-U] [-h ldaphost] [-p ldapport] [-b searchbase] [-a] [-z sizelimit] [-u dapuser] [-f filterfile] [-t templatefile] [-c rdncount]\n";
63 if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
64 prog = strdup( argv[ 0 ] );
66 prog = strdup( prog + 1 );
70 while (( c = getopt( argc, argv, "alUh:b:s:z:f:t:p:c:" )) != EOF ) {
79 #ifdef LDAP_CONNECTIONLESS
81 #else /* LDAP_CONNECTIONLESS */
83 "Compile with -DLDAP_CONNECTIONLESS for -U support\n" );
84 #endif /* LDAP_CONNECTIONLESS */
93 ldapport = atoi( optarg );
96 sizelimit = atoi( optarg );
105 templatefile = optarg;
108 rdncount = atoi( optarg );
114 if ( errflg || optind < argc ) {
115 fprintf( stderr, usage, prog );
116 exit( EXIT_FAILURE );
120 (void) SIGNAL( SIGPIPE, SIG_IGN );
125 * if syslogging requested, initialize
128 openlog( prog, OPENLOG_OPTIONS, LOG_DAEMON );
130 openlog( prog, OPENLOG_OPTIONS );
134 if ( read_msg( stdin, &msg ) < 0 ) {
136 syslog( LOG_INFO, "unparseable message ignored" );
138 exit( 0 ); /* so as not to give sendmail an error */
142 syslog( LOG_INFO, "processing command \"%s %s\" from %s",
143 ( msg.msg_command < 0 ) ? "Unknown" :
144 cmds[ msg.msg_command ].cmd_text,
145 ( msg.msg_arg == NULL ) ? "" : msg.msg_arg, msg.msg_replyto );
148 if ( msg.msg_command < 0 ) {
149 msg.msg_command = 0; /* unknown command == help command */
153 sprintf( reply, "Your request was interpreted as: %s %s\n\n",
154 cmds[ msg.msg_command ].cmd_text, msg.msg_arg );
157 (*cmds[ msg.msg_command ].cmd_handler)( &msg, reply );
159 if ( send_reply( &msg, reply ) < 0 ) {
161 syslog( LOG_INFO, "reply failed: %m" );
163 exit( 0 ); /* so as not to give sendmail an error */
167 syslog( LOG_INFO, "reply OK" );
175 read_msg( FILE *fp, struct msginfo *msgp )
177 char buf[ MAXSIZE ], *line;
180 msgp->msg_replyto = msgp->msg_date = msgp->msg_subject = NULL;
184 if ( line == NULL ) {
185 if (( line = fgets( buf, MAXSIZE, fp )) == NULL ) {
188 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
191 if ( *buf == '\0' ) { /* start of message body */
194 if ( strncasecmp( buf, "Reply-To:", 9 ) == 0 ) {
195 if ( msgp->msg_replyto != NULL ) {
196 free( msgp->msg_replyto );
198 msgp->msg_replyto = read_hdr( fp, 9, buf, MAXSIZE, &line );
199 } else if ( strncasecmp( buf, "From:", 5 ) == 0 &&
200 msgp->msg_replyto == NULL ) {
201 msgp->msg_replyto = read_hdr( fp, 5, buf, MAXSIZE, &line );
202 } else if ( strncasecmp( buf, "Date:", 5 ) == 0 ) {
203 msgp->msg_date = read_hdr( fp, 5, buf, MAXSIZE, &line );
204 } else if ( strncasecmp( buf, "Message-ID:", 5 ) == 0 ) {
205 msgp->msg_messageid = read_hdr( fp, 11, buf, MAXSIZE, &line );
206 } else if ( strncasecmp( buf, "Subject:", 8 ) == 0 ) {
207 if (( msgp->msg_subject =
208 read_hdr( fp, 8, buf, MAXSIZE, &line )) != NULL ) {
209 command = find_command( msgp->msg_subject, &msgp->msg_arg );
212 line = NULL; /* discard current line */
216 while ( command < 0 && line != NULL ) {
218 * read the body of the message, looking for commands
220 if (( line = fgets( buf, MAXSIZE, fp )) != NULL ) {
221 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
222 command = find_command( buf, &msgp->msg_arg );
226 if ( msgp->msg_replyto == NULL ) {
230 msgp->msg_command = command;
236 read_hdr( FILE *fp, int offset, char *buf, int MAXSIZEe, char **linep )
240 for ( hdr = buf + offset; isspace( (unsigned char) *hdr ); ++hdr ) {
243 if (( hdr = strdup( hdr )) == NULL ) {
245 syslog( LOG_ERR, "strdup: %m" );
247 exit( EXIT_FAILURE );
251 *linep = fgets( buf, MAXSIZE, fp );
252 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
253 if ( *linep == NULL || !isspace( (unsigned char) **linep )) {
256 if (( hdr = realloc( hdr, strlen( hdr ) +
257 strlen( *linep ) + 3 )) == NULL) {
259 syslog( LOG_ERR, "realloc: %m" );
261 exit( EXIT_FAILURE );
264 strcat( hdr, *linep );
272 send_reply( struct msginfo *msgp, char *body )
278 if (( cmdpipe = popen( RCPT500_PIPEMAILCMD, "w" )) == NULL ) {
280 syslog( LOG_ERR, "popen pipemailcmd failed: %m" );
289 sprintf( buf, "From: %s\n", RCPT500_FROM );
290 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
293 if ( msgp->msg_subject != NULL ) {
294 sprintf( buf, "Subject: Re: %s\n", msgp->msg_subject );
296 sprintf( buf, "Subject: query response\n" );
298 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
301 if ( rc == 1 && msgp->msg_date != NULL ) {
303 * add "In-reply-to:" header
305 if ( msgp->msg_messageid == NULL ) {
306 sprintf( buf, "In-reply-to: Your message of \"%s\"\n",
310 "In-reply-to: Your message of \"%s\"\n %s\n",
311 msgp->msg_date, msgp->msg_messageid );
313 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
317 sprintf( buf, "To: %s\n", msgp->msg_replyto );
318 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
322 * send the header/body separator (blank line)
325 rc = fwrite( "\n", 1, 1, cmdpipe );
332 rc = fwrite( body, strlen( body ), 1, cmdpipe );
335 if ( rc != 1 && dosyslog ) {
336 syslog( LOG_ERR, "write to binmail failed: %m" );
339 if ( pclose( cmdpipe ) < 0 ) {
341 syslog( LOG_ERR, "pclose binmail failed: %m" );
346 return( rc == 1 ? 0 : -1 );
351 find_command( char *text, char **argp )
355 static char argbuf[ MAXSIZE ];
358 for ( s = argbuf; *p != '\0'; ++p ) {
359 *s++ = TOLOWER( (unsigned char) *p );
363 for ( i = 0; cmds[ i ].cmd_text != NULL; ++i ) {
364 if (( s = strstr( argbuf, cmds[ i ].cmd_text )) != NULL
365 && isspace( (unsigned char) s[ strlen( cmds[ i ].cmd_text ) ] )) {
366 strcpy( argbuf, text + (s - argbuf) + strlen( cmds[ i ].cmd_text ));
368 while ( isspace( (unsigned char) **argp )) {