From: David Howells <dhowells-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
To: James.Bottomley-d9PhHud1JfjCXq6kfMZ53/egYHeGw8Jk@public.gmane.org,
ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org
Cc: linux-nfs-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org,
linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
dhowells-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org,
keyrings-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
linux-fsdevel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Example daemon program
Date: Tue, 30 May 2017 17:13:10 +0100 [thread overview]
Message-ID: <10295.1496160790@warthog.procyon.org.uk> (raw)
In-Reply-To: <149616052408.10194.7774163568767478808.stgit-S6HVgzuS8uM4Awkfq6JHfwNdhmdF6hFW@public.gmane.org>
Here's a one-shot example daemon that services user-type keys.
David
---
/* request_key() service daemon
*
* Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public Licence
* as published by the Free Software Foundation; either version
* 2 of the Licence, or (at your option) any later version.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <keyutils.h>
#define KEY_SERVICE_NS_UTS 0x0001
#define KEY_SERVICE_NS_IPC 0x0002
#define KEY_SERVICE_NS_MNT 0x0004
#define KEY_SERVICE_NS_PID 0x0008
#define KEY_SERVICE_NS_NET 0x0010
#define KEY_SERVICE_NS_CGROUP 0x0020
#define KEY_SERVICE___ALL_NS 0x003f
#define KEY_SERVICE_FD_CLOEXEC 0x0001
#define KEY_SERVICE_FD_NONBLOCK 0x0002
#define KEYCTL_SERVICE_CREATE 30 /* Create a request_key() service channel */
#define KEYCTL_SERVICE_ADD 31 /* Specify the a request pattern we can service */
int main()
{
key_serial_t key, tring, pring, sring;
char buf[128], op[12];
uid_t uid;
gid_t gid;
int kfd, len;
kfd = keyctl(KEYCTL_SERVICE_CREATE, KEY_SERVICE_FD_CLOEXEC);
if (kfd == -1) {
perror("service-create");
exit(1);
}
if (keyctl(KEYCTL_SERVICE_ADD, kfd, "user", 0) == -1) {
perror("service-add");
exit(1);
}
len = read(kfd, buf, sizeof(buf) - 1);
if (len == -1) {
perror("read");
exit(1);
}
buf[len] = 0;
printf("received %d [%s]\n", len, buf);
if (sscanf(buf, "%12s %x %x %x %x %x %x",
op, &key, &uid, &gid, &tring, &pring, &sring) != 7) {
fprintf(stderr, "sscanf: Insufficient args decoded\n");
exit(1);
}
if (keyctl_assume_authority(key) == -1) {
perror("assume");
exit(1);
}
if (keyctl_instantiate(key, "foo_bar", 7, 0) == -1) {
perror("instantiate");
exit(1);
}
exit(0);
}
next prev parent reply other threads:[~2017-05-30 16:13 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-30 16:08 [RFC PATCH] KEYS: Allow a live daemon in a namespace to service request_key upcalls David Howells
[not found] ` <149616052408.10194.7774163568767478808.stgit-S6HVgzuS8uM4Awkfq6JHfwNdhmdF6hFW@public.gmane.org>
2017-05-30 16:13 ` David Howells [this message]
2017-05-31 13:59 ` Colin Walters
[not found] ` <1496239145.289295.994170312.57409998-2RFepEojUI2N1INw9kWLP6GC3tUn3ZHUQQ4Iyu8u01E@public.gmane.org>
2017-05-31 14:47 ` David Howells
[not found] ` <3412.1496242065-S6HVgzuS8uM4Awkfq6JHfwNdhmdF6hFW@public.gmane.org>
2017-05-31 15:36 ` Colin Walters
[not found] ` <1496418474.13822.6.camel-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2017-06-02 16:14 ` David Howells
[not found] ` <1496244979.313075.994296480.7C5735E8-2RFepEojUI2N1INw9kWLP6GC3tUn3ZHUQQ4Iyu8u01E@public.gmane.org>
2017-06-02 15:47 ` Jeff Layton
2017-06-02 16:34 ` David Howells
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=10295.1496160790@warthog.procyon.org.uk \
--to=dhowells-h+wxahxf7alqt0dzr+alfa@public.gmane.org \
--cc=James.Bottomley-d9PhHud1JfjCXq6kfMZ53/egYHeGw8Jk@public.gmane.org \
--cc=cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org \
--cc=ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org \
--cc=keyrings-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=linux-fsdevel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=linux-nfs-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox