~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-updates

« back to all changes in this revision

Viewing changes to agent/cache.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Urlichs
  • Date: 2005-12-08 22:13:21 UTC
  • mto: (5.1.1 edgy)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20051208221321-d54343ca8hlwzkac
Tags: upstream-1.9.19
ImportĀ upstreamĀ versionĀ 1.9.19

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
  int ttl;  /* max. lifetime given in seconds, -1 one means infinite */
43
43
  int lockcount;
44
44
  struct secret_data_s *pw;
 
45
  cache_mode_t cache_mode;
45
46
  char key[1];
46
47
};
47
48
 
78
79
}
79
80
 
80
81
 
 
82
 
81
83
/* check whether there are items to expire */
82
84
static void
83
85
housekeeping (void)
85
87
  ITEM r, rprev;
86
88
  time_t current = gnupg_get_time ();
87
89
 
88
 
  /* first expire the actual data */
 
90
  /* First expire the actual data */
89
91
  for (r=thecache; r; r = r->next)
90
92
    {
91
93
      if (!r->lockcount && r->pw
100
102
        }
101
103
    }
102
104
 
103
 
  /* second, make sure that we also remove them based on the created stamp so
104
 
     that the user has to enter it from time to time.  We do this every hour */
 
105
  /* Second, make sure that we also remove them based on the created stamp so
 
106
     that the user has to enter it from time to time. */
105
107
  for (r=thecache; r; r = r->next)
106
108
    {
107
 
      if (!r->lockcount && r->pw && r->created + opt.max_cache_ttl < current)
 
109
      unsigned long maxttl;
 
110
      
 
111
      switch (r->cache_mode)
 
112
        {
 
113
        case CACHE_MODE_SSH: maxttl = opt.max_cache_ttl_ssh; break;
 
114
        default: maxttl = opt.max_cache_ttl; break;
 
115
        }
 
116
      if (!r->lockcount && r->pw && r->created + maxttl < current)
108
117
        {
109
118
          if (DBG_CACHE)
110
119
            log_debug ("  expired `%s' (%lus after creation)\n",
115
124
        }
116
125
    }
117
126
 
118
 
  /* third, make sure that we don't have too many items in the list.
 
127
  /* Third, make sure that we don't have too many items in the list.
119
128
     Expire old and unused entries after 30 minutes */
120
129
  for (rprev=NULL, r=thecache; r; )
121
130
    {
186
195
   with a maximum lifetime of TTL seconds.  If there is already data
187
196
   under this key, it will be replaced.  Using a DATA of NULL deletes
188
197
   the entry.  A TTL of 0 is replaced by the default TTL and a TTL of
189
 
   -1 set infinite timeout. */
 
198
   -1 set infinite timeout. CACHE_MODE is stored with the cache entry
 
199
   and used t select different timeouts. */
190
200
int
191
 
agent_put_cache (const char *key, const char *data, int ttl)
 
201
agent_put_cache (const char *key, cache_mode_t cache_mode,
 
202
                 const char *data, int ttl)
192
203
{
193
204
  ITEM r;
194
205
 
195
206
  if (DBG_CACHE)
196
 
    log_debug ("agent_put_cache `%s' requested ttl=%d\n", key, ttl);
 
207
    log_debug ("agent_put_cache `%s' requested ttl=%d mode=%d\n",
 
208
               key, ttl, cache_mode);
197
209
  housekeeping ();
198
210
 
199
211
  if (!ttl)
200
 
    ttl = opt.def_cache_ttl;
201
 
  if (!ttl)
 
212
    {
 
213
      switch(cache_mode)
 
214
        {
 
215
        case CACHE_MODE_SSH: ttl = opt.def_cache_ttl_ssh; break;
 
216
        default: ttl = opt.def_cache_ttl; break;
 
217
        }
 
218
    }
 
219
  if (!ttl || cache_mode == CACHE_MODE_IGNORE)
202
220
    return 0;
203
221
 
204
222
  for (r=thecache; r; r = r->next)
217
235
        {
218
236
          r->created = r->accessed = gnupg_get_time (); 
219
237
          r->ttl = ttl;
 
238
          r->cache_mode = cache_mode;
220
239
          r->pw = new_data (data, strlen (data)+1);
221
240
          if (!r->pw)
222
241
            log_error ("out of core while allocating new cache item\n");
232
251
          strcpy (r->key, key);
233
252
          r->created = r->accessed = gnupg_get_time (); 
234
253
          r->ttl = ttl;
 
254
          r->cache_mode = cache_mode;
235
255
          r->pw = new_data (data, strlen (data)+1);
236
256
          if (!r->pw)
237
257
            {
249
269
}
250
270
 
251
271
 
252
 
/* Try to find an item in the cache */
 
272
/* Try to find an item in the cache.  Note that we currently don't
 
273
   make use of CACHE_MODE.  */
253
274
const char *
254
 
agent_get_cache (const char *key, void **cache_id)
 
275
agent_get_cache (const char *key, cache_mode_t cache_mode, void **cache_id)
255
276
{
256
277
  ITEM r;
257
278
 
 
279
  if (cache_mode == CACHE_MODE_IGNORE)
 
280
    return NULL;
 
281
 
258
282
  if (DBG_CACHE)
259
283
    log_debug ("agent_get_cache `%s'...\n", key);
260
284
  housekeeping ();