xref: /openbsd/sbin/dhcpleased/printconf.c (revision d415bd75)
1 /*	$OpenBSD: printconf.c,v 1.4 2022/01/04 06:20:37 florian Exp $	*/
2 
3 /*
4  * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
5  * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 #include <sys/queue.h>
22 #include <sys/socket.h>
23 #include <sys/uio.h>
24 
25 #include <net/if.h>
26 
27 #include <netinet/in.h>
28 #include <netinet/if_ether.h>
29 
30 #include <arpa/inet.h>
31 
32 #include <event.h>
33 #include <imsg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <vis.h>
37 
38 #include "dhcpleased.h"
39 #include "log.h"
40 
41 void	print_dhcp_options(char *, uint8_t *, int);
42 
43 void
44 print_dhcp_options(char *indent, uint8_t *p, int len)
45 {
46 	static char	 buf[4 * 1500 + 1];
47 	int		 rem, i;
48 	uint8_t		 dho, dho_len;
49 
50 	rem = len;
51 
52 	while (rem > 0) {
53 		dho = *p;
54 		p += 1;
55 		rem -= 1;
56 
57 		if (rem == 0)
58 			fatal("dhcp option too short");
59 		dho_len = *p;
60 		p += 1;
61 		rem -= 1;
62 		if (rem < dho_len)
63 			fatal("dhcp option too short: %d %d", rem, dho_len);
64 
65 		switch (dho) {
66 		case DHO_DHCP_CLASS_IDENTIFIER:
67 			strvisx(buf, p, dho_len, VIS_DQ | VIS_CSTYLE);
68 			p += dho_len;
69 			rem -= dho_len;
70 			printf("%ssend vendor class id \"%s\"\n", indent, buf);
71 			break;
72 		case DHO_DHCP_CLIENT_IDENTIFIER:
73 			if (dho_len < 1)
74 				fatal("dhcp option too short");
75 			switch (*p) {
76 			case HTYPE_ETHER:
77 				printf("%ssend client id \"", indent);
78 				for (i = 0; i < dho_len; i++)
79 					printf("%s%02x", i != 0? ":" : "",
80 					    *(p + i));
81 				printf("\"\n");
82 				break;
83 			default:
84 				strvisx(buf, p, dho_len, VIS_DQ | VIS_CSTYLE);
85 				printf("%ssend client id \"%s\"\n",
86 				    indent, buf);
87 				break;
88 			}
89 			p += dho_len;
90 			rem -= dho_len;
91 			break;
92 		default:
93 			fatal("unknown dhcp option: %d [%d]", *p, rem);
94 			break;
95 		}
96 	}
97 }
98 
99 void
100 print_config(struct dhcpleased_conf *conf)
101 {
102 	struct iface_conf	*iface;
103 	int			 i;
104 	char			 hbuf[INET_ADDRSTRLEN];
105 
106 	SIMPLEQ_FOREACH(iface, &conf->iface_list, entry) {
107 		printf("interface %s {\n", iface->name);
108 		print_dhcp_options("\t", iface->c_id, iface->c_id_len);
109 		if (iface->h_name != NULL) {
110 			if (iface->h_name[0] == '\0')
111 				printf("\tsend no host name\n");
112 			else {
113 				printf("\tsend host name \"%s\"\n",
114 				    iface->h_name);
115 			}
116 		}
117 		print_dhcp_options("\t", iface->vc_id, iface->vc_id_len);
118 		if (iface->ignore & IGN_DNS)
119 			printf("\tignore dns\n");
120 		if (iface->ignore & IGN_ROUTES)
121 			printf("\tignore routes\n");
122 		for (i = 0; i < iface->ignore_servers_len; i++) {
123 			if (inet_ntop(AF_INET, &iface->ignore_servers[i],
124 			    hbuf, sizeof(hbuf)) == NULL)
125 				continue;
126 			printf("\tignore %s\n", hbuf);
127 
128 		}
129 		printf("}\n");
130 	}
131 }
132