wrapsrv-1.0.0/000077500000000000000000000000001236626075200132255ustar00rootroot00000000000000wrapsrv-1.0.0/COPYRIGHT000066400000000000000000000010771236626075200145250ustar00rootroot00000000000000Copyright (c) 2009-2014 by Farsight Security, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. wrapsrv-1.0.0/LICENSE000066400000000000000000000261361236626075200142420ustar00rootroot00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. wrapsrv-1.0.0/Makefile000066400000000000000000000010521236626075200146630ustar00rootroot00000000000000CC = gcc WARN = -Wall -Wextra -Werror CFLAGS = -O2 -g $(WARN) INCLUDE = LDFLAGS = -lresolv DESTDIR ?= PREFIX = /usr/local BINDIR ?= $(DESTDIR)$(PREFIX)/bin MANDIR ?= $(DESTDIR)$(PREFIX)/share/man/man1 BIN = wrapsrv MAN = wrapsrv.1 SRC = wrapsrv.c all: $(BIN) $(DOC) $(BIN): $(SRC) $(CC) $(CFLAGS) -o $@ $(SRC) $(INCLUDE) $(LDFLAGS) $(MAN): wrapsrv.docbook docbook2x-man $< clean: rm -f $(BIN) install: $(BIN) mkdir -p $(BINDIR) mkdir -p $(MANDIR) install -m 0755 $(BIN) $(BINDIR) install -m 0644 $(MAN) $(MANDIR) .PHONY: all clean install wrapsrv-1.0.0/README.md000066400000000000000000000010031236626075200144760ustar00rootroot00000000000000DNS SRV record command line wrapper ----------------------------------- wrapsrv adds support for connecting to a network service based on DNS SRV record lookups to commands that do not support the DNS SRV record. wrapsrv implements the weighted priority client connection algorithm in [RFC 2782](http://tools.ietf.org/html/rfc2782). The specified command line will be invoked one or more times with %h and %p sequences in the command line substituted for the hostname and port elements of the selected SRV record. wrapsrv-1.0.0/list.h000066400000000000000000000131551236626075200143560ustar00rootroot00000000000000/* * Copyright (C) 2004, 2006, 2007 Internet Systems Consortium, Inc. ("ISC") * Copyright (C) 1997-2002 Internet Software Consortium. * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ #ifndef ISC_LIST_H #define ISC_LIST_H 1 #define ISC_INSIST(x) assert(x) #define ISC_LINK_INSIST(x) assert(x) #define ISC_LIST(type) struct { type *head, *tail; } #define ISC_LIST_INIT(list) \ do { (list).head = NULL; (list).tail = NULL; } while (0) #define ISC_LINK(type) struct { type *prev, *next; } #define ISC_LINK_INIT_TYPE(elt, link, type) \ do { \ (elt)->link.prev = (type *)(-1); \ (elt)->link.next = (type *)(-1); \ } while (0) #define ISC_LINK_INIT(elt, link) \ ISC_LINK_INIT_TYPE(elt, link, void) #define ISC_LINK_LINKED(elt, link) ((void *)((elt)->link.prev) != (void *)(-1)) #define ISC_LIST_HEAD(list) ((list).head) #define ISC_LIST_TAIL(list) ((list).tail) #define ISC_LIST_EMPTY(list) ((list.head) == NULL ? 1 : 0) #define __ISC_LIST_PREPENDUNSAFE(list, elt, link) \ do { \ if ((list).head != NULL) \ (list).head->link.prev = (elt); \ else \ (list).tail = (elt); \ (elt)->link.prev = NULL; \ (elt)->link.next = (list).head; \ (list).head = (elt); \ } while (0) #define ISC_LIST_PREPEND(list, elt, link) \ do { \ ISC_LINK_INSIST(!ISC_LINK_LINKED(elt, link)); \ __ISC_LIST_PREPENDUNSAFE(list, elt, link); \ } while (0) #define ISC_LIST_INITANDPREPEND(list, elt, link) \ __ISC_LIST_PREPENDUNSAFE(list, elt, link) #define __ISC_LIST_APPENDUNSAFE(list, elt, link) \ do { \ if ((list).tail != NULL) \ (list).tail->link.next = (elt); \ else \ (list).head = (elt); \ (elt)->link.prev = (list).tail; \ (elt)->link.next = NULL; \ (list).tail = (elt); \ } while (0) #define ISC_LIST_APPEND(list, elt, link) \ do { \ ISC_LINK_INSIST(!ISC_LINK_LINKED(elt, link)); \ __ISC_LIST_APPENDUNSAFE(list, elt, link); \ } while (0) #define ISC_LIST_INITANDAPPEND(list, elt, link) \ __ISC_LIST_APPENDUNSAFE(list, elt, link) #define __ISC_LIST_UNLINKUNSAFE_TYPE(list, elt, link, type) \ do { \ if ((elt)->link.next != NULL) \ (elt)->link.next->link.prev = (elt)->link.prev; \ else { \ ISC_INSIST((list).tail == (elt)); \ (list).tail = (elt)->link.prev; \ } \ if ((elt)->link.prev != NULL) \ (elt)->link.prev->link.next = (elt)->link.next; \ else { \ ISC_INSIST((list).head == (elt)); \ (list).head = (elt)->link.next; \ } \ (elt)->link.prev = (type *)(-1); \ (elt)->link.next = (type *)(-1); \ } while (0) #define __ISC_LIST_UNLINKUNSAFE(list, elt, link) \ __ISC_LIST_UNLINKUNSAFE_TYPE(list, elt, link, void) #define ISC_LIST_UNLINK_TYPE(list, elt, link, type) \ do { \ ISC_LINK_INSIST(ISC_LINK_LINKED(elt, link)); \ __ISC_LIST_UNLINKUNSAFE_TYPE(list, elt, link, type); \ } while (0) #define ISC_LIST_UNLINK(list, elt, link) \ ISC_LIST_UNLINK_TYPE(list, elt, link, void) #define ISC_LIST_PREV(elt, link) ((elt)->link.prev) #define ISC_LIST_NEXT(elt, link) ((elt)->link.next) #define __ISC_LIST_INSERTBEFOREUNSAFE(list, before, elt, link) \ do { \ if ((before)->link.prev == NULL) \ ISC_LIST_PREPEND(list, elt, link); \ else { \ (elt)->link.prev = (before)->link.prev; \ (before)->link.prev = (elt); \ (elt)->link.prev->link.next = (elt); \ (elt)->link.next = (before); \ } \ } while (0) #define ISC_LIST_INSERTBEFORE(list, before, elt, link) \ do { \ ISC_LINK_INSIST(ISC_LINK_LINKED(before, link)); \ ISC_LINK_INSIST(!ISC_LINK_LINKED(elt, link)); \ __ISC_LIST_INSERTBEFOREUNSAFE(list, before, elt, link); \ } while (0) #define __ISC_LIST_INSERTAFTERUNSAFE(list, after, elt, link) \ do { \ if ((after)->link.next == NULL) \ ISC_LIST_APPEND(list, elt, link); \ else { \ (elt)->link.next = (after)->link.next; \ (after)->link.next = (elt); \ (elt)->link.next->link.prev = (elt); \ (elt)->link.prev = (after); \ } \ } while (0) #define ISC_LIST_INSERTAFTER(list, after, elt, link) \ do { \ ISC_LINK_INSIST(ISC_LINK_LINKED(after, link)); \ ISC_LINK_INSIST(!ISC_LINK_LINKED(elt, link)); \ __ISC_LIST_INSERTAFTERUNSAFE(list, after, elt, link); \ } while (0) #define ISC_LIST_APPENDLIST(list1, list2, link) \ do { \ if (ISC_LIST_EMPTY(list1)) \ (list1) = (list2); \ else if (!ISC_LIST_EMPTY(list2)) { \ (list1).tail->link.next = (list2).head; \ (list2).head->link.prev = (list1).tail; \ (list1).tail = (list2).tail; \ } \ (list2).head = NULL; \ (list2).tail = NULL; \ } while (0) #define ISC_LIST_ENQUEUE(list, elt, link) ISC_LIST_APPEND(list, elt, link) #define __ISC_LIST_ENQUEUEUNSAFE(list, elt, link) \ __ISC_LIST_APPENDUNSAFE(list, elt, link) #define ISC_LIST_DEQUEUE(list, elt, link) \ ISC_LIST_UNLINK_TYPE(list, elt, link, void) #define ISC_LIST_DEQUEUE_TYPE(list, elt, link, type) \ ISC_LIST_UNLINK_TYPE(list, elt, link, type) #define __ISC_LIST_DEQUEUEUNSAFE(list, elt, link) \ __ISC_LIST_UNLINKUNSAFE_TYPE(list, elt, link, void) #define __ISC_LIST_DEQUEUEUNSAFE_TYPE(list, elt, link, type) \ __ISC_LIST_UNLINKUNSAFE_TYPE(list, elt, link, type) #endif /* ISC_LIST_H */ wrapsrv-1.0.0/wrapsrv.1000066400000000000000000000022171236626075200150150ustar00rootroot00000000000000'\" -*- coding: us-ascii -*- .if \n(.g .ds T< \\FC .if \n(.g .ds T> \\F[\n[.fam]] .de URL \\$2 \(la\\$1\(ra\\$3 .. .if \n(.g .mso www.tmac .TH wrapsrv 1 "13 December 2013" "" "" .SH NAME wrapsrv \- DNS SRV record command line wrapper .SH SYNOPSIS 'nh .fi .ad l \fBwrapsrv\fR \kx .if (\nx>(\n(.l/2)) .nr x (\n(.l/5) 'in \n(.iu+\nxu [\fISRVNAME\fR] [\fICOMMAND\fR] [\fIOPTION\fR]\&... 'in \n(.iu-\nxu .ad b 'hy .SH DESCRIPTION \fBwrapsrv\fR adds support for connecting to a network service based on DNS SRV record lookups to commands that do not support the DNS SRV record. \fBwrapsrv\fR implements the weighted priority client connection algorithm in RFC 2782. The specified command line will be invoked one or more times with \fI%h\fR and \fI%p\fR sequences in the command line substituted for the hostname and port elements of the selected SRV record. The command line invoked must exit and return 0 after a successful connection or exit and return non-zero if the connection fails. \fBwrapsrv\fR will itself exit with the return code of the last command line invocation, or it may exit with a non-zero return code if the DNS SRV record lookup fails for any reason. wrapsrv-1.0.0/wrapsrv.c000066400000000000000000000200151236626075200150730ustar00rootroot00000000000000/* * Copyright (c) 2009, 2011, 2014 by Farsight Security, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* Import. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "list.h" #ifndef NS_MAXMSG #define NS_MAXMSG 65535 #endif /* Data types. */ struct srv { ISC_LINK(struct srv) link; char *tname; uint16_t weight; uint16_t port; }; struct srv_prio { ISC_LINK(struct srv_prio) link; ISC_LIST(struct srv) srv_list; uint16_t prio; }; typedef ISC_LIST(struct srv) srv_list; typedef ISC_LIST(struct srv_prio) srv_prio_list; /* Globals. */ static srv_prio_list prio_list; /* Forward. */ static char *subst_cmd(struct srv *, const char *); static char *target_name(const unsigned char *); static int do_cmd(struct srv *, int, char **); static struct srv *next_tuple(void); static void free_tuples(void); static void insert_tuple(char *, uint16_t, uint16_t, uint16_t); static void parse_answer_section(ns_msg *); static void usage(void); #ifdef DEBUG static void print_tuples(void); #endif /* Functions. */ static struct srv * next_tuple(void) { struct srv_prio *pe; struct srv *se; uint16_t rnd; unsigned csum = 0; unsigned wsum = 0; pe = ISC_LIST_HEAD(prio_list); if (pe == NULL) return (NULL); for (se = ISC_LIST_HEAD(pe->srv_list); se != NULL; se = ISC_LIST_NEXT(se, link)) { wsum += se->weight; } rnd = random() % (wsum + 1); for (se = ISC_LIST_HEAD(pe->srv_list); se != NULL; se = ISC_LIST_NEXT(se, link)) { csum += se->weight; if (csum >= rnd) { ISC_LIST_UNLINK(pe->srv_list, se, link); break; } } if (se == NULL) { ISC_LIST_UNLINK(prio_list, pe, link); free(pe); return (next_tuple()); } #ifdef DEBUG fprintf(stderr, "rnd=%hu -> prio=%hu weight=%hu port=%hu tname=%s\n", rnd, pe->prio, se->weight, se->port, se->tname); #endif return (se); } static void free_tuples(void) { struct srv_prio *pe, *pe_next; struct srv *se, *se_next; pe = ISC_LIST_HEAD(prio_list); while (pe != NULL) { pe_next = ISC_LIST_NEXT(pe, link); ISC_LIST_UNLINK(prio_list, pe, link); se = ISC_LIST_HEAD(pe->srv_list); while (se != NULL) { se_next = ISC_LIST_NEXT(se, link); ISC_LIST_UNLINK(pe->srv_list, se, link); free(se->tname); free(se); se = se_next; } free(pe); pe = pe_next; } } static void insert_tuple(char *tname, uint16_t prio, uint16_t weight, uint16_t port) { struct srv_prio *pe; struct srv *se; for (pe = ISC_LIST_HEAD(prio_list); pe != NULL; pe = ISC_LIST_NEXT(pe, link)) { if (pe->prio == prio) break; } if (pe == NULL) { struct srv_prio *piter; pe = malloc(sizeof(*pe)); assert(pe != NULL); ISC_LINK_INIT(pe, link); ISC_LIST_INIT(pe->srv_list); pe->prio = prio; for (piter = ISC_LIST_HEAD(prio_list); piter != NULL; piter = ISC_LIST_NEXT(piter, link)) { assert(piter->prio != prio); if (piter->prio > prio) { ISC_LIST_INSERTBEFORE(prio_list, piter, pe, link); break; } } if (piter == NULL) ISC_LIST_APPEND(prio_list, pe, link); } se = malloc(sizeof(*se)); assert(se != NULL); ISC_LINK_INIT(se, link); se->tname = tname; se->weight = weight; se->port = port; ISC_LIST_APPEND(pe->srv_list, se, link); } #ifdef DEBUG static void print_tuples(void) { struct srv_prio *pe; struct srv *se; for (pe = ISC_LIST_HEAD(prio_list); pe != NULL; pe = ISC_LIST_NEXT(pe, link)) { fprintf(stderr, "prio=%hu\n", pe->prio); for (se = ISC_LIST_HEAD(pe->srv_list); se != NULL; se = ISC_LIST_NEXT(se, link)) { fprintf(stderr, "\tweight=%hu port=%hu tname=%s\n", se->weight, se->port, se->tname); } } } #endif static char * target_name(const unsigned char *target) { char buf[NS_MAXDNAME]; if (ns_name_ntop(target, buf, sizeof buf) == -1) { perror("ns_name_ntop"); exit(EXIT_FAILURE); } return (strdup(buf)); } static void parse_answer_section(ns_msg *msg) { int rrnum, rrmax; ns_rr rr; uint16_t prio, weight, port, len; const unsigned char *rdata; char *tname; rrmax = ns_msg_count(*msg, ns_s_an); for (rrnum = 0; rrnum < rrmax; rrnum++) { if (ns_parserr(msg, ns_s_an, rrnum, &rr)) { perror("ns_parserr"); exit(EXIT_FAILURE); } if (ns_rr_type(rr) == ns_t_srv) { len = ns_rr_rdlen(rr); rdata = ns_rr_rdata(rr); if (len > 3U * NS_INT16SZ) { NS_GET16(prio, rdata); NS_GET16(weight, rdata); NS_GET16(port, rdata); len -= 3U * NS_INT16SZ; tname = target_name(rdata); insert_tuple(tname, prio, weight, port); } } } } static char * subst_cmd(struct srv *se, const char *cmd) { char *q, *str; const char *p = cmd; int ch; int n_host = 0; int n_port = 0; size_t bufsz; size_t len_tname; len_tname = strlen(se->tname); while ((ch = *p++) != '\0') { if (ch == '%' && *p == 'h') n_host++; if (ch == '%' && *p == 'p') n_port++; } bufsz = strlen(cmd) + 1; bufsz -= 2 * (n_host + n_port); /* '%h' and '%p' */ bufsz += 5 * n_port; /* '%h' -> uint16_t */ bufsz += (strlen(se->tname) + 1) * n_host; str = calloc(1, bufsz); assert(str != NULL); p = cmd; q = str; while ((ch = *p++) != '\0') { if (ch == '%' && *p == 'h') { strcpy(q, se->tname); q += len_tname; p++; } else if (ch == '%' && *p == 'p') { q += sprintf(q, "%hu", se->port); p++; } else { *q++ = ch; } } return (str); } static int do_cmd(struct srv *se, int argc, char **argv) { char *cmd, *scmd, *p; int i, rc; size_t bufsz = 2; for (i = 2; i < argc; i++) { bufsz += 1; bufsz += strlen(argv[i]); } p = cmd = malloc(bufsz); assert(cmd != NULL); for (i = 2; i < argc; i++) { strcpy(p, argv[i]); p += strlen(argv[i]); if (i != argc - 1) { *p++ = ' '; } } scmd = subst_cmd(se, cmd); free(cmd); #ifdef DEBUG fprintf(stderr, "scmd='%s'\n", scmd); #endif rc = system(scmd); rc = WEXITSTATUS(rc); free(scmd); free(se->tname); free(se); #ifdef DEBUG fprintf(stderr, "rc=%i\n", rc); #endif return (rc); } static void usage(void) { fprintf(stderr, "Usage: wrapsrv [OPTION]...\n"); fprintf(stderr, "%%h and %%p sequences will be converted to " "hostname and port.\n"); exit(EXIT_FAILURE); } int main(int argc, char **argv) { char *qname; ns_msg msg; int len, rc = 0; unsigned char answer[NS_MAXMSG]; unsigned rcode; struct srv *se; struct timeval tv; unsigned int seed = 0; if (argc < 3) usage(); ISC_LIST_INIT(prio_list); gettimeofday(&tv, NULL); seed ^= (unsigned int) tv.tv_usec; seed ^= (unsigned int) getpid(); srandom(seed); res_init(); qname = argv[1]; len = res_query(qname, ns_c_in, ns_t_srv, answer, sizeof answer); if (len < 0) { herror("res_query"); return (EXIT_FAILURE); } if (ns_initparse(answer, len, &msg) < 0) { perror("ns_initparse"); return (EXIT_FAILURE); } rcode = ns_msg_getflag(msg, ns_f_rcode); if (rcode != ns_r_noerror) { fprintf(stderr, "wrapsrv: query for %s returned rcode %u\n", qname, rcode); return (EXIT_FAILURE); } if (ns_msg_count(msg, ns_s_an) == 0) { fprintf(stderr, "wrapsrv: query for %s returned no answers\n", qname); return (EXIT_FAILURE); } parse_answer_section(&msg); #ifdef DEBUG print_tuples(); fprintf(stderr, "\n"); #endif while ((se = next_tuple()) != NULL) { if ((rc = do_cmd(se, argc, argv)) == 0) break; #ifdef DEBUG fprintf(stderr, "\n"); #endif } free_tuples(); return (rc); } wrapsrv-1.0.0/wrapsrv.docbook000066400000000000000000000032721236626075200162770ustar00rootroot00000000000000 wrapsrv 1 wrapsrv DNS SRV record command line wrapper wrapsrv SRVNAME COMMAND OPTION DESCRIPTION wrapsrv adds support for connecting to a network service based on DNS SRV record lookups to commands that do not support the DNS SRV record. wrapsrv implements the weighted priority client connection algorithm in RFC 2782. The specified command line will be invoked one or more times with %h and %p sequences in the command line substituted for the hostname and port elements of the selected SRV record. The command line invoked must exit and return 0 after a successful connection or exit and return non-zero if the connection fails. wrapsrv will itself exit with the return code of the last command line invocation, or it may exit with a non-zero return code if the DNS SRV record lookup fails for any reason.