Logo Search packages:      
Sourcecode: nbsmtp version File versions  Download package

int nbsmtp_auth ( servinfo_t serverinfo  ) 

Sends AUTH command to the server.

Parameters:
[in] serverinfo A pointer to a servinfo_t struct with the needed info
Returns:
Returns 0 in case of error and 1 in command success

Definition at line 366 of file nbsmtp.c.

References servinfo::auth_mech, servinfo::auth_pass, servinfo::auth_user, from64tobits(), hmac_md5(), log_msg(), smtp_last_message(), smtp_okay(), smtp_read(), smtp_write(), and to64frombits().

Referenced by send_mail().

{
      char local_in_buf[BUF_SIZE];
      char local_tmp_buf[BUF_SIZE];
      char *local_out_buf;
      int len;
      int i;

      memset(local_tmp_buf,0,sizeof(local_tmp_buf));

      if (serverinfo->auth_mech==SASL_LOGIN)
      {
            to64frombits((unsigned char*)local_tmp_buf,
                        (const unsigned char *)serverinfo->auth_user,
                        strlen(serverinfo->auth_user));

            asprintf(&local_out_buf,"AUTH LOGIN %s",local_tmp_buf);

            if (smtp_write(serverinfo,local_out_buf)<1)
            {
                  log_msg(LOG_ERR,"Error writting AUTH command to the socket");
                  return 0;
            }

            free(local_out_buf);

            if (smtp_read(serverinfo,local_in_buf)!=3)
            {
                  log_msg(LOG_ERR,"The server rejected the authentication method");
                  log_msg(LOG_ERR,"Server said: '%s'",smtp_last_message());

                  return 0;
            }

            memset(local_tmp_buf,0,sizeof(local_tmp_buf));

            to64frombits((unsigned char *)local_tmp_buf,
                        (const unsigned char *)serverinfo->auth_pass,
                        strlen(serverinfo->auth_pass));

            asprintf(&local_out_buf,"%s",local_tmp_buf);

            if (smtp_write(serverinfo,local_out_buf)<1)
            {
                  log_msg(LOG_ERR,"Error writting the password to the socket");
                  return 0;
            }

            free(local_out_buf);

            if (!smtp_okay(serverinfo))
            {
                  log_msg(LOG_ERR,"The password wasn't accepted");
                  log_msg(LOG_ERR,"Server said: '%s'",smtp_last_message());

                  return 0;
            }
      }
      else if(serverinfo->auth_mech==SASL_PLAIN)
      {
            asprintf(&local_out_buf,"^%s^%s",serverinfo->auth_user,serverinfo->auth_pass);
            len = strlen(local_out_buf);

            for ( i = len-1 ; i >= 0 ; i-- )
            {
                  if (local_out_buf[i]=='^')
                  {
                        local_out_buf[i]='\0';
                  }
            }

            to64frombits((unsigned char *)local_tmp_buf,(const unsigned char *)local_out_buf,len);

            free(local_out_buf);

            asprintf(&local_out_buf,"AUTH PLAIN %s",local_tmp_buf);

            if (smtp_write(serverinfo,local_out_buf)<1)
            {
                  log_msg(LOG_ERR,"Error writting AUTH PLAIN command to the socket");
                  return 0;
            }
            
            free(local_out_buf);

            if (!smtp_okay(serverinfo))
            {
                  log_msg(LOG_ERR,"Error, the authentication failed");
                  log_msg(LOG_ERR,"Server said: '%s'",smtp_last_message());

                  return 0;
            }
      }
#ifdef HAVE_SSL
      else if (serverinfo->auth_mech==SASL_CRAMMD5)
      {
            /*
             * This code has been adapted from a code by Oliver Hitz <oliver@net-track.ch>
             */
            unsigned char challenge[BUFSIZ];
            unsigned char digest[16];
            unsigned char digasc[33];
            char *decoded;
            unsigned char encoded[BUFSIZ];
            unsigned char greeting[BUFSIZ];
            static char hextab[] = "0123456789abcdef";

            local_out_buf = (char *)strdup("AUTH CRAM-MD5");

            if (smtp_write(serverinfo,local_out_buf)<1)
            {
                  log_msg(LOG_ERR,"Error writting AUTH CRAM-MD5 command to the socket");
                  return 0;
            }

            free(local_out_buf);

            if (smtp_read(serverinfo,local_in_buf)!=3)
            {
                  /* Server rejected the auth method */
                  log_msg(LOG_ERR,"The server rejected the authentication method");
                  log_msg(LOG_ERR,"Server said: '%s'",smtp_last_message());

                  return 0;
            }

            /* First get the greeting and decode the challenge */
            strncpy((char *)greeting,smtp_last_message(),sizeof(greeting));
            i = from64tobits((char *)challenge,(char *)greeting);

            /* Make sure challenge is '\0' ended, since from64tobits doesn't do it itself */
            challenge[i] = '\0';

            /* Perform the keyed-hashing algorithm */
            hmac_md5(challenge,strlen((char *)challenge),
                        (unsigned char *)serverinfo->auth_pass,
                        strlen(serverinfo->auth_pass),digest);

            /* Standard hexadecimal conversion */
            for (i = 0; i < 16; i++)
            {
                  digasc[2*i] = hextab[digest[i] >> 4];
                  digasc[2*i+1] = hextab[digest[i] & 0xf];
            }

            /* Always NULL-terminate digasc to avoid problems */
            digasc[32] = '\0';

            /* Create and encode the challenge response */
            asprintf(&decoded,"%s %s",serverinfo->auth_user,digasc);
            to64frombits((unsigned char*)encoded,(const unsigned char*)decoded,strlen(decoded));
            free(decoded);

            if (smtp_write(serverinfo,(char *)encoded)<1)
            {
                  log_msg(LOG_ERR,"Error writting auth string to the socket");
            }

            if (!smtp_okay(serverinfo))
            {
                  log_msg(LOG_ERR,"Error, the authentication failed");
                  log_msg(LOG_ERR,"Server said: '%s'",smtp_last_message());

                  return 0;
            }
      }
#endif

      /* Command succeded so tell the log */
      log_msg(LOG_INFO,"Authentication succeded [%s]",serverinfo->auth_user);

      return 1;
}


Generated by  Doxygen 1.6.0   Back to index