xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/libpaa/client.c (revision 3fb45f3cb6268894e10d0a1f1413811afbe5b819)
1*3fb45f3cSagc /*-
2*3fb45f3cSagc  * Copyright (c) 2010 Alistair Crooks <agc@NetBSD.org>
3*3fb45f3cSagc  * All rights reserved.
4*3fb45f3cSagc  *
5*3fb45f3cSagc  * Redistribution and use in source and binary forms, with or without
6*3fb45f3cSagc  * modification, are permitted provided that the following conditions
7*3fb45f3cSagc  * are met:
8*3fb45f3cSagc  * 1. Redistributions of source code must retain the above copyright
9*3fb45f3cSagc  *    notice, this list of conditions and the following disclaimer.
10*3fb45f3cSagc  * 2. Redistributions in binary form must reproduce the above copyright
11*3fb45f3cSagc  *    notice, this list of conditions and the following disclaimer in the
12*3fb45f3cSagc  *    documentation and/or other materials provided with the distribution.
13*3fb45f3cSagc  *
14*3fb45f3cSagc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15*3fb45f3cSagc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*3fb45f3cSagc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*3fb45f3cSagc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18*3fb45f3cSagc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*3fb45f3cSagc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*3fb45f3cSagc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*3fb45f3cSagc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*3fb45f3cSagc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*3fb45f3cSagc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*3fb45f3cSagc  */
25*3fb45f3cSagc #include <sys/types.h>
26*3fb45f3cSagc 
27*3fb45f3cSagc #include <netdb.h>
28*3fb45f3cSagc 
29*3fb45f3cSagc #include <netpgp.h>
30*3fb45f3cSagc #include <regex.h>
31*3fb45f3cSagc #include <sha1.h>
32*3fb45f3cSagc #include <stdio.h>
33*3fb45f3cSagc #include <stdlib.h>
34*3fb45f3cSagc #include <string.h>
35*3fb45f3cSagc #include <time.h>
36*3fb45f3cSagc #include <unistd.h>
37*3fb45f3cSagc 
38*3fb45f3cSagc #include "libpaa.h"
39*3fb45f3cSagc 
40*3fb45f3cSagc #define DEFAULT_HASH_ALG "SHA256"
41*3fb45f3cSagc 
42*3fb45f3cSagc int
main(int argc,char ** argv)43*3fb45f3cSagc main(int argc, char **argv)
44*3fb45f3cSagc {
45*3fb45f3cSagc 	paa_response_t	response;
46*3fb45f3cSagc 	netpgp_t	netpgp;
47*3fb45f3cSagc 	char		challenge[2048];
48*3fb45f3cSagc 	char		buf[2048];
49*3fb45f3cSagc 	int		challengec;
50*3fb45f3cSagc 	int		cc;
51*3fb45f3cSagc 	int		i;
52*3fb45f3cSagc 
53*3fb45f3cSagc 	(void) memset(&response, 0x0, sizeof(response));
54*3fb45f3cSagc 	(void) memset(&netpgp, 0x0, sizeof(netpgp));
55*3fb45f3cSagc 	while ((i = getopt(argc, argv, "S:d:r:u:")) != -1) {
56*3fb45f3cSagc 		switch(i) {
57*3fb45f3cSagc 		case 'S':
58*3fb45f3cSagc 			netpgp_setvar(&netpgp, "ssh keys", "1");
59*3fb45f3cSagc 			netpgp_setvar(&netpgp, "sshkeyfile", optarg);
60*3fb45f3cSagc 			break;
61*3fb45f3cSagc 		case 'd':
62*3fb45f3cSagc 			//challenge.domain = optarg;
63*3fb45f3cSagc 			break;
64*3fb45f3cSagc 		case 'r':
65*3fb45f3cSagc 			//challenge.realm = optarg;
66*3fb45f3cSagc 			response.realm = optarg;
67*3fb45f3cSagc 			break;
68*3fb45f3cSagc 		case 'u':
69*3fb45f3cSagc 			netpgp_setvar(&netpgp, "userid", optarg);
70*3fb45f3cSagc 			break;
71*3fb45f3cSagc 		}
72*3fb45f3cSagc 	}
73*3fb45f3cSagc 	netpgp_setvar(&netpgp, "hash", DEFAULT_HASH_ALG);
74*3fb45f3cSagc 	netpgp_setvar(&netpgp, "need seckey", "1");
75*3fb45f3cSagc 	netpgp_setvar(&netpgp, "need userid", "1");
76*3fb45f3cSagc 	netpgp_set_homedir(&netpgp, getenv("HOME"),
77*3fb45f3cSagc 			netpgp_getvar(&netpgp, "ssh keys") ? "/.ssh" : "/.gnupg", 1);
78*3fb45f3cSagc 	if (!netpgp_init(&netpgp)) {
79*3fb45f3cSagc 		(void) fprintf(stderr, "can't initialise netpgp\n");
80*3fb45f3cSagc 		exit(EXIT_FAILURE);
81*3fb45f3cSagc 	}
82*3fb45f3cSagc 	/* read challenge into challenge */
83*3fb45f3cSagc 	challengec = read(0, challenge, sizeof(challenge));
84*3fb45f3cSagc 	cc = paa_format_response(&response, &netpgp, challenge, buf, sizeof(buf));
85*3fb45f3cSagc 	write(1, buf, cc);
86*3fb45f3cSagc 	exit(EXIT_SUCCESS);
87*3fb45f3cSagc }
88