4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
29 * Trusted Network control utility
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
47 #include <nss_dbdefs.h>
49 static void process_rh(const char *);
50 static void process_rhl(const char *);
51 static void process_mlp(const char *);
52 static void process_tp(const char *);
53 static void process_tpl(const char *);
54 static void process_tnzone(const char *);
55 static void usage(void);
56 static void translate_inet_addr(tsol_rhent_t
*, int *, char [], int);
58 static boolean_t verbose_mode
;
59 static boolean_t delete_mode
;
60 static boolean_t flush_mode
;
63 main(int argc
, char **argv
)
68 /* Don't do anything if labeling is not active. */
69 if (!is_system_labeled())
72 /* set the locale for only the messages system (all else is clean) */
73 (void) setlocale(LC_ALL
, "");
74 #ifndef TEXT_DOMAIN /* Should be defined by cc -D */
75 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
78 (void) textdomain(TEXT_DOMAIN
);
80 while ((chr
= getopt(argc
, argv
, "dfh:H:m:t:T:vz:")) != EOF
) {
104 verbose_mode
= B_TRUE
;
107 process_tnzone(optarg
);
117 print_error(int linenum
, int err
, const char *errstr
)
120 (void) fprintf(stderr
, gettext("line %1$d: %2$s:\n"), linenum
,
121 tsol_strerror(err
, errno
));
123 (void) fprintf(stderr
, gettext("tnctl: parsing error: %s\n"),
124 tsol_strerror(err
, errno
));
125 (void) fprintf(stderr
, "%.32s\n", errstr
);
129 * Produce ascii format of address and prefix length
132 translate_inet_addr(tsol_rhent_t
*rhentp
, int *alen
, char abuf
[], int abuflen
)
136 struct in6_addr ipv6addr
;
139 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "/%d", rhentp
->rh_prefix
);
141 if (rhentp
->rh_address
.ta_family
== AF_INET6
) {
142 aptr
= &(rhentp
->rh_address
.ta_addr_v6
);
143 (void) inet_ntop(rhentp
->rh_address
.ta_family
, aptr
, abuf
,
145 if (rhentp
->rh_prefix
!= 128) {
146 if (strlcat(abuf
, tmpbuf
, abuflen
) >= abuflen
)
147 (void) fprintf(stderr
, gettext(
148 "tnctl: buffer overflow detected: %s\n"),
151 *alen
= strlen(abuf
);
153 aptr
= &(rhentp
->rh_address
.ta_addr_v4
);
154 (void) inet_ntop(rhentp
->rh_address
.ta_family
, aptr
, abuf
,
156 if (rhentp
->rh_prefix
!= 32) {
157 if (strlcat(abuf
, tmpbuf
, abuflen
) >= abuflen
)
158 (void) fprintf(stderr
, gettext(
159 "tnctl: buffer overflow detected: %s\n"),
162 *alen
= strlen(abuf
);
167 * Load remote host entries from the designated file.
170 process_rhl(const char *file
)
172 boolean_t error
= B_FALSE
;
173 boolean_t success
= B_FALSE
;
174 tsol_rhent_t
*rhentp
= NULL
;
177 /* abuf holds: <numeric-ip-addr>'/'<prefix-length>'\0' */
178 char abuf
[INET6_ADDRSTRLEN
+5];
180 if ((fp
= fopen(file
, "r")) == NULL
) {
181 (void) fprintf(stderr
,
182 gettext("tnctl: failed to open %1$s: %2$s\n"),
183 file
, strerror(errno
));
188 while (rhentp
= tsol_fgetrhent(fp
, &error
)) {
189 /* First time through the loop, flush it all */
190 if (!success
&& flush_mode
)
191 (void) tnrh(TNDB_FLUSH
, NULL
);
195 (void) printf("loading rh entry...\n");
197 if (tnrh(TNDB_LOAD
, rhentp
) != 0) {
202 translate_inet_addr(rhentp
, &alen
, abuf
,
204 (void) fprintf(stderr
,
205 gettext("tnctl: load of remote-host entry "
206 "%1$s into kernel cache failed: %2$s\n"),
207 abuf
, strerror(errno
));
211 tsol_freerhent(rhentp
);
214 (void) fprintf(stderr
,
215 gettext("tnctl: No valid tnrhdb entries found in %s\n"),
226 * The argument can be either a host name, an address
227 * in tnrhdb address format, or a complete tnrhdb entry.
230 process_rh(const char *hostname
)
234 tsol_rhent_t
*rhentp
;
238 /* abuf holds: <numeric-ip-addr>'/'<prefix-length>'\0' */
239 char abuf
[INET6_ADDRSTRLEN
+5];
244 char buf
[NSS_BUFLEN_TSOL_RH
];
245 struct in6_addr ipv6addr
;
247 /* was a template name provided on the command line? */
248 if ((cp
= strrchr(hostname
, ':')) != NULL
&& cp
!= hostname
&&
250 /* use common tnrhdb line conversion function */
251 (void) str_to_rhstr(hostname
, strlen(hostname
), &rhstr
, buf
,
253 rhentp
= rhstr_to_ent(&rhstr
, &err
, &errstr
);
254 if (rhentp
== NULL
) {
255 print_error(0, err
, errstr
);
263 /* Check for a subnet prefix length */
264 if ((prefix_p
= strchr(hostname
, '/')) != NULL
) {
267 rhent
.rh_prefix
= strtol(cp1
, &cp2
, 0);
268 if (*cp2
!= '\0' || errno
!= 0 || rhent
.rh_prefix
< 0) {
269 (void) fprintf(stderr
, gettext("tnct: invalid "
270 "prefix length: %s\n"), cp
);
274 rhent
.rh_prefix
= -1;
277 /* Strip any backslashes from numeric address */
278 hostname_p
= malloc(strlen(hostname
)+1);
279 if (hostname_p
== NULL
) {
284 while (*hostname
!= '\0' && *hostname
!= '/') {
291 /* Convert address or hostname to binary af_inet6 format */
292 hp
= getipnodebyname(hostname_p
, AF_INET6
,
293 AI_ALL
| AI_ADDRCONFIG
| AI_V4MAPPED
, &err
);
295 (void) fprintf(stderr
, gettext("tnctl: unknown host "
296 "or invalid literal address: %s\n"), hostname_p
);
297 if (err
== TRY_AGAIN
)
298 (void) fprintf(stderr
,
299 gettext("\t(try again later)\n"));
303 (void) memcpy(&ipv6addr
, hp
->h_addr
, hp
->h_length
);
305 /* if ipv4 address, convert to af_inet format */
306 if (IN6_IS_ADDR_V4MAPPED(&ipv6addr
)) {
307 rhent
.rh_address
.ta_family
= AF_INET
;
308 IN6_V4MAPPED_TO_INADDR(&ipv6addr
,
309 &rhent
.rh_address
.ta_addr_v4
);
310 if (rhent
.rh_prefix
== -1)
311 rhent
.rh_prefix
= 32;
313 rhent
.rh_address
.ta_family
= AF_INET6
;
314 rhent
.rh_address
.ta_addr_v6
= ipv6addr
;
315 if (rhent
.rh_prefix
== -1)
316 rhent
.rh_prefix
= 128;
318 rhent
.rh_template
[0] = '\0';
322 /* produce ascii format of address and prefix length */
323 translate_inet_addr(rhentp
, &alen
, abuf
, sizeof (abuf
));
326 * look up the entry from ldap or tnrhdb if this is a load
327 * request and a template name was not provided.
330 rhentp
->rh_template
[0] == '\0' &&
331 (rhentp
= tsol_getrhbyaddr(abuf
, alen
+1,
332 rhent
.rh_address
.ta_family
)) == NULL
) {
333 (void) fprintf(stderr
,
334 gettext("tnctl: database lookup failed for %s\n"),
340 (void) printf("%s rh entry %s\n", delete_mode
? "deleting" :
343 /* update the tnrhdb entry in the kernel */
344 if (tnrh(delete_mode
? TNDB_DELETE
: TNDB_LOAD
, rhentp
) != 0) {
347 else if (errno
== ENOENT
)
348 (void) fprintf(stderr
,
349 gettext("tnctl: %1$s of remote-host kernel cache "
350 "entry %2$s failed: no such entry\n"),
351 delete_mode
? gettext("delete") : gettext("load"),
354 (void) fprintf(stderr
,
355 gettext("tnctl: %1$s of remote-host kernel cache "
356 "entry %2$s failed: %3$s\n"),
357 delete_mode
? gettext("delete") : gettext("load"),
358 abuf
, strerror(errno
));
361 if (rhentp
!= &rhent
)
362 tsol_freerhent(rhentp
);
366 handle_mlps(zoneid_t zoneid
, tsol_mlp_t
*mlp
, int flags
, int cmd
)
370 tsme
.tsme_zoneid
= zoneid
;
371 tsme
.tsme_flags
= flags
;
372 while (!TSOL_MLP_END(mlp
)) {
373 tsme
.tsme_mlp
= *mlp
;
374 if (tnmlp(cmd
, &tsme
) != 0) {
376 * Usage of ?: here is ugly, but helps with
379 (void) fprintf(stderr
,
380 flags
& TSOL_MEF_SHARED
?
381 gettext("tnctl: cannot set "
382 "shared MLP on %1$d-%2$d/%3$d: %4$s\n") :
383 gettext("tnctl: cannot set "
384 "zone-specific MLP on %1$d-%2$d/%3$d: %4$s\n"),
385 mlp
->mlp_port
, mlp
->mlp_port_upper
, mlp
->mlp_ipp
,
394 * This reads the configuration for the global zone out of tnzonecfg
395 * and sets it in the kernel. The non-global zones are configured
399 process_tnzone(const char *file
)
406 char line
[2048], *cp
;
409 if ((fp
= fopen(file
, "r")) == NULL
) {
410 (void) fprintf(stderr
,
411 gettext("tnctl: failed to open %s: %s\n"), file
,
416 linenum
= errors
= 0;
418 while (fgets(line
, sizeof (line
), fp
) != NULL
) {
419 if ((cp
= strchr(line
, '\n')) != NULL
)
423 if ((zc
= tsol_sgetzcent(line
, &err
, &errstr
)) == NULL
) {
424 if (err
== LTSNET_EMPTY
)
429 (void) fprintf(stderr
, gettext("tnctl: errors "
430 "parsing %s:\n"), file
);
433 print_error(linenum
, err
, errstr
);
438 if (strcasecmp(zc
->zc_name
, "global") == 0)
445 (void) fprintf(stderr
,
446 gettext("tnctl: cannot find global zone in %s\n"), file
);
450 tsme
.tsme_zoneid
= GLOBAL_ZONEID
;
453 (void) tnmlp(TNDB_FLUSH
, &tsme
);
455 handle_mlps(GLOBAL_ZONEID
, zc
->zc_private_mlp
, 0, TNDB_LOAD
);
456 handle_mlps(GLOBAL_ZONEID
, zc
->zc_shared_mlp
, TSOL_MEF_SHARED
,
463 process_tpl(const char *file
)
466 boolean_t error
= B_FALSE
;
467 boolean_t success
= B_FALSE
;
468 tsol_tpent_t
*tpentp
;
470 if ((fp
= fopen(file
, "r")) == NULL
) {
471 (void) fprintf(stderr
,
472 gettext("tnctl: failed to open %s: %s\n"), file
,
478 while (tpentp
= tsol_fgettpent(fp
, &error
)) {
479 /* First time through the loop, flush it all */
480 if (!success
&& flush_mode
)
481 (void) tnrhtp(TNDB_FLUSH
, NULL
);
486 (void) printf("tnctl: loading rhtp entry ...\n");
488 if (tnrhtp(TNDB_LOAD
, tpentp
) != 0) {
493 (void) fprintf(stderr
, gettext("tnctl: load "
494 "of remote-host template %1$s into kernel "
495 "cache failed: %2$s\n"), tpentp
->name
,
500 tsol_freetpent(tpentp
);
503 (void) fprintf(stderr
,
504 gettext("tnctl: No valid tnrhtp entries found in %s\n"),
515 process_tp(const char *template)
519 tsol_tpent_t
*tpentp
;
522 char buf
[NSS_BUFLEN_TSOL_TP
];
524 if (strchr(template, ':') != NULL
) {
525 (void) str_to_tpstr(template, strlen(template), &tpstr
, buf
,
527 tpentp
= tpstr_to_ent(&tpstr
, &err
, &errstr
);
528 if (tpentp
== NULL
) {
529 print_error(0, err
, errstr
);
532 } else if (delete_mode
) {
533 (void) memset(&tpent
, 0, sizeof (tpent
));
535 (void) strlcpy(tpentp
->name
, template, sizeof (tpentp
->name
));
536 } else if ((tpentp
= tsol_gettpbyname(template)) == NULL
) {
537 (void) fprintf(stderr
,
538 gettext("tnctl: template %s not found\n"), template);
543 (void) printf("%s rhtp entry ...\n", delete_mode
? "deleting" :
546 if (tnrhtp(delete_mode
? TNDB_DELETE
: TNDB_LOAD
, tpentp
) != 0) {
549 else if (errno
== ENOENT
)
550 (void) fprintf(stderr
,
551 gettext("tnctl: %1$s of remote-host template "
552 "kernel cache entry %2$s failed: no such "
554 delete_mode
? gettext("delete") : gettext("load"),
557 (void) fprintf(stderr
,
558 gettext("tnctl: %1$s of remote-host template "
559 "kernel cache entry %2$s failed: %3$s\n"),
560 delete_mode
? gettext("delete") : gettext("load"),
561 tpentp
->name
, strerror(errno
));
564 if (tpentp
!= &tpent
)
565 tsol_freetpent(tpentp
);
569 process_mlp(const char *str
)
572 char zonename
[ZONENAME_MAX
];
579 if ((cp
= strchr(str
, ':')) == NULL
) {
581 (void) fprintf(stderr
,
582 gettext("tnctl: need MLP list to insert\n"));
585 (void) strlcpy(zonename
, str
, sizeof (zonename
));
586 } else if (cp
- str
>= ZONENAME_MAX
) {
587 (void) fprintf(stderr
, gettext("tnctl: illegal zone name\n"));
590 (void) memcpy(zonename
, str
, cp
- str
);
591 zonename
[cp
- str
] = '\0';
595 if ((zoneid
= getzoneidbyname(zonename
)) == -1) {
596 (void) fprintf(stderr
, gettext("tninfo: zone '%s' unknown\n"),
601 sbuf
= malloc(strlen(zonename
) + sizeof (":ADMIN_LOW:0:") +
607 /* LINTED: sprintf is known not to be unbounded here */
608 (void) sprintf(sbuf
, "%s:ADMIN_LOW:0:%s", zonename
, str
);
609 if ((zc
= tsol_sgetzcent(sbuf
, &err
, &errstr
)) == NULL
) {
610 (void) fprintf(stderr
,
611 gettext("tnctl: unable to parse MLPs\n"));
614 handle_mlps(zoneid
, zc
->zc_private_mlp
, 0,
615 delete_mode
? TNDB_DELETE
: TNDB_LOAD
);
616 handle_mlps(zoneid
, zc
->zc_shared_mlp
, TSOL_MEF_SHARED
,
617 delete_mode
? TNDB_DELETE
: TNDB_LOAD
);
624 (void) fprintf(stderr
, gettext("usage: tnctl [-dfv] "
625 "[-h host[/prefix][:tmpl]] [-m zone:priv:share]\n\t"
626 "[-t tmpl[:key=val[;key=val]]] [-[HTz] file]\n"));