xref: /minix3/crypto/external/bsd/netpgp/dist/src/hkpd/main.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*-
2*ebfedea0SLionel Sambuc  * Copyright (c) 2009,2010 The NetBSD Foundation, Inc.
3*ebfedea0SLionel Sambuc  * All rights reserved.
4*ebfedea0SLionel Sambuc  *
5*ebfedea0SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
6*ebfedea0SLionel Sambuc  * by Alistair Crooks (agc@NetBSD.org)
7*ebfedea0SLionel Sambuc  *
8*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc  * are met:
11*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
12*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
13*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
14*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
15*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
16*ebfedea0SLionel Sambuc  *
17*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18*ebfedea0SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19*ebfedea0SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20*ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21*ebfedea0SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*ebfedea0SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*ebfedea0SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*ebfedea0SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*ebfedea0SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*ebfedea0SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
28*ebfedea0SLionel Sambuc  */
29*ebfedea0SLionel Sambuc #ifdef HAVE_CONFIG_H
30*ebfedea0SLionel Sambuc #include "config.h"
31*ebfedea0SLionel Sambuc #endif
32*ebfedea0SLionel Sambuc 
33*ebfedea0SLionel Sambuc #include <sys/types.h>
34*ebfedea0SLionel Sambuc #include <sys/param.h>
35*ebfedea0SLionel Sambuc #include <sys/stat.h>
36*ebfedea0SLionel Sambuc 
37*ebfedea0SLionel Sambuc #include <stdio.h>
38*ebfedea0SLionel Sambuc #include <stdlib.h>
39*ebfedea0SLionel Sambuc #include <string.h>
40*ebfedea0SLionel Sambuc #include <unistd.h>
41*ebfedea0SLionel Sambuc 
42*ebfedea0SLionel Sambuc #include "hkpd.h"
43*ebfedea0SLionel Sambuc 
44*ebfedea0SLionel Sambuc #define HAVE_DAEMON	1
45*ebfedea0SLionel Sambuc 
46*ebfedea0SLionel Sambuc /* set the home directory value to "home/subdir" */
47*ebfedea0SLionel Sambuc static int
set_homedir(netpgp_t * netpgp,char * home,const char * subdir,const int quiet)48*ebfedea0SLionel Sambuc set_homedir(netpgp_t *netpgp, char *home, const char *subdir, const int quiet)
49*ebfedea0SLionel Sambuc {
50*ebfedea0SLionel Sambuc 	struct stat	st;
51*ebfedea0SLionel Sambuc 	char		d[MAXPATHLEN];
52*ebfedea0SLionel Sambuc 
53*ebfedea0SLionel Sambuc 	if (home == NULL) {
54*ebfedea0SLionel Sambuc 		if (!quiet) {
55*ebfedea0SLionel Sambuc 			(void) fprintf(stderr, "NULL HOME directory\n");
56*ebfedea0SLionel Sambuc 		}
57*ebfedea0SLionel Sambuc 		return 0;
58*ebfedea0SLionel Sambuc 	}
59*ebfedea0SLionel Sambuc 	(void) snprintf(d, sizeof(d), "%s%s", home, (subdir) ? subdir : "");
60*ebfedea0SLionel Sambuc 	if (stat(d, &st) == 0) {
61*ebfedea0SLionel Sambuc 		if ((st.st_mode & S_IFMT) == S_IFDIR) {
62*ebfedea0SLionel Sambuc 			netpgp_setvar(netpgp, "homedir", d);
63*ebfedea0SLionel Sambuc 			return 1;
64*ebfedea0SLionel Sambuc 		}
65*ebfedea0SLionel Sambuc 		(void) fprintf(stderr, "netpgp: homedir \"%s\" is not a dir\n",
66*ebfedea0SLionel Sambuc 					d);
67*ebfedea0SLionel Sambuc 		return 0;
68*ebfedea0SLionel Sambuc 	}
69*ebfedea0SLionel Sambuc 	if (!quiet) {
70*ebfedea0SLionel Sambuc 		(void) fprintf(stderr,
71*ebfedea0SLionel Sambuc 			"netpgp: warning homedir \"%s\" not found\n", d);
72*ebfedea0SLionel Sambuc 	}
73*ebfedea0SLionel Sambuc 	return 1;
74*ebfedea0SLionel Sambuc }
75*ebfedea0SLionel Sambuc 
76*ebfedea0SLionel Sambuc int
main(int argc,char ** argv)77*ebfedea0SLionel Sambuc main(int argc, char **argv)
78*ebfedea0SLionel Sambuc {
79*ebfedea0SLionel Sambuc 	netpgp_t	 netpgp;
80*ebfedea0SLionel Sambuc 	char		*family;
81*ebfedea0SLionel Sambuc 	char		*host;
82*ebfedea0SLionel Sambuc 	int		 daemonise;
83*ebfedea0SLionel Sambuc 	int		 port;
84*ebfedea0SLionel Sambuc 	int		 sock6;
85*ebfedea0SLionel Sambuc 	int		 sock4;
86*ebfedea0SLionel Sambuc 	int		 i;
87*ebfedea0SLionel Sambuc 
88*ebfedea0SLionel Sambuc 	(void) memset(&netpgp, 0x0, sizeof(netpgp));
89*ebfedea0SLionel Sambuc 	/* set some defaults */
90*ebfedea0SLionel Sambuc 	set_homedir(&netpgp, getenv("HOME"), "/.gnupg", 1);
91*ebfedea0SLionel Sambuc 	port = 11371;
92*ebfedea0SLionel Sambuc 	host = strdup("localhost");
93*ebfedea0SLionel Sambuc 	daemonise = 1;
94*ebfedea0SLionel Sambuc 	family = strdup("46");
95*ebfedea0SLionel Sambuc 	while ((i = getopt(argc, argv, "DH:S:Vf:h:p:v:")) != -1) {
96*ebfedea0SLionel Sambuc 		switch(i) {
97*ebfedea0SLionel Sambuc 		case 'D':
98*ebfedea0SLionel Sambuc 			daemonise = 0;
99*ebfedea0SLionel Sambuc 			break;
100*ebfedea0SLionel Sambuc 		case 'H':
101*ebfedea0SLionel Sambuc 			set_homedir(&netpgp, optarg, NULL, 0);
102*ebfedea0SLionel Sambuc 			break;
103*ebfedea0SLionel Sambuc 		case 'S':
104*ebfedea0SLionel Sambuc 			netpgp_setvar(&netpgp, "ssh keys", "1");
105*ebfedea0SLionel Sambuc 			netpgp_setvar(&netpgp, "sshkeyfile", optarg);
106*ebfedea0SLionel Sambuc 			break;
107*ebfedea0SLionel Sambuc 		case 'V':
108*ebfedea0SLionel Sambuc 			printf("%s: Version %d\n", *argv, HKPD_VERSION);
109*ebfedea0SLionel Sambuc 			exit(EXIT_SUCCESS);
110*ebfedea0SLionel Sambuc 		case 'f':
111*ebfedea0SLionel Sambuc 			(void) free(family);
112*ebfedea0SLionel Sambuc 			family = strdup(optarg);
113*ebfedea0SLionel Sambuc 			break;
114*ebfedea0SLionel Sambuc 		case 'h':
115*ebfedea0SLionel Sambuc 			(void) free(host);
116*ebfedea0SLionel Sambuc 			host = strdup(optarg);
117*ebfedea0SLionel Sambuc 			break;
118*ebfedea0SLionel Sambuc 		case 'p':
119*ebfedea0SLionel Sambuc 			port = atoi(optarg);
120*ebfedea0SLionel Sambuc 			break;
121*ebfedea0SLionel Sambuc 		case 'v':
122*ebfedea0SLionel Sambuc 			netpgp_set_debug(optarg);
123*ebfedea0SLionel Sambuc 			break;
124*ebfedea0SLionel Sambuc 		default:
125*ebfedea0SLionel Sambuc 			break;
126*ebfedea0SLionel Sambuc 		}
127*ebfedea0SLionel Sambuc 	}
128*ebfedea0SLionel Sambuc #ifdef HAVE_DAEMON
129*ebfedea0SLionel Sambuc 	/* if we are supposed to be a daemon, detach from controlling tty */
130*ebfedea0SLionel Sambuc 	if (daemonise && daemon(0, 0) < 0) {
131*ebfedea0SLionel Sambuc 		(void) fprintf(stderr, "daemon() failed\n");
132*ebfedea0SLionel Sambuc 		exit(EXIT_FAILURE);
133*ebfedea0SLionel Sambuc 	}
134*ebfedea0SLionel Sambuc #endif
135*ebfedea0SLionel Sambuc 	if (!netpgp_init(&netpgp)) {
136*ebfedea0SLionel Sambuc 		(void) fprintf(stderr, "can't initialise\n");
137*ebfedea0SLionel Sambuc 		exit(EXIT_FAILURE);
138*ebfedea0SLionel Sambuc 	}
139*ebfedea0SLionel Sambuc 	sock4 = sock6 = -1;
140*ebfedea0SLionel Sambuc 	if (strchr(family, '4') != NULL &&
141*ebfedea0SLionel Sambuc 	    (sock4 = hkpd_sock_bind(host, port, 4)) < 0) {
142*ebfedea0SLionel Sambuc 		(void) fprintf(stderr,"hkpd: can't bind inet4 socket\n");
143*ebfedea0SLionel Sambuc 	}
144*ebfedea0SLionel Sambuc 	if (strchr(family, '6') != NULL &&
145*ebfedea0SLionel Sambuc 	    (sock6 = hkpd_sock_bind(host, port, 6)) < 0) {
146*ebfedea0SLionel Sambuc 		(void) fprintf(stderr,"hkpd: can't bind inet6 socket\n");
147*ebfedea0SLionel Sambuc 	}
148*ebfedea0SLionel Sambuc 	if (sock4 < 0 && sock6 < 0) {
149*ebfedea0SLionel Sambuc 		(void) fprintf(stderr,"hkpd: no sockets available\n");
150*ebfedea0SLionel Sambuc 		exit(EXIT_FAILURE);
151*ebfedea0SLionel Sambuc 	}
152*ebfedea0SLionel Sambuc 	hkpd(&netpgp, sock4, sock6);
153*ebfedea0SLionel Sambuc }
154