File: password_cache.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (207 lines) | stat: -rw-r--r-- 5,526 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "mforms/password_cache.h"

#include "base/log.h"
#include "base/threading.h"
#include <errno.h>
#include <cstdlib>
#include <cstring>

DEFAULT_LOG_DOMAIN("pwdcache");

#ifndef _WIN32
#include <sys/mman.h>
#define HAVE_MLOCK 1
#endif

using namespace mforms;

PasswordCache::PasswordCache()
{
  storage_len = 0;
  storage_size = 4*1024;
  storage = (char*)malloc(storage_size);
  if (!storage)
    log_error("Unable to allocate memory for password cache, caching will be disabled (errno %i)\n", errno);
#ifdef HAVE_MLOCK
  else
  {
    if (mlock(storage, storage_size) < 0)
    {
      log_error("mlock password cache (errno %i)\n", errno);
      free(storage);
      storage = NULL;
    }
  }
#endif
}

PasswordCache PasswordCache::instance;
static base::Mutex cache_mutex;

PasswordCache *PasswordCache::get()
{
  return &instance;
}

PasswordCache::~PasswordCache()
{
  if (storage)
  {
    memset(storage, 0, storage_size);
#ifdef HAVE_MLOCK
    if (munlock(storage, storage_size) < 0)
      log_error("munlock password cache failed (errno %i)\n", errno);
#endif
    free(storage);
  }
}

void PasswordCache::add_password(const std::string &service, const std::string &account, const char *password)
{
  if (storage)
  {
    if (!password) password = "";

    bool flag = false;
    {
      base::MutexLock lock(cache_mutex);

      const char *opassword = find_password(service, account);
      if (opassword)
      {
        if (strcmp(password, opassword) == 0)
          return;
        flag = true;
      }
    }
    if (flag)
      remove_password(service, account);

    base::MutexLock lock(cache_mutex);
    
    size_t reclen = sizeof(size_t) + service.size() + 1 + account.size() + 1 + strlen(password) + 1;
    
    // increase buffer size if new record doesnt fit
    while (storage_len + reclen > storage_size)
    {
      char *new_block;
      size_t new_size = storage_size + 4*1024;
      
      new_block = (char*)malloc(new_size);
      if (new_block)
      {
#ifdef HAVE_MLOCK
        if (mlock(new_block, new_size) < 0)
        {
          log_error("mlock password cache (errno %i)\n", errno);
          free(new_block);
          throw std::runtime_error("Could not increase password cache size");
        }
#endif
        memcpy(new_block, storage, storage_len);
        memset(storage, 0, storage_size);
#ifdef HAVE_MLOCK
        if (munlock(storage, storage_size) < 0)
          log_error("munlock password cache (errno %i)\n", errno);
#endif
        free(storage);
        storage = new_block;
        storage_size = new_size;
      }
      else
        throw std::runtime_error("Could not increase password cache size");
    }
    
    // store length of this record
    *(size_t*)(storage + storage_len) = reclen;
    storage_len += sizeof(reclen);
    // store contents
    memcpy(storage + storage_len, service.data(), service.size()+1);
    storage_len += service.size()+1;
    memcpy(storage + storage_len, account.data(), account.size()+1);
    storage_len += account.size()+1;
    memcpy(storage + storage_len, password, strlen(password)+1);
    storage_len += strlen(password)+1;
  }
  else
    throw std::runtime_error("Password storage is not available");
}

size_t PasswordCache::find_block(const std::string &service, const std::string &account)
{
  size_t offset = 0;
  while (offset < storage_len)
  {
    size_t recsize = *(size_t*)(storage+offset);
    const char *recservice = storage+offset+sizeof(recsize);
    const char *recaccount = storage+offset+sizeof(recsize)+strlen(recservice)+1;
    
    if (strcmp(recservice, service.c_str()) == 0 &&
        strcmp(recaccount, account.c_str()) == 0)
      return offset;
    
    offset += recsize;
  }
  return (size_t)-1;
}

void PasswordCache::remove_password(const std::string &service, const std::string &account)
{
  if (storage)
  {
    base::MutexLock lock(cache_mutex);
    
    size_t offset = find_block(service, account);
    if (offset != (size_t)-1)
    {
      size_t recsize = *(size_t*)(storage+offset);
      
      memmove(storage+offset, storage+offset+recsize, storage_len-recsize);
      storage_len -= recsize;
    }
  }
}

const char *PasswordCache::find_password(const std::string &service, const std::string &account)
{
  if (storage)
  {
    size_t offset = find_block(service, account);
    if (offset != (size_t)-1)
    {
      const char *pwd = storage + offset + sizeof(size_t) + service.size()+1 + account.size()+1;
      return pwd;
    }
  }
  return 0;
}


bool PasswordCache::get_password(const std::string &service, const std::string &account, std::string &ret_password)
{
  base::MutexLock lock(cache_mutex);
  const char *tmp = find_password(service, account);
  if (tmp)
    ret_password = tmp;
  return tmp != NULL;
}