Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "global.h"
- #include "md5.h"
- #include "hmac_md5.h"
- #include <errno.h>
- #include <pwd.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <my_global.h>
- #include <mysql.h>
- #define LINE_MAX 256
- #define SMTP_PASSWD_FILE "/etc/smtppasswd"
- MYSQL *conn;
- MYSQL_RES *res;
- MYSQL_ROW *row;
- char up[513];
- int uplen;
- static char hextab[]="0123456789abcdef";
- int sq( char *query) {
- conn = mysql_init(NULL);
- if (conn == NULL) {
- printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
- exit(1);
- }
- if (mysql_real_connect(conn, "mysql.sitename.net", "cmd5yourpw", "blarpass", "cmd5yourpw", 0, NULL, 0) == NULL) {
- printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
- exit(1);
- }
- mysql_query(conn, query);
- mysql_close(conn);
- }
- int unblock(char *ip) {
- unsigned char *query[128];
- int ul = 0;
- conn = mysql_init(NULL);
- if (conn == NULL) {
- printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
- exit(1);
- }
- if (mysql_real_connect(conn, "mysql.sitename.net", "cmd5yourpw", "blarpass", "cmd5yourpw", 0, NULL, 0) == NULL) {
- printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
- exit(1);
- }
- sprintf(query, "select count(*) from blocked where ip = '%s'",ip);
- mysql_query(conn, query);
- res = mysql_store_result(conn);
- row = mysql_fetch_row(res);
- if(row) {
- sprintf(query, "delete from blocked where ip = '%s'", ip);
- sq(query);
- sprintf(query, "/bin/bash /push/scripts/unblock %s", ip);
- system(query);
- }
- return(1);
- }
- int check(char *ip) {
- unsigned char *query[128];
- int ul = 0;
- conn = mysql_init(NULL);
- if (conn == NULL) {
- printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
- exit(1);
- }
- if (mysql_real_connect(conn, "mysql.sitename.net", "cmd5yourpw", "blarpass", "cmd5yourpw", 0, NULL, 0) == NULL) {
- printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
- exit(1);
- }
- sprintf(query, "select attempts from blocked where ip = '%s'",ip);
- mysql_query(conn, query);
- res = mysql_store_result(conn);
- row = mysql_fetch_row(res);
- if(!row) {
- printf("No Results Found, adding %s", ip);
- sprintf(query, "insert into blocked (ip, attempts) values( '%s','1')", ip);
- sq(query);
- } else {
- ul = atoi(row[0]);
- if (ul < 3 ) {
- sprintf(query, "update blocked set modified = NOW(), attempts=attempts+1 where ip = '%s'", ip);
- sq(query);
- }
- if(ul == 3) {
- block(ip);
- }
- }
- return(1);
- }
- int block(char *ip) {
- char *string[48];
- sprintf(string,"/bin/bash /push/scripts/nullroute %s\n", ip);
- system(string);
- return(1);
- }
- int doit(unsigned char *testlogin, unsigned char *challenge, unsigned char *response)
- {
- static char line[LINE_MAX + 1];
- int found_user= 0;
- unsigned char *password = NULL;
- unsigned char *salt = (char *)malloc(3);
- unsigned char *cpassword = (char *)malloc(LINE_MAX);
- unsigned char *cchallenge = NULL;
- int res;
- unsigned char digest[16];
- unsigned char digascii[33];
- unsigned char h;
- FILE *fp, *lfp;
- int j;
- char *remoteip = NULL;
- char *linepnt;
- char *time_str = (char*)malloc(256);
- time_t tm;
- pid_t pid;
- pid = getpid();
- // Open the password file..
- if ((remoteip = getenv("TCPREMOTEIP")) == NULL) {
- remoteip = (char*)malloc(32);
- strcpy(remoteip, "(unknown host)");
- }
- // Open the password file..
- if ((fp = fopen(SMTP_PASSWD_FILE, "rt")) == NULL) _exit(2);
- // Open the log file..
- lfp=fopen("/hdtmp/cmd5yourpw.log", "a+");
- // Start pulling lines out..
- while (fgets(line, LINE_MAX, fp) != NULL)
- {
- //fprintf(lfp, "[%s] line from file: '%s'\n", testlogin, line);
- fflush(lfp);
- found_user = 0;
- if ((linepnt = strchr(line, '\n')) != NULL)
- {
- *linepnt = 0;
- }
- // If no ':', bad password file format, so loop
- if ((linepnt = strtok(line, ":")) == NULL) continue;
- // Is this our man?
- if (strcmp(linepnt, testlogin) == 0)
- {
- // If no password, bad password file format, so exit
- if ((linepnt = strtok(NULL, ":")) == NULL) _exit(2);
- // Yes! So, save it..
- password = linepnt;
- found_user = 1;
- }
- if (found_user)
- {
- /* So, they gave a username in our file, and a password.. Fail if the passwords don't match. */
- // Get the salt..
- strncpy(salt, password, 2);
- // Get the crypted string..
- strcpy(cpassword, password + 2);
- // Null terminate the salt..
- salt[2] = 0;
- tm = time(NULL);
- cftime(time_str, "%b %d %T", &tm);
- fprintf(lfp, "%s [pid=%d] [%s] [%s] string from file: '%s' salt: '%s' password: '%s'\n", time_str, pid, testlogin, remoteip, password, salt, cpassword);
- fprintf(lfp, "%s [pid=%d] challenge: '%s'\n", time_str, pid, challenge);
- fflush(lfp);
- // Crypt the challenge string with the salt
- tm = time(NULL);
- cftime(time_str, "%b %d %T", &tm);
- cchallenge = crypt(challenge, salt);
- fprintf(lfp, "%s [pid=%d] crypt()ed challenge: '%s'\n", time_str, pid, cchallenge);
- // Compare..
- res = strcmp(password, cchallenge);
- tm = time(NULL);
- cftime(time_str, "%b %d %T", &tm);
- fprintf(lfp, "%s [pid=%d] strcmp(password, cchallenge): %d\n", time_str, pid, res);
- // Do they match?!
- if (res == 0)
- {
- // YES!
- fprintf(lfp, "%s [pid=%d] Passwords match. Allowing SMTP.\n", time_str, pid);
- unblock(remoteip);
- fclose(fp);
- fclose(lfp);
- return 0;
- }
- // Nope, so try the next one..
- }
- }
- fclose(fp);
- tm = time(NULL);
- cftime(time_str, "%b %d %T", &tm);
- check(remoteip);
- fprintf(lfp, "%s [pid=%d] [%s] Username/password pair not matched! Denying SMTP.\n", time_str, pid, remoteip);
- fclose(lfp);
- return 1;
- if (!found_user) return(1);
- hmac_md5( cchallenge, strlen(cchallenge), password, strlen(password), digest);
- digascii[32]=0;
- for (j=0;j<16;j++)
- {
- h=digest[j] >> 4;
- digascii[2*j]=hextab[h];
- h=digest[j] & 0x0f;
- digascii[(2*j)+1]=hextab[h];
- }
- return(strcmp(digascii,response) && strcmp(password,cchallenge));
- }
- void main(int argc,char **argv)
- {
- char *login;
- char *response;
- char *challenge;
- int r;
- int i;
- int accepted;
- if (!argv[1]) _exit(2);
- uplen = 0;
- for (;;) {
- do
- r = read(3,up + uplen,sizeof(up) - uplen);
- while ((r == -1) && (errno == EINTR));
- if (r == -1) _exit(111);
- if (r == 0) break;
- uplen += r;
- if (uplen >= sizeof(up)) _exit(1);
- }
- close(3);
- i = 0;
- login = up + i;
- while (up[i++]) if (i == uplen) _exit(2);
- challenge = up + i;
- if (i == uplen) _exit(2);
- while (up[i++]) if (i == uplen) _exit(2);
- response = up + i;
- accepted=doit(login,challenge,response);
- for (i = 0;i < sizeof(up);++i) up[i] = 0;
- if (accepted) _exit(1);
- execvp(argv[1],argv + 1);
- _exit(111);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement