Mercurial > hg > mlmmj
comparison src/mlmmj-send.c @ 776:de31a9e15c5b
Improved and more consistent closing of SMTP sessions
author | Ben Schmidt |
---|---|
date | Fri, 12 Nov 2010 02:30:00 +1100 |
parents | 62f9b36df5a5 |
children | 5f038f36f66f |
comparison
equal
deleted
inserted
replaced
775:d0bf2135ab34 | 776:de31a9e15c5b |
---|---|
418 myfree(reply); | 418 myfree(reply); |
419 retval = MLMMJ_QUIT; | 419 retval = MLMMJ_QUIT; |
420 } | 420 } |
421 | 421 |
422 close(*sockfd); | 422 close(*sockfd); |
423 | |
424 *sockfd = -1; | 423 *sockfd = -1; |
425 | 424 |
426 return retval; | 425 return retval; |
427 } | 426 } |
428 | 427 |
1006 | 1005 |
1007 switch(listctrl[0]) { | 1006 switch(listctrl[0]) { |
1008 case '1': /* A single mail is to be sent */ | 1007 case '1': /* A single mail is to be sent */ |
1009 case '6': | 1008 case '6': |
1010 initsmtp(&sockfd, relay, smtpport); | 1009 initsmtp(&sockfd, relay, smtpport); |
1011 sendres = send_mail(sockfd, bounceaddr, to_addr, replyto, | 1010 if(send_mail(sockfd, bounceaddr, to_addr, replyto, |
1012 mailmap, st.st_size, listdir, NULL, | 1011 mailmap, st.st_size, listdir, NULL, |
1013 hdrs, hdrslen, body, bodylen); | 1012 hdrs, hdrslen, body, bodylen)) { |
1014 endsmtp(&sockfd); | 1013 close(sockfd); |
1015 if(sendres) { | 1014 sockfd = -1; |
1016 /* error, so keep it in the queue */ | 1015 /* error, so keep it in the queue */ |
1017 deletewhensent = 0; | 1016 deletewhensent = 0; |
1018 /* dump data we want when resending first check | 1017 /* dump data we want when resending first check |
1019 * if it already exists. In that case continue */ | 1018 * if it already exists. In that case continue */ |
1020 tmpstr = concatstr(2, mailfilename, ".mailfrom"); | 1019 tmpstr = concatstr(2, mailfilename, ".mailfrom"); |
1058 strlen(replyto)); | 1057 strlen(replyto)); |
1059 fsync(tmpfd); | 1058 fsync(tmpfd); |
1060 } | 1059 } |
1061 close(tmpfd); | 1060 close(tmpfd); |
1062 } | 1061 } |
1062 } else { | |
1063 endsmtp(&sockfd); | |
1063 } | 1064 } |
1064 break; | 1065 break; |
1065 case '2': /* Moderators */ | 1066 case '2': /* Moderators */ |
1066 initsmtp(&sockfd, relay, smtpport); | 1067 initsmtp(&sockfd, relay, smtpport); |
1067 if(send_mail_many_fd(sockfd, bounceaddr, NULL, mailmap, | 1068 if(send_mail_many_fd(sockfd, bounceaddr, NULL, mailmap, |
1068 st.st_size, subfd, NULL, NULL, NULL, | 1069 st.st_size, subfd, NULL, NULL, NULL, |
1069 listdir, NULL, hdrs, hdrslen, | 1070 listdir, NULL, hdrs, hdrslen, |
1070 body, bodylen)) | 1071 body, bodylen)) { |
1071 close(sockfd); | 1072 close(sockfd); |
1072 else | 1073 sockfd = -1; |
1074 } else { | |
1073 endsmtp(&sockfd); | 1075 endsmtp(&sockfd); |
1076 } | |
1074 break; | 1077 break; |
1075 case '3': /* resending earlier failed mails */ | 1078 case '3': /* resending earlier failed mails */ |
1076 initsmtp(&sockfd, relay, smtpport); | 1079 initsmtp(&sockfd, relay, smtpport); |
1077 if(send_mail_many_fd(sockfd, NULL, NULL, mailmap, st.st_size, | 1080 if(send_mail_many_fd(sockfd, NULL, NULL, mailmap, st.st_size, |
1078 subfd, listaddr, listdelim, mailfilename, | 1081 subfd, listaddr, listdelim, mailfilename, |
1079 listdir, mlmmjbounce, hdrs, hdrslen, | 1082 listdir, mlmmjbounce, hdrs, hdrslen, |
1080 body, bodylen)) | 1083 body, bodylen)) { |
1081 close(sockfd); | 1084 close(sockfd); |
1082 else | 1085 sockfd = -1; |
1086 } else { | |
1083 endsmtp(&sockfd); | 1087 endsmtp(&sockfd); |
1088 } | |
1084 unlink(subfilename); | 1089 unlink(subfilename); |
1085 break; | 1090 break; |
1086 case '4': /* send mails to owner */ | 1091 case '4': /* send mails to owner */ |
1087 initsmtp(&sockfd, relay, smtpport); | 1092 initsmtp(&sockfd, relay, smtpport); |
1088 if(send_mail_many_fd(sockfd, bounceaddr, NULL, mailmap, | 1093 if(send_mail_many_fd(sockfd, bounceaddr, NULL, mailmap, |
1089 st.st_size, subfd, listaddr, listdelim, | 1094 st.st_size, subfd, listaddr, listdelim, |
1090 mailfilename, listdir, mlmmjbounce, | 1095 mailfilename, listdir, mlmmjbounce, |
1091 hdrs, hdrslen, body, bodylen)) | 1096 hdrs, hdrslen, body, bodylen)) { |
1092 close(sockfd); | 1097 close(sockfd); |
1093 else | 1098 sockfd = -1; |
1099 } else { | |
1094 endsmtp(&sockfd); | 1100 endsmtp(&sockfd); |
1101 } | |
1095 break; | 1102 break; |
1096 case '5': /* bounceprobe - handle relayhost local users bouncing*/ | 1103 case '5': /* bounceprobe - handle relayhost local users bouncing*/ |
1097 initsmtp(&sockfd, relay, smtpport); | 1104 initsmtp(&sockfd, relay, smtpport); |
1098 sendres = send_mail(sockfd, bounceaddr, to_addr, replyto, | 1105 if(send_mail(sockfd, bounceaddr, to_addr, replyto, |
1099 mailmap, st.st_size, listdir, NULL, | 1106 mailmap, st.st_size, listdir, NULL, |
1100 hdrs, hdrslen, body, bodylen); | 1107 hdrs, hdrslen, body, bodylen)) { |
1101 endsmtp(&sockfd); | 1108 close(sockfd); |
1102 if(sendres) { | 1109 sockfd = -1; |
1103 /* error, so remove the probefile */ | 1110 /* error, so remove the probefile */ |
1104 tmpstr = mystrdup(to_addr); | 1111 tmpstr = mystrdup(to_addr); |
1105 a = strchr(tmpstr, '@'); | 1112 a = strchr(tmpstr, '@'); |
1106 MY_ASSERT(a); | 1113 MY_ASSERT(a); |
1107 *a = '='; | 1114 *a = '='; |
1108 probefile = concatstr(4, listdir, "/bounce/", tmpstr, | 1115 probefile = concatstr(4, listdir, "/bounce/", tmpstr, |
1109 "-probe"); | 1116 "-probe"); |
1110 unlink(probefile); | 1117 unlink(probefile); |
1111 myfree(probefile); | 1118 myfree(probefile); |
1112 myfree(tmpstr); | 1119 myfree(tmpstr); |
1120 } else { | |
1121 endsmtp(&sockfd); | |
1113 } | 1122 } |
1114 break; | 1123 break; |
1115 case '7': | 1124 case '7': |
1116 digest = 1; | 1125 digest = 1; |
1117 addtohdr = 1; | 1126 addtohdr = 1; |
1171 "Not sending with VERP."); | 1180 "Not sending with VERP."); |
1172 myfree(reply); | 1181 myfree(reply); |
1173 verp = NULL; | 1182 verp = NULL; |
1174 } | 1183 } |
1175 } | 1184 } |
1185 /* We can't be in SMTP DATA state or anything like | |
1186 * that, so should be able to safely QUIT. */ | |
1176 endsmtp(&sockfd); | 1187 endsmtp(&sockfd); |
1177 } | 1188 } |
1178 | 1189 |
1179 while((dp = readdir(subddir)) != NULL) { | 1190 while((dp = readdir(subddir)) != NULL) { |
1180 if(!strcmp(dp->d_name, ".")) | 1191 if(!strcmp(dp->d_name, ".")) |
1236 listdir, | 1247 listdir, |
1237 mlmmjbounce, | 1248 mlmmjbounce, |
1238 hdrs, hdrslen, | 1249 hdrs, hdrslen, |
1239 body, bodylen); | 1250 body, bodylen); |
1240 } | 1251 } |
1241 endsmtp(&sockfd); | 1252 if (sendres) { |
1253 close(sockfd); | |
1254 sockfd = -1; | |
1255 } else { | |
1256 endsmtp(&sockfd); | |
1257 } | |
1242 for(i = 0; i < stl.count; i++) | 1258 for(i = 0; i < stl.count; i++) |
1243 myfree(stl.strs[i]); | 1259 myfree(stl.strs[i]); |
1244 stl.count = 0; | 1260 stl.count = 0; |
1245 } | 1261 } |
1246 } while(res > 0); | 1262 } while(res > 0); |
1264 &stl, listaddr, listdelim, | 1280 &stl, listaddr, listdelim, |
1265 archivefilename, listdir, | 1281 archivefilename, listdir, |
1266 mlmmjbounce, hdrs, hdrslen, | 1282 mlmmjbounce, hdrs, hdrslen, |
1267 body, bodylen); | 1283 body, bodylen); |
1268 } | 1284 } |
1269 endsmtp(&sockfd); | 1285 if (sendres) { |
1286 close(sockfd); | |
1287 sockfd = -1; | |
1288 } else { | |
1289 endsmtp(&sockfd); | |
1290 } | |
1270 for(i = 0; i < stl.count; i++) | 1291 for(i = 0; i < stl.count; i++) |
1271 myfree(stl.strs[i]); | 1292 myfree(stl.strs[i]); |
1272 stl.count = 0; | 1293 stl.count = 0; |
1273 } | |
1274 | |
1275 if (sendres) { | |
1276 /* If send_mail_many() failed we close the | |
1277 * connection to the mail server in a brutal | |
1278 * manner, because we could be in any state | |
1279 * (DATA for instance). */ | |
1280 close(sockfd); | |
1281 } else { | |
1282 endsmtp(&sockfd); | |
1283 } | 1294 } |
1284 myfree(stl.strs); | 1295 myfree(stl.strs); |
1285 myfree(verpfrom); | 1296 myfree(verpfrom); |
1286 closedir(subddir); | 1297 closedir(subddir); |
1287 myfree(subddirname); | 1298 myfree(subddirname); |
1294 | 1305 |
1295 myfree(listdelim); | 1306 myfree(listdelim); |
1296 myfree(hdrs); | 1307 myfree(hdrs); |
1297 myfree(body); | 1308 myfree(body); |
1298 myfree(mlmmjbounce); | 1309 myfree(mlmmjbounce); |
1299 close(sockfd); | |
1300 munmap(mailmap, st.st_size); | 1310 munmap(mailmap, st.st_size); |
1301 close(mailfd); | 1311 close(mailfd); |
1302 myfree(verp); | 1312 myfree(verp); |
1303 | 1313 |
1304 if(archive) { | 1314 if(archive) { |