2 * Copyright (C) 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 * This file is part of GnuTLS.
6 * GnuTLS is free software: you can redistribute it and/or modify it
7 * 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 * GnuTLS is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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
18 * <http://www.gnu.org/licenses/>.
23 /* Gnulib portability files. */
24 #include <version-etc.h>
32 main (int argc, char **argv)
34 printf ("\nPSK not supported. This program is a dummy.\n\n");
43 #include <gnutls/gnutls.h>
44 #include <gnutls/extra.h>
47 #include "../lib/random.h" /* for random */
49 #include <sys/types.h>
59 /* Gnulib portability files. */
63 static int write_key (const char *username, const char *key, int key_size,
66 #define KPASSWD "/etc/passwd.psk"
67 #define MAX_KEY_SIZE 64
69 main (int argc, char **argv)
76 unsigned char key[MAX_KEY_SIZE];
77 char hex_key[MAX_KEY_SIZE * 2 + 1];
79 size_t hex_key_size = sizeof (hex_key);
81 set_program_name (argv[0]);
83 if ((ret = gnutls_global_init ()) < 0)
85 fprintf (stderr, "global_init: %s\n", gnutls_strerror (ret));
91 if (gaa (argc, argv, &info) != -1)
93 fprintf (stderr, "Error in the arguments.\n");
97 if (info.passwd == NULL)
98 info.passwd = (char *) KPASSWD;
100 if (info.username == NULL)
103 pwd = getpwuid (getuid ());
107 fprintf (stderr, "No such user\n");
111 info.username = pwd->pw_name;
113 fprintf (stderr, "Please specify a user\n");
118 if (info.key_size > MAX_KEY_SIZE)
120 fprintf (stderr, "Key size is too long\n");
124 if (info.key_size < 1)
127 printf ("Generating a random key for user '%s'\n", info.username);
129 ret = gnutls_rnd (GNUTLS_RND_RANDOM, (char *) key, info.key_size);
132 fprintf (stderr, "Not enough randomness\n");
137 dkey.size = info.key_size;
139 ret = gnutls_hex_encode (&dkey, hex_key, &hex_key_size);
142 fprintf (stderr, "HEX encoding error\n");
146 ret = write_key (info.username, hex_key, hex_key_size, info.passwd);
148 printf ("Key stored to %s\n", info.passwd);
154 filecopy (char *src, char *dst)
160 fd = fopen (dst, "w");
163 fprintf (stderr, "Cannot open '%s' for write\n", dst);
167 fd2 = fopen (src, "r");
175 line[sizeof (line) - 1] = 0;
178 p = fgets (line, sizeof (line) - 1, fd2);
193 write_key (const char *username, const char *key, int key_size,
202 /* delete previous entry */
207 if (strlen (passwd_file) > sizeof (tmpname) + 5)
209 fprintf (stderr, "file '%s' is tooooo long\n", passwd_file);
212 strcpy (tmpname, passwd_file);
213 strcat (tmpname, ".tmp");
215 if (stat (tmpname, &st) != -1)
217 fprintf (stderr, "file '%s' is locked\n", tmpname);
221 if (filecopy (passwd_file, tmpname) != 0)
223 fprintf (stderr, "Cannot copy '%s' to '%s'\n", passwd_file, tmpname);
227 fd = fopen (passwd_file, "w");
230 fprintf (stderr, "Cannot open '%s' for write\n", passwd_file);
235 fd2 = fopen (tmpname, "r");
238 fprintf (stderr, "Cannot open '%s' for read\n", tmpname);
246 p = fgets (line, sizeof (line) - 1, fd2);
250 pp = strchr (line, ':');
254 if (strncmp (p, username,
255 MAX (strlen (username), (unsigned int) (pp - p))) == 0)
258 fprintf (fd, "%s:%s\n", username, key);
269 fprintf (fd, "%s:%s\n", username, key);
281 #endif /* ENABLE_PSK */
283 void psktool_version (void);
286 psktool_version (void)
288 const char *p = PACKAGE_NAME;
289 if (strcmp (gnutls_check_version (NULL), PACKAGE_VERSION) != 0)
291 version_etc (stdout, "psktool", p, gnutls_check_version (NULL),
292 "Nikos Mavrogiannopoulos", (char *) NULL);