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