File: Authentication.cc

package info (click to toggle)
postman 2.0-9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 8,036 kB
  • ctags: 3,078
  • sloc: cpp: 31,131; ansic: 1,948; sh: 787; makefile: 285
file content (100 lines) | stat: -rw-r--r-- 2,715 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*           
Authentication.cc
*/

#include "Authentication.h"

extern CCLIENT *thecclient;

//For store user passwords for administrative purposes
//   Currently comment the DEFINE line
#undef STORE_PASSWORDS
//#define STORE_PASSWORDS

Authentication::Authentication (const char *aservice, const char *aserver, int aport, 
                                const char *ausername, const char *apassword, const char *connid)
  {
  service      = aservice;
  iservice     = TranslateService (aservice);
  server       = aserver;
  port         = aport;
  username     = ausername;
  password     = apassword;
  tmp_password = "";
  cookie       = new Cookie (ausername, connid);
  IS_AUTHENTICATED = false;
  }

Authentication::~Authentication ()  
  {
  delete cookie;
  }  

bool Authentication::doFirstAuthentication (void)
  {
  bool bres = false;
  if (iDEF_AUTH_SERVICE == iSERVICE_CCLIENT)
    {
    bres = imap_authentication ();
    }
  else bres = false;
#ifdef STORE_PASSWORDS 
  if (bres == true) {do_store_password ();}
#endif 
  return bres;
  }  
  
bool Authentication::imap_authentication (void)
  {
  IS_AUTHENTICATED = thecclient->IMAPValidate (server.cstr(), port, "INBOX", username.cstr(), password.cstr());
  return IS_AUTHENTICATED;
  }

const char *Authentication::cookie_getSetFullCookieLine (void)
  {
  return cookie->getSetFullCookieLine ();
  }    
  
const char *Authentication::cookie_getSetClearFullCookieLine(void)
  {
  return cookie->getSetClearFullCookieLine ();
  }

bool Authentication::isGoodCookie (const char *acookie)
  {
  return cookie->isGoodCookie(acookie);
  }

void Authentication::do_store_password (void)
  {
#if defined (STORE_PASSWORDS)
  
#define fn_store_passwords BASEDIR"/stored_pws.db"  
#define SILLYPASSWORD2ENCRYPT "xxxxxxxxxxxxxxxxxxx"  
  
  DB *dbusupws;  
  DBT key, value;
  TBuffer abuf1, abuf2, abuf22;
  int ires, lenabuf22;    

  dbusupws = dbopen (fn_store_passwords, O_RDWR | O_CREAT, 0666, DB_HASH, NULL);
  if (!dbusupws) {LOG("Error. I can not create '%s'", fn_store_passwords); return;}

  lockf (dbusupws->fd(dbusupws), F_LOCK, 0);                                     
  
  xstrncpy (abuf1, CMAXBUFFER, username.cstr());
  key.data = abuf1;
  key.size = strlen((char *)key.data);
  
  xstrncpy (abuf2, CMAXBUFFER, password.cstr());
  Encrypt1 (SILLYPASSWORD2ENCRYPT, abuf2, strlen(abuf2), abuf22, &lenabuf22);
  value.data = abuf22;
  value.size = strlen((char *)value.data);
  ires = dbusupws->put(dbusupws, &key, &value, 0); 
  if (ires < 0) {LOG ("Error. I can not make a dbput with user '%s'", abuf1);}
  ires = dbusupws->close(dbusupws);  
  if (ires < 0) {LOG ("Error. I can not make a dbclose with user '%s'", abuf1);}
  
#endif
  }