1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2001-2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate * mkdstore: fast datastore creation program.
31*0Sstevel@tonic-gate *
32*0Sstevel@tonic-gate * mkdstore <table> <nrecords> <cid> <flags> <cip> <sip> <lease> <comment>
33*0Sstevel@tonic-gate */
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate #include <stdio.h>
36*0Sstevel@tonic-gate #include <netdb.h>
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate #include <string.h>
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <rpcsvc/nis.h>
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate #include <sys/types.h>
43*0Sstevel@tonic-gate #include <sys/socket.h>
44*0Sstevel@tonic-gate #include <netinet/in.h>
45*0Sstevel@tonic-gate #include <arpa/inet.h>
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate #include <stdio.h>
48*0Sstevel@tonic-gate #include <stdlib.h>
49*0Sstevel@tonic-gate #include <unistd.h>
50*0Sstevel@tonic-gate #include <sys/types.h>
51*0Sstevel@tonic-gate #include <errno.h>
52*0Sstevel@tonic-gate #include <assert.h>
53*0Sstevel@tonic-gate #include <string.h>
54*0Sstevel@tonic-gate #include <syslog.h>
55*0Sstevel@tonic-gate #include <sys/socket.h>
56*0Sstevel@tonic-gate #include <net/if.h>
57*0Sstevel@tonic-gate #include <netinet/in.h>
58*0Sstevel@tonic-gate #include <netinet/if_ether.h>
59*0Sstevel@tonic-gate #include <arpa/inet.h>
60*0Sstevel@tonic-gate #include <netinet/dhcp.h>
61*0Sstevel@tonic-gate #include <netdb.h>
62*0Sstevel@tonic-gate #include <locale.h>
63*0Sstevel@tonic-gate #include <signal.h>
64*0Sstevel@tonic-gate #include <tnf/probe.h>
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate #include <dhcp_svc_confopt.h>
67*0Sstevel@tonic-gate #include <dhcp_svc_private.h>
68*0Sstevel@tonic-gate #include <dhcp_impl.h>
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate dsvc_handle_t dh; /* data handle */
71*0Sstevel@tonic-gate dsvc_datastore_t datastore; /* Datastore for container access */
72*0Sstevel@tonic-gate uint_t nrecords;
73*0Sstevel@tonic-gate char network[INET_ADDRSTRLEN];
74*0Sstevel@tonic-gate struct in_addr net;
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate /*
77*0Sstevel@tonic-gate * mkdstore <table> <nrecords> <cid> <flags> <cip> <sip> <lease> <macro>
78*0Sstevel@tonic-gate * <comment>
79*0Sstevel@tonic-gate */
main(int c,char ** v)80*0Sstevel@tonic-gate main(int c, char **v)
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate long long cid;
83*0Sstevel@tonic-gate uchar_t flags;
84*0Sstevel@tonic-gate struct in_addr cip;
85*0Sstevel@tonic-gate struct in_addr sip;
86*0Sstevel@tonic-gate int i, j;
87*0Sstevel@tonic-gate char **entries;
88*0Sstevel@tonic-gate uint_t lease;
89*0Sstevel@tonic-gate char *network = v[1];
90*0Sstevel@tonic-gate int ct = strtol(v[2], 0L, 0L);
91*0Sstevel@tonic-gate char *server;
92*0Sstevel@tonic-gate char *macro;
93*0Sstevel@tonic-gate int err;
94*0Sstevel@tonic-gate uint32_t query;
95*0Sstevel@tonic-gate dn_rec_t dn;
96*0Sstevel@tonic-gate dn_rec_list_t *dncp = NULL;
97*0Sstevel@tonic-gate dhcp_confopt_t *dsp = NULL;
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gate #ifdef DEBUG
100*0Sstevel@tonic-gate mallocctl(MTDEBUGPATTERN, 1);
101*0Sstevel@tonic-gate mallocctl(MTINITBUFFER, 1);
102*0Sstevel@tonic-gate #endif /* DEBUG */
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate if (c == 1) {
105*0Sstevel@tonic-gate (void) fprintf(stderr, "/*\n * mkdstore <table> <nrecords> "
106*0Sstevel@tonic-gate "<cid> <flags> <cip> <sip> <lease> <comment>\n*/");
107*0Sstevel@tonic-gate return (0);
108*0Sstevel@tonic-gate }
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate cid = (c > 3) ? strtoul(v[3], 0L, 0L) : 0;
111*0Sstevel@tonic-gate flags = (c > 4) ? (char)strtol(v[4], 0L, 0L) : 0;
112*0Sstevel@tonic-gate cip.s_addr = (c > 5) ? strtoul(v[5], 0L, 0L) : 0;
113*0Sstevel@tonic-gate sip.s_addr = (c > 6) ? strtoul(v[6], 0L, 0L) : 0;
114*0Sstevel@tonic-gate lease = (c > 7) ? strtoul(v[7], 0L, 0L) : 0;
115*0Sstevel@tonic-gate macro = (c > 8) ? v[8] : 0;
116*0Sstevel@tonic-gate server = (c > 9) ? v[9] : "unknown";
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate entries = (char **) malloc(ct * (sizeof (char *) * 8 + 4));
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate /* Load current datastore. */
121*0Sstevel@tonic-gate (void) read_dsvc_conf(&dsp);
122*0Sstevel@tonic-gate if ((i = confopt_to_datastore(dsp, &datastore)) != DSVC_SUCCESS) {
123*0Sstevel@tonic-gate (void) fprintf(stderr, "Invalid datastore: %s\n",
124*0Sstevel@tonic-gate dhcpsvc_errmsg(i));
125*0Sstevel@tonic-gate return (EINVAL);
126*0Sstevel@tonic-gate }
127*0Sstevel@tonic-gate err = open_dd(&dh, &datastore, DSVC_DHCPNETWORK, network,
128*0Sstevel@tonic-gate DSVC_READ | DSVC_WRITE);
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gate if (err != DSVC_SUCCESS) {
131*0Sstevel@tonic-gate (void) fprintf(stderr, "Invalid network: %s trying create...\n",
132*0Sstevel@tonic-gate dhcpsvc_errmsg(err));
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate err = open_dd(&dh, &datastore, DSVC_DHCPNETWORK, network,
135*0Sstevel@tonic-gate DSVC_READ | DSVC_WRITE | DSVC_CREATE);
136*0Sstevel@tonic-gate if (err != DSVC_SUCCESS) {
137*0Sstevel@tonic-gate (void) fprintf(stderr, "Can't create network: %s\n",
138*0Sstevel@tonic-gate dhcpsvc_errmsg(err));
139*0Sstevel@tonic-gate return (err);
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate }
142*0Sstevel@tonic-gate /* XXXX: bug: currently can't get the count as advertised */
143*0Sstevel@tonic-gate (void) memset(&dn, '\0', sizeof (dn));
144*0Sstevel@tonic-gate DSVC_QINIT(query);
145*0Sstevel@tonic-gate err = lookup_dd(dh, B_FALSE, query, -1,
146*0Sstevel@tonic-gate (const void *) &dn, (void **) &dncp, &nrecords);
147*0Sstevel@tonic-gate if (dncp)
148*0Sstevel@tonic-gate free_dd_list(dh, dncp);
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gate if (err != DSVC_SUCCESS) {
151*0Sstevel@tonic-gate (void) fprintf(stderr, "Bad nrecords: %s [%d]\n",
152*0Sstevel@tonic-gate dhcpsvc_errmsg(err), nrecords);
153*0Sstevel@tonic-gate return (err);
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate for (i = 0, j = 0; i < ct; i++) {
157*0Sstevel@tonic-gate TNF_PROBE_1(main, "main",
158*0Sstevel@tonic-gate "main%debug 'in function main'",
159*0Sstevel@tonic-gate tnf_ulong, record, i);
160*0Sstevel@tonic-gate if (cid) {
161*0Sstevel@tonic-gate (void) memcpy(dn.dn_cid, &cid, sizeof (long long));
162*0Sstevel@tonic-gate dn.dn_cid_len = 7;
163*0Sstevel@tonic-gate } else {
164*0Sstevel@tonic-gate (void) memset(dn.dn_cid, '\0', sizeof (long long));
165*0Sstevel@tonic-gate dn.dn_cid_len = 1;
166*0Sstevel@tonic-gate }
167*0Sstevel@tonic-gate dn.dn_sig = 0;
168*0Sstevel@tonic-gate dn.dn_flags = flags;
169*0Sstevel@tonic-gate dn.dn_cip.s_addr = cip.s_addr;
170*0Sstevel@tonic-gate dn.dn_sip.s_addr = sip.s_addr;
171*0Sstevel@tonic-gate dn.dn_lease = lease;
172*0Sstevel@tonic-gate strcpy(dn.dn_macro, macro);
173*0Sstevel@tonic-gate strcpy(dn.dn_comment, server);
174*0Sstevel@tonic-gate (void) add_dd_entry(dh, &dn);
175*0Sstevel@tonic-gate if (cid)
176*0Sstevel@tonic-gate cid += 0x100;
177*0Sstevel@tonic-gate cip.s_addr++;
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gate TNF_PROBE_0(main_end, "main", "");
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate (void) close_dd(&dh);
182*0Sstevel@tonic-gate
183*0Sstevel@tonic-gate return (0);
184*0Sstevel@tonic-gate }
185