2 * main.c: for rcpt500 (X.500 email query responder)
4 * 16 June 1992 by Mark C Smith
5 * Copyright (c) 1992 The Regents of The University of Michigan
15 #include <ac/syslog.h>
16 #include <ac/string.h>
19 #include "ldapconfig.h"
23 #ifdef LDAP_CONNECTIONLESS
25 #endif /* LDAP_CONNECTIONLESS */
28 int sizelimit = RCPT500_SIZELIMIT;
29 int rdncount = RCPT500_RDNCOUNT;
31 char *ldaphost = LDAPHOST;
32 char *searchbase = RCPT500_BASE;
33 char *dapuser = RCPT500_BINDDN;
34 char *filterfile = FILTERFILE;
35 char *templatefile = TEMPLATEFILE;
36 char reply[ MAXSIZE * RCPT500_LISTLIMIT ];
48 * main is invoked by sendmail via the alias file
49 * the entire incoming message gets piped to our standard input
55 char *prog, *usage = "%s [-l] [-U] [-h ldaphost] [-p ldapport] [-b searchbase] [-a] [-z sizelimit] [-u dapuser] [-f filterfile] [-t templatefile] [-c rdncount]\n";
65 if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
66 prog = strdup( argv[ 0 ] );
68 prog = strdup( prog + 1 );
72 while (( c = getopt( argc, argv, "alUh:b:s:z:f:t:p:c:" )) != EOF ) {
81 #ifdef LDAP_CONNECTIONLESS
83 #else /* LDAP_CONNECTIONLESS */
85 "Compile with -DLDAP_CONNECTIONLESS for -U support\n" );
86 #endif /* LDAP_CONNECTIONLESS */
95 ldapport = atoi( optarg );
98 sizelimit = atoi( optarg );
107 templatefile = optarg;
110 rdncount = atoi( optarg );
116 if ( errflg || optind < argc ) {
117 fprintf( stderr, usage, prog );
123 * if syslogging requested, initialize
126 openlog( prog, OPENLOG_OPTIONS, LOG_DAEMON );
128 openlog( prog, OPENLOG_OPTIONS );
132 if ( read_msg( stdin, &msg ) < 0 ) {
134 syslog( LOG_INFO, "unparseable message ignored" );
136 exit( 0 ); /* so as not to give sendmail an error */
140 syslog( LOG_INFO, "processing command \"%s %s\" from %s",
141 ( msg.msg_command < 0 ) ? "Unknown" :
142 cmds[ msg.msg_command ].cmd_text,
143 ( msg.msg_arg == NULL ) ? "" : msg.msg_arg, msg.msg_replyto );
146 if ( msg.msg_command < 0 ) {
147 msg.msg_command = 0; /* unknown command == help command */
151 sprintf( reply, "Your request was interpreted as: %s %s\n\n",
152 cmds[ msg.msg_command ].cmd_text, msg.msg_arg );
155 (*cmds[ msg.msg_command ].cmd_handler)( &msg, reply );
157 if ( send_reply( &msg, reply ) < 0 ) {
159 syslog( LOG_INFO, "reply failed: %m" );
161 exit( 0 ); /* so as not to give sendmail an error */
165 syslog( LOG_INFO, "reply OK" );
175 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( fp, offset, buf, MAXSIZEe, linep )
245 for ( hdr = buf + offset; isspace( *hdr ); ++hdr ) {
248 if (( hdr = strdup( hdr )) == NULL ) {
250 syslog( LOG_ERR, "strdup: %m" );
256 *linep = fgets( buf, MAXSIZE, fp );
257 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
258 if ( *linep == NULL || !isspace( **linep )) {
261 if (( hdr = realloc( hdr, strlen( hdr ) +
262 strlen( *linep ) + 3 )) == NULL) {
264 syslog( LOG_ERR, "realloc: %m" );
269 strcat( hdr, *linep );
277 send_reply( msgp, body )
278 struct msginfo *msgp;
285 if (( cmdpipe = popen( RCPT500_PIPEMAILCMD, "w" )) == NULL ) {
287 syslog( LOG_ERR, "popen pipemailcmd failed: %m" );
296 sprintf( buf, "From: %s\n", RCPT500_FROM );
297 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
300 if ( msgp->msg_subject != NULL ) {
301 sprintf( buf, "Subject: Re: %s\n", msgp->msg_subject );
303 sprintf( buf, "Subject: query response\n" );
305 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
308 if ( rc == 1 && msgp->msg_date != NULL ) {
310 * add "In-reply-to:" header
312 if ( msgp->msg_messageid == NULL ) {
313 sprintf( buf, "In-reply-to: Your message of \"%s\"\n",
317 "In-reply-to: Your message of \"%s\"\n %s\n",
318 msgp->msg_date, msgp->msg_messageid );
320 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
324 sprintf( buf, "To: %s\n", msgp->msg_replyto );
325 rc = fwrite( buf, strlen( buf ), 1, cmdpipe );
329 * send the header/body separator (blank line)
332 rc = fwrite( "\n", 1, 1, cmdpipe );
339 rc = fwrite( body, strlen( body ), 1, cmdpipe );
342 if ( rc != 1 && dosyslog ) {
343 syslog( LOG_ERR, "write to binmail failed: %m" );
346 if ( pclose( cmdpipe ) < 0 ) {
348 syslog( LOG_ERR, "pclose binmail failed: %m" );
353 return( rc == 1 ? 0 : -1 );
358 find_command( text, argp )
364 static char argbuf[ MAXSIZE ];
367 for ( s = argbuf; *p != '\0'; ++p ) {
368 *s++ = tolower( *p );
372 for ( i = 0; cmds[ i ].cmd_text != NULL; ++i ) {
373 if (( s = strstr( argbuf, cmds[ i ].cmd_text )) != NULL
374 && isspace( *(s + strlen( cmds[ i ].cmd_text )))) {
375 strcpy( argbuf, text + (s - argbuf) + strlen( cmds[ i ].cmd_text ));
377 while ( isspace( **argp )) {