Debian Codegraph @

pkcs11/rpc-layer/gkm-rpc-daemon-standalone.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* gkm-rpc-daemon-standalone.c - A sample daemon.
3
4 Copyright (C) 2008, Stef Walter
5
6 The Gnome Keyring Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 The Gnome Keyring Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the Gnome Library; see the file COPYING.LIB. If not,
18 <http://www.gnu.org/licenses/>.
19
20 Author: Stef Walter <[email protected]>
21 */
22
23 #include "config.h"
24
25 #include "pkcs11/pkcs11.h"
26
27 #include "gkm-rpc-layer.h"
28
29 #include <stdio.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <dlfcn.h>
36 #include <pthread.h>
37 #include <sys/select.h>
38
39 #define SOCKET_PATH "/tmp/gkm-rpc-daemon.sock"
40
41 /* Sample configuration for loading NSS remotely */
42 static CK_C_INITIALIZE_ARGS p11_init_args = {
43 NULL,
44 NULL,
45 NULL,
46 NULL,
47 CKF_OS_LOCKING_OK,
48 "init-string = configdir='/tmp' certPrefix='' keyPrefix='' secmod='/tmp/secmod.db' flags="
49 };
50
51 static int is_running = 1;
52
53 static int
54 usage (void)
55 {
56 fprintf (stderr, "usage: gkm-rpc-daemon pkcs11-module\n");
57 exit (2);
58 }
59
60 int
61 main (int argc, char *argv[])
62 {
63 CK_C_GetFunctionList func_get_list;
64 CK_FUNCTION_LIST_PTR funcs;
65 void *module;
66 fd_set read_fds;
67 int sock, ret;
68 CK_RV rv;
69
70 /* The module to load is the argument */
71 if (argc != 2)
72 usage();
73
74 /* Load the library */
75 module = dlopen(argv[1], RTLD_NOW);
76 if(!module) {
77 fprintf (stderr, "couldn't open library: %s: %s\n", argv[1], dlerror());
78 exit (1);
79 }
80
81 /* Lookup the appropriate function in library */
82 func_get_list = (CK_C_GetFunctionList)dlsym (module, "C_GetFunctionList");
83 if (!func_get_list) {
84 fprintf (stderr, "couldn't find C_GetFunctionList in library: %s: %s\n", argv[1], dlerror());
85 exit (1);
86 }
87
88 /* Get the function list */
89 rv = (func_get_list) (&funcs);
90 if (rv != CKR_OK || !funcs) {
91 fprintf (stderr, "couldn't get function list from C_GetFunctionList in libary: %s: 0x%08x\n",
92 argv[1], (int)rv);
93 exit (1);
94 }
95
96 /* RPC layer expects initialized module */
97 rv = (funcs->C_Initialize) (&p11_init_args);
98 if (rv != CKR_OK) {
99 fprintf (stderr, "couldn't initialize module: %s: 0x%08x\n", argv[1], (int)rv);
100 exit (1);
101 }
102
103 gkm_rpc_layer_initialize (funcs);
104 sock = gkm_rpc_layer_startup (SOCKET_PATH);
105 if (sock == -1)
106 exit (1);
107
108 is_running = 1;
109 while (is_running) {
110 FD_ZERO (&read_fds);
111 FD_SET (sock, &read_fds);
112 ret = select (sock + 1, &read_fds, NULL, NULL, NULL);
113 if (ret < 0) {
114 if (errno == EINTR)
115 continue;
116 fprintf (stderr, "error watching socket: %s\n", strerror (errno));
117 exit (1);
118 }
119
120 if (FD_ISSET (sock, &read_fds))
121 gkm_rpc_layer_accept ();
122 }
123
124 gkm_rpc_layer_shutdown ();
125 gkm_rpc_layer_uninitialize ();
126
127 rv = (funcs->C_Finalize) (NULL);
128 if (rv != CKR_OK)
129 fprintf (stderr, "couldn't finalize module: %s: 0x%08x\n", argv[1], (int)rv);
130
131 dlclose(module);
132
133 return 0;
134 }