auth.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * saop: smtp authentication over pop3
  3. * Copyright (C) 2007 Juan José Gutiérrez de Quevedo <juanjo@iteisa.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. * @author Juan José Gutiérrez de Quevedo <juanjo@iteisa.com>
  19. */
  20. #include "auth.h"
  21. #ifdef DEBUG
  22. extern FILE *fsaopdebug;
  23. #endif /* DEBUG */
  24. int authenticate_with_pop3(char *user,char *pass,char *server,unsigned long port)
  25. {
  26. int fd;
  27. char *line=NULL;
  28. char tmpline[1024];
  29. debug("Connecting to pop3 server");
  30. if(-1==(fd=create_connected_socket(server,port))) { printf("Error connecting to pop3 server\n"); return 0; }
  31. /* read pop3 banner */
  32. free_and_read_string(line,fd);
  33. if(NULL==line||!cmp(line,"-ERR")) { closeskt(fd); return 0; }
  34. /* check user */
  35. snprintf(tmpline,sizeof(tmpline),"USER %s",user);
  36. write_string(fd,tmpline);
  37. free_and_read_string(line,fd);
  38. if(NULL==line||!cmp(line,"-ERR")) { closeskt(fd); return 0; }
  39. /* check password */
  40. snprintf(tmpline,sizeof(tmpline),"PASS %s",pass);
  41. write_string(fd,tmpline);
  42. free_and_read_string(line,fd);
  43. if(NULL==line||!cmp(line,"-ERR")) { closeskt(fd); return 0; }
  44. closeskt(fd);
  45. return 1;
  46. }
  47. int authenticate_with_imap4(char *user,char *pass,char *server,unsigned long port)
  48. {
  49. int fd;
  50. char *line=NULL;
  51. char tmpline[1024];
  52. debug("Connecting to imap4 server");
  53. if(-1==(fd=create_connected_socket(server,port))) { printf("Error connecting to imap4 server\n"); return 0; }
  54. /* read imap4 banner */
  55. free_and_read_string(line,fd);
  56. if(NULL==line||cmp(line,"* OK")) { closeskt(fd); return 0; }
  57. /* check user and password */
  58. snprintf(tmpline,sizeof(tmpline),"0 LOGIN %s %s",user,pass);
  59. write_string(fd,tmpline);
  60. free_and_read_string(line,fd);
  61. if(NULL==line||cmp(line+2,"OK")) { closeskt(fd); return 0; }
  62. closeskt(fd);
  63. return 1;
  64. }
  65. int authenticate_with_file(char *p_user,char *p_pass,char *file)
  66. {
  67. FILE *f;
  68. char line[1024];
  69. char *pass=NULL;
  70. int retval=0;
  71. if(NULL==(f=fopen(file,"r"))) { perror("Error opening password file"); return 0; }
  72. while(!feof(f))
  73. {
  74. fgets(line,sizeof(line),f);
  75. if(cmp(line,"#")&&NULL!=(pass=strchr(line,':')))
  76. {
  77. *(pass++)='\0';
  78. if(!cmp(p_user,line)&&!cmp(p_pass,pass))
  79. {
  80. retval=1;
  81. break;
  82. }
  83. }
  84. }
  85. fclose(f);
  86. return retval;
  87. }