1 /* sh-cmd.c - The Assuan server for g13-syshelp
2 * Copyright (C) 2015 Werner Koch
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #include "g13-syshelp.h"
34 /* Local data for this server module. A pointer to this is stored in
35 the CTRL object of each connection. */
38 /* The Assuan contect we are working on. */
39 assuan_context_t assuan_ctx;
41 /* The malloced name of the device. */
44 /* A stream open for read of the device set by the DEVICE command or
45 NULL if no DEVICE command has been used. */
52 /* Local prototypes. */
61 /* Set an error and a description. */
62 #define set_error(e,t) assuan_set_error (ctx, gpg_error (e), (t))
63 #define set_error_fail_cmd() set_error (GPG_ERR_NOT_INITIALIZED, \
64 "not called via userv or unknown user")
67 /* Skip over options. Blanks after the options are also removed. */
69 skip_options (const char *line)
73 while ( *line == '-' && line[1] == '-' )
75 while (*line && !spacep (line))
84 /* Check whether the option NAME appears in LINE. */
86 /* has_option (const char *line, const char *name) */
89 /* int n = strlen (name); */
91 /* s = strstr (line, name); */
92 /* if (s && s >= skip_options (line)) */
94 /* return (s && (s == line || spacep (s-1)) && (!s[n] || spacep (s+n))); */
98 /* Helper to print a message while leaving a command. */
100 leave_cmd (assuan_context_t ctx, gpg_error_t err)
104 const char *name = assuan_get_command_name (ctx);
107 if (gpg_err_source (err) == GPG_ERR_SOURCE_DEFAULT)
108 log_error ("command '%s' failed: %s\n", name,
111 log_error ("command '%s' failed: %s <%s>\n", name,
112 gpg_strerror (err), gpg_strsource (err));
120 /* The handler for Assuan OPTION commands. */
122 option_handler (assuan_context_t ctx, const char *key, const char *value)
124 ctrl_t ctrl = assuan_get_pointer (ctx);
131 if (ctrl->fail_all_cmds)
132 err = set_error_fail_cmd ();
134 err = gpg_error (GPG_ERR_UNKNOWN_OPTION);
140 /* The handler for an Assuan RESET command. */
142 reset_notify (assuan_context_t ctx, char *line)
144 ctrl_t ctrl = assuan_get_pointer (ctx);
148 xfree (ctrl->server_local->devicename);
149 ctrl->server_local->devicename = NULL;
150 es_fclose (ctrl->server_local->devicefp);
151 ctrl->server_local->devicefp = NULL;
154 assuan_close_input_fd (ctx);
155 assuan_close_output_fd (ctx);
160 static const char hlp_device[] =
163 "Set the device used by further commands.\n"
164 "A device name or a PARTUUID string may be used.\n"
165 "Access to that device (by the g13 system) is locked\n"
166 "until a new DEVICE command or end of this process\n";
168 cmd_device (assuan_context_t ctx, char *line)
170 ctrl_t ctrl = assuan_get_pointer (ctx);
175 line = skip_options (line);
177 /* # warning hardwired to /dev/sdb1 ! */
178 /* if (strcmp (line, "/dev/sdb1")) */
180 /* err = gpg_error (GPG_ERR_ENOENT); */
184 /* Always close an open device stream of this session. */
185 xfree (ctrl->server_local->devicename);
186 ctrl->server_local->devicename = NULL;
187 es_fclose (ctrl->server_local->devicefp);
188 ctrl->server_local->devicefp = NULL;
190 /* Are we allowed to use the given device? */
191 for (ti=ctrl->client.tab; ti; ti = ti->next)
192 if (!strcmp (line, ti->blockdev))
196 err = set_error (GPG_ERR_EACCES, "device not configured for user");
200 ctrl->server_local->devicename = xtrystrdup (line);
201 if (!ctrl->server_local->devicename)
203 err = gpg_error_from_syserror ();
208 /* Check whether we have permissions to open the device and keep an
210 fp = es_fopen (ctrl->server_local->devicename, "rb");
213 err = gpg_error_from_syserror ();
214 log_error ("error opening '%s': %s\n",
215 ctrl->server_local->devicename, gpg_strerror (err));
219 es_fclose (ctrl->server_local->devicefp);
220 ctrl->server_local->devicefp = fp;
224 /* Fixme: Take some kind of lock. */
230 xfree (ctrl->server_local->devicename);
231 ctrl->server_local->devicename = NULL;
234 return leave_cmd (ctx, err);
238 static const char hlp_create[] =
241 "Create a new encrypted partition on the current device.\n"
242 "<type> must be \"dm-crypt\" for now.";
244 cmd_create (assuan_context_t ctx, char *line)
246 ctrl_t ctrl = assuan_get_pointer (ctx);
250 line = skip_options (line);
251 if (strcmp (line, "dm-crypt"))
253 err = set_error (GPG_ERR_INV_ARG, "Type must be \"dm-crypt\"");
257 if (!ctrl->server_local->devicename
258 || !ctrl->server_local->devicefp
261 err = set_error (GPG_ERR_ENOENT, "No device has been set");
265 err = sh_is_empty_partition (ctrl->server_local->devicename);
268 if (gpg_err_code (err) == GPG_ERR_FALSE)
269 err = gpg_error (GPG_ERR_CONFLICT);
270 err = assuan_set_error (ctx, err, "Partition is not empty");
274 /* We need a writeable stream to create the container. */
275 fp = es_fopen (ctrl->server_local->devicename, "r+b");
278 err = gpg_error_from_syserror ();
279 log_error ("error opening '%s': %s\n",
280 ctrl->server_local->devicename, gpg_strerror (err));
283 if (es_setvbuf (fp, NULL, _IONBF, 0))
285 err = gpg_error_from_syserror ();
286 log_error ("error setting '%s' to _IONBF: %s\n",
287 ctrl->server_local->devicename, gpg_strerror (err));
291 err = sh_dmcrypt_create_container (ctrl,
292 ctrl->server_local->devicename,
296 gpg_error_t err2 = gpg_error_from_syserror ();
297 log_error ("error closing '%s': %s\n",
298 ctrl->server_local->devicename, gpg_strerror (err2));
306 return leave_cmd (ctx, err);
310 static const char hlp_mount[] =
313 "Mount an encrypted partition on the current device.\n"
314 "<type> must be \"dm-crypt\" for now.";
316 cmd_mount (assuan_context_t ctx, char *line)
318 ctrl_t ctrl = assuan_get_pointer (ctx);
320 unsigned char *keyblob = NULL;
322 tupledesc_t tuples = NULL;
324 line = skip_options (line);
326 if (strcmp (line, "dm-crypt"))
328 err = set_error (GPG_ERR_INV_ARG, "Type must be \"dm-crypt\"");
332 if (!ctrl->server_local->devicename
333 || !ctrl->server_local->devicefp
336 err = set_error (GPG_ERR_ENOENT, "No device has been set");
340 err = sh_is_empty_partition (ctrl->server_local->devicename);
343 err = gpg_error (GPG_ERR_ENODEV);
344 assuan_set_error (ctx, err, "Partition is empty");
349 /* We expect that the client already decrypted the keyblob.
350 * Eventually we should move reading of the keyblob to here and ask
351 * the client to decrypt it. */
352 assuan_begin_confidential (ctx);
353 err = assuan_inquire (ctx, "KEYBLOB",
354 &keyblob, &keybloblen, 4 * 1024);
355 assuan_end_confidential (ctx);
358 log_error (_("assuan_inquire failed: %s\n"), gpg_strerror (err));
361 err = create_tupledesc (&tuples, keyblob, keybloblen);
366 if (gpg_err_code (err) == GPG_ERR_NOT_SUPPORTED)
367 log_error ("unknown keyblob version received\n");
371 err = sh_dmcrypt_mount_container (ctrl,
372 ctrl->server_local->devicename,
377 destroy_tupledesc (tuples);
378 return leave_cmd (ctx, err);
382 static const char hlp_suspend[] =
385 "Suspend an encrypted partition and wipe the key.\n"
386 "<type> must be \"dm-crypt\" for now.";
388 cmd_suspend (assuan_context_t ctx, char *line)
390 ctrl_t ctrl = assuan_get_pointer (ctx);
393 line = skip_options (line);
395 if (strcmp (line, "dm-crypt"))
397 err = set_error (GPG_ERR_INV_ARG, "Type must be \"dm-crypt\"");
401 if (!ctrl->server_local->devicename
402 || !ctrl->server_local->devicefp
405 err = set_error (GPG_ERR_ENOENT, "No device has been set");
409 err = sh_is_empty_partition (ctrl->server_local->devicename);
412 err = gpg_error (GPG_ERR_ENODEV);
413 assuan_set_error (ctx, err, "Partition is empty");
418 err = sh_dmcrypt_suspend_container (ctrl, ctrl->server_local->devicename);
421 return leave_cmd (ctx, err);
425 static const char hlp_resume[] =
428 "Resume an encrypted partition and set the key.\n"
429 "<type> must be \"dm-crypt\" for now.";
431 cmd_resume (assuan_context_t ctx, char *line)
433 ctrl_t ctrl = assuan_get_pointer (ctx);
435 unsigned char *keyblob = NULL;
437 tupledesc_t tuples = NULL;
439 line = skip_options (line);
441 if (strcmp (line, "dm-crypt"))
443 err = set_error (GPG_ERR_INV_ARG, "Type must be \"dm-crypt\"");
447 if (!ctrl->server_local->devicename
448 || !ctrl->server_local->devicefp
451 err = set_error (GPG_ERR_ENOENT, "No device has been set");
455 err = sh_is_empty_partition (ctrl->server_local->devicename);
458 err = gpg_error (GPG_ERR_ENODEV);
459 assuan_set_error (ctx, err, "Partition is empty");
464 /* We expect that the client already decrypted the keyblob.
465 * Eventually we should move reading of the keyblob to here and ask
466 * the client to decrypt it. */
467 assuan_begin_confidential (ctx);
468 err = assuan_inquire (ctx, "KEYBLOB",
469 &keyblob, &keybloblen, 4 * 1024);
470 assuan_end_confidential (ctx);
473 log_error (_("assuan_inquire failed: %s\n"), gpg_strerror (err));
476 err = create_tupledesc (&tuples, keyblob, keybloblen);
481 if (gpg_err_code (err) == GPG_ERR_NOT_SUPPORTED)
482 log_error ("unknown keyblob version received\n");
486 err = sh_dmcrypt_resume_container (ctrl,
487 ctrl->server_local->devicename,
492 destroy_tupledesc (tuples);
493 return leave_cmd (ctx, err);
497 static const char hlp_getinfo[] =
500 "Multipurpose function to return a variety of information.\n"
501 "Supported values for WHAT are:\n"
503 " version - Return the version of the program.\n"
504 " pid - Return the process id of the server.\n"
505 " showtab - Show the table for the user.";
507 cmd_getinfo (assuan_context_t ctx, char *line)
509 ctrl_t ctrl = assuan_get_pointer (ctx);
513 if (!strcmp (line, "version"))
515 const char *s = PACKAGE_VERSION;
516 err = assuan_send_data (ctx, s, strlen (s));
518 else if (!strcmp (line, "pid"))
522 snprintf (numbuf, sizeof numbuf, "%lu", (unsigned long)getpid ());
523 err = assuan_send_data (ctx, numbuf, strlen (numbuf));
525 else if (!strncmp (line, "getsz", 5))
527 unsigned long long nblocks;
528 err = sh_blockdev_getsz (line+6, &nblocks);
530 log_debug ("getsz=%llu\n", nblocks);
532 else if (!strcmp (line, "showtab"))
536 for (ti=ctrl->client.tab; !err && ti; ti = ti->next)
538 buf = es_bsprintf ("%s %s%s %s %s%s\n",
540 *ti->blockdev=='/'? "":"partuuid=",
542 ti->label? ti->label : "-",
543 ti->mountpoint? " ":"",
544 ti->mountpoint? ti->mountpoint:"");
546 err = gpg_error_from_syserror ();
549 err = assuan_send_data (ctx, buf, strlen (buf));
551 err = assuan_send_data (ctx, NULL, 0); /* Flush */
557 err = set_error (GPG_ERR_ASS_PARAMETER, "unknown value for WHAT");
559 return leave_cmd (ctx, err);
563 /* This command handler is used for all commands if this process has
564 not been started as expected. */
566 fail_command (assuan_context_t ctx, char *line)
569 const char *name = assuan_get_command_name (ctx);
576 err = set_error_fail_cmd ();
577 log_error ("command '%s' failed: %s\n", name, gpg_strerror (err));
582 /* Tell the Assuan library about our commands. */
584 register_commands (assuan_context_t ctx, int fail_all)
588 assuan_handler_t handler;
589 const char * const help;
591 { "DEVICE", cmd_device, hlp_device },
592 { "CREATE", cmd_create, hlp_create },
593 { "MOUNT", cmd_mount, hlp_mount },
594 { "SUSPEND", cmd_suspend,hlp_suspend},
595 { "RESUME", cmd_resume, hlp_resume },
598 { "GETINFO", cmd_getinfo, hlp_getinfo },
604 for (i=0; table[i].name; i++)
606 err = assuan_register_command (ctx, table[i].name,
607 fail_all ? fail_command : table[i].handler,
616 /* Startup the server. */
618 syshelp_server (ctrl_t ctrl)
621 assuan_fd_t filedes[2];
622 assuan_context_t ctx = NULL;
624 /* We use a pipe based server so that we can work from scripts.
625 assuan_init_pipe_server will automagically detect when we are
626 called with a socketpair and ignore FILEDES in this case. */
627 filedes[0] = assuan_fdopen (0);
628 filedes[1] = assuan_fdopen (1);
629 err = assuan_new (&ctx);
632 log_error ("failed to allocate an Assuan context: %s\n",
637 err = assuan_init_pipe_server (ctx, filedes);
640 log_error ("failed to initialize the server: %s\n", gpg_strerror (err));
644 err = register_commands (ctx, 0 /*FIXME:ctrl->fail_all_cmds*/);
647 log_error ("failed to the register commands with Assuan: %s\n",
652 assuan_set_pointer (ctx, ctrl);
655 char *tmp = xtryasprintf ("G13-syshelp %s ready to serve requests "
658 (unsigned long)ctrl->client.uid,
662 assuan_set_hello_line (ctx, tmp);
667 assuan_register_reset_notify (ctx, reset_notify);
668 assuan_register_option_handler (ctx, option_handler);
670 ctrl->server_local = xtrycalloc (1, sizeof *ctrl->server_local);
671 if (!ctrl->server_local)
673 err = gpg_error_from_syserror ();
676 ctrl->server_local->assuan_ctx = ctx;
678 while ( !(err = assuan_accept (ctx)) )
680 err = assuan_process (ctx);
682 log_info ("Assuan processing failed: %s\n", gpg_strerror (err));
687 log_info ("Assuan accept problem: %s\n", gpg_strerror (err));
690 reset_notify (ctx, NULL); /* Release all items hold by SERVER_LOCAL. */
691 if (ctrl->server_local)
693 xfree (ctrl->server_local);
694 ctrl->server_local = NULL;
697 assuan_release (ctx);
703 sh_encrypt_keyblob (ctrl_t ctrl, const void *keyblob, size_t keybloblen,
704 char **r_enckeyblob, size_t *r_enckeybloblen)
706 assuan_context_t ctx = ctrl->server_local->assuan_ctx;
708 unsigned char *enckeyblob;
709 size_t enckeybloblen;
711 *r_enckeyblob = NULL;
713 /* Send the plaintext. */
714 err = g13_status (ctrl, STATUS_PLAINTEXT_FOLLOWS, NULL);
717 assuan_begin_confidential (ctx);
718 err = assuan_send_data (ctx, keyblob, keybloblen);
720 err = assuan_send_data (ctx, NULL, 0);
721 assuan_end_confidential (ctx);
723 err = assuan_write_line (ctx, "END");
726 log_error (_("error sending data: %s\n"), gpg_strerror (err));
730 /* Inquire the ciphertext. */
731 err = assuan_inquire (ctx, "ENCKEYBLOB",
732 &enckeyblob, &enckeybloblen, 16 * 1024);
735 log_error (_("assuan_inquire failed: %s\n"), gpg_strerror (err));
739 *r_enckeyblob = enckeyblob;
740 *r_enckeybloblen = enckeybloblen;
745 /* Send a status line with status ID NO. The arguments are a list of
746 strings terminated by a NULL argument. */
748 g13_status (ctrl_t ctrl, int no, ...)
754 va_start (arg_ptr, no);
758 assuan_context_t ctx = ctrl->server_local->assuan_ctx;
764 while ( (text = va_arg (arg_ptr, const char *)) )
771 for ( ; *text && n < DIM (buf)-2; n++)
775 err = assuan_write_status (ctx, get_status_string (no), buf);