228
/*****************************************************************************/
230
* plain realloc is just crazy
232
static void* __xrealloc(void* ptr, size_t size)
236
ret = realloc(ptr, size);
245
227
long keyctl_invalidate(key_serial_t id)
247
229
return keyctl(KEYCTL_INVALIDATE, id);
232
long keyctl_get_persistent(uid_t uid, key_serial_t id)
234
return keyctl(KEYCTL_GET_PERSISTENT, uid, id);
250
237
/*****************************************************************************/
252
239
* fetch key description into an allocated buffer
266
buf = malloc(buflen);
254
buf = malloc(buflen);
271
258
ret = keyctl_describe(id, buf, buflen);
277
264
if (buflen >= ret)
281
buf = __xrealloc(buf, buflen);
289
} /* end keyctl_describe_alloc() */
291
273
/*****************************************************************************/
297
279
int keyctl_read_alloc(key_serial_t id, void **_buffer)
300
282
long buflen, ret;
302
284
ret = keyctl_read(id, NULL, 0);
307
buf = malloc(buflen + 1);
290
buf = malloc(buflen + 1);
312
294
ret = keyctl_read(id, buf, buflen);
318
300
if (buflen >= ret)
322
buf = __xrealloc(buf, buflen + 1);
327
((unsigned char *) buf)[buflen] = 0;
331
} /* end keyctl_read_alloc() */
333
310
/*****************************************************************************/
430
* Find a key by type and description
432
key_serial_t find_key_by_type_and_desc(const char *type, const char *desc,
433
key_serial_t destringid)
435
key_serial_t id, error;
437
char buf[1024], typebuf[40], rdesc[1024], *kdesc, *cp;
442
id = request_key(type, desc, NULL, destringid);
443
if (id >= 0 || errno == ENOMEM)
450
f = fopen("/proc/keys", "r");
452
fprintf(stderr, "libkeyutils: Can't open /proc/keys: %m\n");
456
while (fgets(buf, sizeof(buf), f)) {
457
cp = strchr(buf, '\n');
462
n = sscanf(buf, "%x %*s %*u %*s %*x %*d %*d %s %n",
463
&id, typebuf, &ndesc);
464
if (n == 2 && ndesc > 0 && ndesc <= cp - buf) {
465
if (strcmp(typebuf, type) != 0)
468
if (memcmp(kdesc, desc, dlen) != 0)
470
if (kdesc[dlen] != ':' &&
471
kdesc[dlen] != '\0' &&
476
/* The key type appends extra stuff to the end of the
477
* description after a colon in /proc/keys. Colons,
478
* however, are allowed in descriptions, so we need to
479
* make a further check. */
480
n = keyctl_describe(id, rdesc, sizeof(rdesc) - 1);
487
if (n >= sizeof(rdesc) - 1)
491
cp = strrchr(rdesc, ';');
495
if (strcmp(cp, desc) != 0)
501
keyctl_link(id, destringid) == -1)
454
513
#ifdef NO_GLIBC_KEYERR
455
514
/*****************************************************************************/