2 * Copyright (C) 2005-2012 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>
46 #include <gnutls/crypto.h> /* for random */
48 #include <sys/types.h>
58 /* Gnulib portability files. */
62 static int write_key (const char *username
, const char *key
, int key_size
,
63 const char *passwd_file
);
65 #define KPASSWD "/etc/passwd.psk"
66 #define MAX_KEY_SIZE 64
68 main (int argc
, char **argv
)
74 unsigned char key
[MAX_KEY_SIZE
];
75 char hex_key
[MAX_KEY_SIZE
* 2 + 1];
78 const char* passwd
, *username
;
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 optct
= optionProcess( &psktoolOptions
, argc
, argv
);
95 if (!HAVE_OPT(PASSWD
))
96 passwd
= (char *) KPASSWD
;
98 passwd
= OPT_ARG(PASSWD
);
100 if (!HAVE_OPT(USERNAME
))
103 pwd
= getpwuid (getuid ());
107 fprintf (stderr
, "No such user\n");
111 username
= pwd
->pw_name
;
113 fprintf (stderr
, "Please specify a user\n");
118 username
= OPT_ARG(USERNAME
);
120 if (HAVE_OPT(KEYSIZE
) && OPT_VALUE_KEYSIZE
> MAX_KEY_SIZE
)
122 fprintf (stderr
, "Key size is too long\n");
126 if (!HAVE_OPT(KEYSIZE
) || OPT_VALUE_KEYSIZE
< 1)
129 key_size
= OPT_VALUE_KEYSIZE
;
131 printf ("Generating a random key for user '%s'\n", username
);
133 ret
= gnutls_rnd (GNUTLS_RND_RANDOM
, (char *) key
, key_size
);
136 fprintf (stderr
, "Not enough randomness\n");
141 dkey
.size
= key_size
;
143 ret
= gnutls_hex_encode (&dkey
, hex_key
, &hex_key_size
);
146 fprintf (stderr
, "HEX encoding error\n");
150 ret
= write_key (username
, hex_key
, hex_key_size
, passwd
);
152 printf ("Key stored to %s\n", passwd
);
158 filecopy (const char *src
, const char *dst
)
164 fd
= fopen (dst
, "w");
167 fprintf (stderr
, "Cannot open '%s' for write\n", dst
);
171 fd2
= fopen (src
, "r");
179 line
[sizeof (line
) - 1] = 0;
182 p
= fgets (line
, sizeof (line
) - 1, fd2
);
197 write_key (const char *username
, const char *key
, int key_size
,
198 const char *passwd_file
)
206 /* delete previous entry */
211 if (strlen (passwd_file
) > sizeof (tmpname
) + 5)
213 fprintf (stderr
, "file '%s' is tooooo long\n", passwd_file
);
216 strcpy (tmpname
, passwd_file
);
217 strcat (tmpname
, ".tmp");
219 if (stat (tmpname
, &st
) != -1)
221 fprintf (stderr
, "file '%s' is locked\n", tmpname
);
225 if (filecopy (passwd_file
, tmpname
) != 0)
227 fprintf (stderr
, "Cannot copy '%s' to '%s'\n", passwd_file
, tmpname
);
231 fd
= fopen (passwd_file
, "w");
234 fprintf (stderr
, "Cannot open '%s' for write\n", passwd_file
);
239 fd2
= fopen (tmpname
, "r");
242 fprintf (stderr
, "Cannot open '%s' for read\n", tmpname
);
250 p
= fgets (line
, sizeof (line
) - 1, fd2
);
254 pp
= strchr (line
, ':');
258 if (strncmp (p
, username
,
259 MAX (strlen (username
), (unsigned int) (pp
- p
))) == 0)
262 fprintf (fd
, "%s:%s\n", username
, key
);
273 fprintf (fd
, "%s:%s\n", username
, key
);
285 #endif /* ENABLE_PSK */