forked from simta/simta
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimc.c
More file actions
218 lines (182 loc) · 4.4 KB
/
simc.c
File metadata and controls
218 lines (182 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* RFC's of interest:
*
* RFC 822 "Standard for the format of ARPA Internet text messages"
* RFC 1123 "Requirements for Internet Hosts -- Application and Support"
* RFC 2476 "Message Submission"
* RFC 2822 "Internet Message Format"
*
*/
#include "config.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <sys/stat.h>
#ifdef HAVE_LIBSSL
#include <openssl/ssl.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#endif /* HAVE_LIBSSL */
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <pwd.h>
#include <fcntl.h>
#include <signal.h>
#include <sysexits.h>
#include <syslog.h>
#include <dirent.h>
#ifdef HAVE_LIBSASL
#include <sasl/sasl.h>
#endif /* HAVE_LIBSASL */
#include <snet.h>
#include "denser.h"
#include "line_file.h"
#include "envelope.h"
#include "header.h"
#include "simta.h"
#include "queue.h"
int
main( int argc, char *argv[] )
{
int usage = 0;
int c;
int pidfd;
int server_pid;
int pid;
FILE *pf;
struct timeval tv_now;
int fd;
FILE *tff;
char tf[ MAXPATHLEN + 1 ];
char cf[ MAXPATHLEN + 1 ];
char *command = NULL;
char *arg1 = NULL;
opterr = 0;
simta_openlog( 0 );
while (( c = getopt( argc, argv, "dmqs" )) != -1 ) {
switch ( c ) {
default:
usage++;
break;
case 'd':
if ( command != NULL ) {
usage++;
}
command = S_DEBUG;
break;
case 'm':
if ( command != NULL ) {
usage++;
}
command = S_MESSAGE;
break;
case 'q':
if ( command != NULL ) {
usage++;
}
command = S_QUEUE;
break;
case 's':
if ( command != NULL ) {
usage++;
}
command = S_SENDER;
break;
}
}
if ( argc - optind == 0 ) {
arg1 = NULL;
} else if ( argc - optind == 1 ) {
arg1 = argv[ optind ];
} else {
usage++;
}
if (( usage != 0 ) || ( command == NULL )) {
fprintf( stderr, "Usage: %s -d | -m | -s | -q [ arg ] \n", argv[ 0 ] );
exit( EX_USAGE );
}
if ( simta_read_config( SIMTA_FILE_CONFIG ) < 0 ) {
exit( EX_TEMPFAIL );
}
/* init simta config / defaults */
if ( simta_config( SIMTA_BASE_DIR ) != 0 ) {
exit( EX_TEMPFAIL );
}
if (( pid = getpid()) < 0 ) {
syslog( LOG_ERR, "env_id getpid: %m" );
perror( "getpid" );
exit( EX_TEMPFAIL );
}
if ( simta_gettimeofday( &tv_now ) != 0 ) {
perror( "gettimeofday" );
exit( EX_TEMPFAIL );
}
snprintf( tf, MAXPATHLEN, "%s/c%lX.%lX.%d", simta_dir_command,
(unsigned long)tv_now.tv_sec, (unsigned long)tv_now.tv_usec, pid );
snprintf( cf, MAXPATHLEN, "%s/C%lX.%lX.%d", simta_dir_command,
(unsigned long)tv_now.tv_sec, (unsigned long)tv_now.tv_usec, pid );
/* make tfile */
if (( fd = open( tf, O_WRONLY | O_CREAT | O_EXCL, 0600 )) < 0 ) {
syslog( LOG_ERR, "%s open %s: %m", argv[ 0 ], tf );
exit( EX_TEMPFAIL );
}
if (( tff = fdopen( fd, "w" )) == NULL ) {
syslog( LOG_ERR, "fdopen: %m" );
close( fd );
unlink( tf );
exit( EX_TEMPFAIL );
}
if ( arg1 == NULL ) {
printf( "%s\n", command );
if ( fprintf( tff, "%s\n", command ) < 0 ) {
syslog( LOG_ERR, "fprintf: %m" );
perror( "fprintf" );
close( fd );
unlink( tf );
exit( EX_TEMPFAIL );
}
} else {
printf( "%s %s\n", command, arg1 );
if ( fprintf( tff, "%s %s\n", command, arg1 ) < 0 ) {
syslog( LOG_ERR, "fprintf: %m" );
perror( "fprintf" );
close( fd );
unlink( tf );
exit( EX_TEMPFAIL );
}
}
if ( fclose( tff ) != 0 ) {
syslog( LOG_ERR, "%m" );
unlink( tf );
exit( EX_TEMPFAIL );
}
if ( rename( tf, cf ) < 0 ) {
syslog( LOG_ERR, "Syserror: rename %s %s: %m", tf, cf );
perror( "rename" );
unlink( tf );
exit( EX_TEMPFAIL );
}
/* signal server */
if (( pidfd = open( SIMTA_FILE_PID, O_RDONLY, 0 )) < 0 ) {
syslog( LOG_NOTICE, "open %s: %m", SIMTA_FILE_PID );
exit( EX_TEMPFAIL );
}
if (( pf = fdopen( pidfd, "r" )) == NULL ) {
syslog( LOG_NOTICE, "fdopen %s: %m", SIMTA_FILE_PID );
exit( EX_TEMPFAIL );
}
fscanf( pf, "%d\n", &server_pid );
if ( server_pid <= 0 ) {
syslog( LOG_NOTICE, "illegal pid %s: %d", SIMTA_FILE_PID, server_pid );
exit( EX_TEMPFAIL );
}
if ( kill( server_pid, SIGUSR2 ) < 0 ) {
syslog( LOG_NOTICE, "kill %d: %m", server_pid );
exit( EX_TEMPFAIL );
}
return( 0 );
}