📄 ssmtp.c
字号:
{ fclose(outfile); continue; } fprintf(outfile, "\r\n--=\r\n"); } if(!wrotefiletype++) { fprintf(outfile, "Content-Type: %s; name=\"%s\"\r\n", type, cleanfname); fprintf(outfile, "Content-Transfer-Encoding: base64\r\n"); fprintf(outfile, "Content-Disposition: inline; filename=\"%s\"\r\n", cleanfname); fprintf(outfile, "Content-MD5: %s\r\n\r\n", digest); free(digest); written += 80; } if (written == maxsize) written--; // avoid a nasty fencepost error // to base 64 written += to64(infile, outfile, (thispart == numparts) ? 0 : (maxsize-written)); if (thispart == numparts) { if (applefile) fprintf(outfile, "\r\n--=--\r\n"); fprintf(outfile, "\r\n-----\r\n"); // end boundary } fclose(outfile); } // end of main for loop return 0;}///////////////////////////////////////////////////////////////////////// Function ssmtp_failed//// Fatal handle function/////////////////////////////////////////////////////////////////////////void ssmtp_failed(int error_code){ switch(error_code) { case MEM_ALLOC_FAIL: dbgmsg("ssmtp: memory allocate failed\n"); exit(0); break; case SOCKET_FAIL: dbgmsg("ssmtp: socket error\n"); exit(0); break; case HOSTNAME_ERR: dbgmsg("ssmtp: unresolved server name\n"); exit(0); break; case CONNECT_ERR: dbgmsg("ssmtp: unable to connect to server, check DNS and network settings\n"); exit(0); break; case RECV_ERR: dbgmsg("ssmtp: receive from server error\n"); exit(0); break; case SEND_ERR: dbgmsg("ssmtp: send to server error\n"); exit(0); break; case RECV_TIMEOUT: dbgmsg("ssmtp: receive from server timed out\n"); exit(0); break; case SMTP_FAILED: dbgmsg("ssmtp: SMTP server error\n"); dbgmsg("%s", ssmtp_stored_message); break; case PIC_OPEN_ERR: dbgmsg("ssmtp: unable to open picture file\n"); break; case TMP_OPEN_ERR: dbgmsg("ssmtp: unable to create tmep file\n"); break; case FILE_READ_ERR: dbgmsg("ssmtp: file read error\n"); break; }}///////////////////////////////////////////////////////////////////////// Function ssmtp_usage//// show smtp usage information/////////////////////////////////////////////////////////////////////////int ssmtp_usage(void){ printf("ssmtp usage:\n"); printf("[-f from_address] [-t to_address] [-u username] [-p password]\n"); printf("[-s server_address] [-d domain] [-a authtication_method]\n"); printf("[-c attched_file_name] [-n mime_encode_number]\n\n"); exit(0);}///////////////////////////////////////////////////////////////////////// Function ssmtp_init//// brief initialize smtp information structure from config//// TODO// read config file from ramdisk/////////////////////////////////////////////////////////////////////////int ssmtp_init(SMTP_Client_Info *smtpinfo, int argc, char *argv[]){ int i,var; struct timeval tv; struct timezone tz; char cbuf[100]; char toaddr_param=0; char server_param=0; char attch_param=0; char mime_num=0; // get day time var = gettimeofday(&tv, &tz); strcpy(mail_date, ctime(&(tv.tv_sec)) ); smtpinfo->port = SMTPPORT; // fixed to port 25 smtpinfo->auth_mech = SASL_DEFAULT; smtpinfo->host=tmp_host; smtpinfo->fromaddr=tmp_fromaddr; smtpinfo->toaddr=tmp_toaddr; smtpinfo->domain=tmp_domain; smtpinfo->auth_user=tmp_auth_user; smtpinfo->auth_pass=tmp_auth_pass; if( argc < 4 ) ssmtp_usage(); for(i=0; i<argc; i++) { if( !strcmp(argv[i], "-f") ) { if( argv[i+1]!=NULL ) smtpinfo->fromaddr = argv[i+1]; else ssmtp_usage(); } if( !strcmp(argv[i], "-t") ) { if( argv[i+1]!=NULL ) { smtpinfo->toaddr = argv[i+1]; toaddr_param = 1; } else ssmtp_usage(); } if( !strcmp(argv[i], "-u") ) { if( argv[i+1]!=NULL ) smtpinfo->auth_user = argv[i+1]; else ssmtp_usage(); } if( !strcmp(argv[i], "-p") ) { if( argv[i+1]!=NULL ) smtpinfo->auth_pass = argv[i+1]; else ssmtp_usage(); } if( !strcmp(argv[i], "-s") ) { if( argv[i+1]!=NULL ) { smtpinfo->host = argv[i+1]; server_param = 1; } else ssmtp_usage(); } if( !strcmp(argv[i], "-d") ) { if( argv[i+1]!=NULL ) smtpinfo->domain = argv[i+1]; else ssmtp_usage(); } if( !strcmp(argv[i], "-n") ) { if( argv[i+1]!=NULL ) { strcpy(mimetmpfile, argv[i+1]); strcat(mimetmpfile, "mime.tmp"); mime_num = 1; } else ssmtp_usage(); } if( !strcmp(argv[i], "-c") ) { if( argv[i+1]!=NULL ) { strcpy(attachment, argv[i+1]); attch_param = 1; } else ssmtp_usage(); } if( !strcmp(argv[i], "-a") ) { if( argv[i+1]!=NULL ) { if(!strcmp(argv[i+1], "login")) smtpinfo->auth_mech = SASL_LOGIN; else if(!strcmp(argv[i+1], "plain")) smtpinfo->auth_mech = SASL_PLAIN; else smtpinfo->auth_mech = SASL_DEFAULT; } else ssmtp_usage(); } } if(toaddr_param==0 || server_param==0 || attch_param==0 || mime_num==0) ssmtp_usage(); return 0;}///////////////////////////////////////////////////////////////////////// Function ssmtp_connect//// brief opens a socket and getting connection to server/////////////////////////////////////////////////////////////////////////int ssmtp_connect(SMTP_Client_Info *smtpinfo){ struct hostent *servp; struct sockaddr_in addr; // get host name if domain name assigned servp = NULL; servp = gethostbyname(smtpinfo->host); if( servp == NULL ) { ssmtp_failed(HOSTNAME_ERR); return -1; } // open socket if( (smtpinfo->sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { perror("ssmtp: socket"); ssmtp_failed(SOCKET_FAIL); return -1; } // set non_blocking // fcntl(smtpinfo->sockfd, F_SETFL, O_NONBLOCK); addr.sin_family = AF_INET; addr.sin_port = htons(smtpinfo->port); addr.sin_addr = *((struct in_addr *)servp->h_addr); memset(&(addr.sin_zero), '\0', 8); // connect if( connect(smtpinfo->sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == -1 ) { perror("ssmtp: connect"); ssmtp_failed(CONNECT_ERR); return -1; } return smtpinfo->sockfd;}///////////////////////////////////////////////////////////////////////// Function ssmtp_okay//// simplely read response from server/////////////////////////////////////////////////////////////////////////int ssmtp_read(SMTP_Client_Info *smtpinfo, char *buf){ int i,s,n; struct timeval timeout_tv; fd_set sockfds; s = smtpinfo->sockfd; timeout_tv.tv_sec = SMTPTIMEOUT; timeout_tv.tv_usec = 0; FD_ZERO(&sockfds); FD_SET(s, &sockfds); select(s+1, &sockfds, NULL, NULL, &timeout_tv); if(FD_ISSET(s, &sockfds)) { n = recv(smtpinfo->sockfd, buf, MAXDATASIZE, 0); if(n<0) { perror("ssmtp: recv"); ssmtp_failed(RECV_ERR); return -1; } buf[n]='\0'; // store response message memset(ssmtp_stored_message, '\0', MAXDATASIZE); for( i=0; i<n; i++ ) ssmtp_stored_message[i] = buf[i]; return n; } else { // timeout, retry ? ssmtp_failed(RECV_TIMEOUT); return -1; }}///////////////////////////////////////////////////////////////////////// Function ssmtp_send//// simplely read response from server/////////////////////////////////////////////////////////////////////////int ssmtp_send(SMTP_Client_Info *smtpinfo, char *buf){ if( sendall(smtpinfo->sockfd, buf, strlen(buf)) < 0 ) { ssmtp_failed(SEND_ERR); printf("ssmtp send FAIL !!!!\n"); return -1; } return 0;}///////////////////////////////////////////////////////////////////////// Function ssmtp_ready//// simply read status code from server// to check whetehr server is ready/////////////////////////////////////////////////////////////////////////int ssmtp_ready(SMTP_Client_Info *smtpinfo, char *status_code){ int i; char buf[MAXDATASIZE]; usleep(READ_DELAY); // wait for response, avoid packet split if( ssmtp_read(smtpinfo, buf) < 0 ) return -1; i = strlen(status_code); if( !strncmp(status_code, buf, i) ) { // status code match, return 0 dbgmsg("%s", ssmtp_stored_message); return 0; } else { // status code not match, ssmtp state failed ssmtp_failed(SMTP_FAILED); return -1; }}///////////////////////////////////////////////////////////////////////// Function ssmtp_ehlo//// sends EHLO SMTP command/////////////////////////////////////////////////////////////////////////int ssmtp_ehlo(SMTP_Client_Info *smtpinfo, char *status_code){ int i; char buf[MAXDATASIZE]; strcpy(buf, "EHLO aaa.bbb"); strcat(buf, smtpinfo->domain); strcat(buf, "\r\n"); if( ssmtp_send(smtpinfo, buf) < 0 ) return -1; usleep(READ_DELAY); memset(buf, 0, MAXDATASIZE); if( ssmtp_read(smtpinfo, buf) < 0 ) return -1; i = strlen(status_code); if( !strncmp(status_code, buf, i) ) { // status code match, return 0 dbgmsg("%s", ssmtp_stored_message); return 0; } else { // status code not match, ssmtp state failed ssmtp_failed(SMTP_FAILED); return -1; }}///////////////////////////////////////////////////////////////////////// Function ssmtp_helo//// sends HELO SMTP command/////////////////////////////////////////////////////////////////////////int ssmtp_helo(SMTP_Client_Info *smtpinfo, char *status_code){ int i; char buf[MAXDATASIZE]; strcpy(buf, "HELO aaa.bbb\r\n"); if( ssmtp_send(smtpinfo, buf) < 0 ) return -1; usleep(READ_DELAY); memset(buf, 0, MAXDATASIZE); if( ssmtp_read(smtpinfo, buf) < 0 ) return -1; i = strlen(status_code); if( !strncmp(status_code, buf, i) ) { // status code match, return 0 dbgmsg("%s", ssmtp_stored_message); return 0; } else { // status code not match, ssmtp state failed ssmtp_failed(SMTP_FAILED); return -1; }}///////////////////////////////////////////////////////////////////////// Function ssmtp_mail//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -