"Fossies" - the Fresh Open Source Software Archive

Member "cryptofs-0.6.0/src/cryptofs/pinentry.c" (29 Jun 2006, 2602 Bytes) of package /linux/misc/old/cryptofs-0.6.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "pinentry.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) 2006 Christoph Hohmann
    3  *
    4  * This program is free software; you can redistribute it and/or modify
    5  * it under the terms of the GNU General Public License as published by
    6  * the Free Software Foundation; either version 2 of the License, or
    7  * (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   17  */
   18 
   19 #include <stdio.h>
   20 #include <unistd.h>
   21 #include <string.h>
   22 
   23 #include <glib.h>
   24 
   25 #include "utils.h"
   26 #include "passwordquery.h"
   27 
   28 char *args[] = {
   29     "pinentry",
   30     NULL,
   31 };
   32 
   33 char *getpin(char *desc, char *prompt)
   34 {
   35     int childin, childout;
   36     char *out, *ret = NULL;
   37     int state = 0;
   38     gboolean exit = FALSE;
   39     GPid pid;
   40 
   41     if (!g_spawn_async_with_pipes(NULL, args, NULL, G_SPAWN_STDERR_TO_DEV_NULL | G_SPAWN_SEARCH_PATH, NULL, NULL, &pid, &childin, &childout, NULL, NULL))
   42     return NULL;
   43 
   44     while(!exit && ((out = reads(childout)) != NULL)) {
   45      /* printf("%s", out); */
   46 
   47     if (strncmp(out, "OK\n", 3) == 0 || strncmp(out, "OK ", 3) == 0)
   48     {
   49         switch(state) {
   50         case 0:
   51         writes(childin, "SETDESC ");
   52         writes(childin, desc);
   53         writes(childin, "\n");
   54         break;
   55         case 1:
   56         writes(childin, "SETPROMPT ");
   57         writes(childin, prompt);
   58         writes(childin, "\n");
   59         break;
   60         case 2: 
   61         {
   62         char *tty;
   63 
   64         tty = ttyname(0);
   65         if (tty != NULL) {
   66             writes(childin, "OPTION ttyname=");
   67             writes(childin, tty);
   68             writes(childin, "\n");
   69             break; 
   70         }
   71         state++;
   72         }
   73         case 3:
   74         writes(childin, "GETPIN\n");
   75         break;
   76         case 4:
   77         exit = TRUE;
   78         }
   79         state++;
   80     } else if (strncmp(out, "D ", 2) == 0) {
   81         if (state == 4) {
   82         g_free(ret);
   83             ret = g_strndup(out + 2, strlen(out) - 3);
   84         }
   85     } else {
   86         g_free(out);
   87         break;
   88     }
   89     g_free(out);
   90     }
   91 
   92     g_spawn_close_pid(pid);
   93     close(childin);
   94     close(childout);
   95 
   96     return ret;
   97 }
   98 
   99 static char *getPassword(void)
  100 {
  101     return getpin("Enter password for filesystem", "Password:");
  102 }
  103 
  104 static void freePassword(char *password)
  105 {
  106     memset(password, '\0', strlen(password));
  107     g_free(password);
  108 }
  109 
  110 static PasswordQuery query =
  111 {
  112     getPassword,
  113     freePassword
  114 };
  115 
  116 PasswordQuery *getDefaultPasswordQuery(void)
  117 {
  118     return &query;
  119 }