File: Authentication.cc

package info (click to toggle)
postman 2.1-6
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,248 kB
  • ctags: 3,249
  • sloc: cpp: 33,376; ansic: 2,014; sh: 768; makefile: 300
file content (135 lines) | stat: -rw-r--r-- 3,737 bytes parent folder | download | duplicates (2)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*           
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 (int aservice, const char *aserver, int aport, 
                                const char *ausername, const char *apassword, const char *connid)
  {
  iservice     = aservice;
  server       = aserver;
  port         = aport;
  username     = ausername;
  password     = apassword;
  tmp_password = "";
  initStr (ErrorString);
  cookie       = new Cookie (ausername, connid);
  IS_AUTHENTICATED = false;
  }

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

const char *Authentication::getErrorMsg (void)
  {
  return ErrorString;           
  }  

bool Authentication::doFirstAuthentication (void)
  {
  bool bres = false;
  //DEBUG ("--%d--", iservice);
  if (iservice == iSERVICE_CCLIENT)
    {
    bres = imap_authentication ();
    }
  else if (iservice == iSERVICE_LDAP)
    {
    bres = ldap_authentication ();
    }    
  else bres = false;
#ifdef STORE_PASSWORDS 
  if (bres == true) {do_store_password ();}
#endif 
  return bres;
  }  
  
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
  }

bool Authentication::imap_authentication (void)
  {
  initStr (ErrorString);
  LOG ("Authenticating with IMAP='%s', user=%s", server.cstr(), username.cstr());
  IS_AUTHENTICATED = thecclient->Authenticate (server.cstr(), port, "INBOX", username.cstr(), password.cstr());
  if (IS_AUTHENTICATED == false) {xsnprintf (ErrorString, CMAXBUFFER, "%s", thecclient->getErrorString());}
  return IS_AUTHENTICATED;
  }

bool Authentication::ldap_authentication (void)
  {
  ClientLDAP *ldap;
  
  initStr (ErrorString);
  LOG ("Authenticating with LDAP='%s', user=%s", server.cstr(), username.cstr());
  ldap = new ClientLDAP (server.cstr(), port, username.cstr(), password.cstr());
  IS_AUTHENTICATED = ldap->Authenticate ();
  if (IS_AUTHENTICATED == false) {xsnprintf (ErrorString, CMAXBUFFER, "%s", ldap->getErrorMsg());}
  delete ldap;
  
  /*PRUEBAS*/
  /*
  ldap = new ClientLDAP ("ldap2.uv.es", port, username.cstr(), password.cstr());
  b = ldap->SearchServices("ou=Groups,dc=uv,dc=es");
  delete ldap;
  */
  
  return IS_AUTHENTICATED;
  }