[5d1cb8a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup dnscfg
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file DNS configuration utility.
|
---|
| 33 | *
|
---|
| 34 | * Controls the DNS resolution server (@c dnsrsrv).
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <errno.h>
|
---|
[a2e3ee6] | 38 | #include <inet/addr.h>
|
---|
[5d1cb8a] | 39 | #include <inet/dnsr.h>
|
---|
| 40 | #include <ipc/services.h>
|
---|
| 41 | #include <loc.h>
|
---|
| 42 | #include <stdio.h>
|
---|
| 43 | #include <stdlib.h>
|
---|
| 44 | #include <str_error.h>
|
---|
| 45 | #include <sys/types.h>
|
---|
| 46 |
|
---|
| 47 | #define NAME "dnscfg"
|
---|
| 48 |
|
---|
| 49 | static void print_syntax(void)
|
---|
| 50 | {
|
---|
[3e66428] | 51 | printf("Syntax:\n");
|
---|
| 52 | printf("\t%s get-ns\n", NAME);
|
---|
| 53 | printf("\t%s set-ns <server-addr>\n", NAME);
|
---|
| 54 | printf("\t%s unset-ns\n", NAME);
|
---|
[5d1cb8a] | 55 | }
|
---|
| 56 |
|
---|
| 57 | static int dnscfg_set_ns(int argc, char *argv[])
|
---|
| 58 | {
|
---|
| 59 | if (argc < 1) {
|
---|
[3e66428] | 60 | printf("%s: Missing arguments.\n", NAME);
|
---|
[5d1cb8a] | 61 | print_syntax();
|
---|
| 62 | return EINVAL;
|
---|
| 63 | }
|
---|
[3e66428] | 64 |
|
---|
[5d1cb8a] | 65 | if (argc > 1) {
|
---|
[3e66428] | 66 | printf("%s: Too many arguments.\n", NAME);
|
---|
[5d1cb8a] | 67 | print_syntax();
|
---|
| 68 | return EINVAL;
|
---|
| 69 | }
|
---|
[3e66428] | 70 |
|
---|
| 71 | char *srv_addr = argv[0];
|
---|
| 72 |
|
---|
[a2e3ee6] | 73 | inet_addr_t addr;
|
---|
| 74 | int rc = inet_addr_parse(srv_addr, &addr);
|
---|
[3e66428] | 75 |
|
---|
[5d1cb8a] | 76 | if (rc != EOK) {
|
---|
[3e66428] | 77 | printf("%s: Invalid address format '%s'.\n", NAME, srv_addr);
|
---|
| 78 | return rc;
|
---|
[5d1cb8a] | 79 | }
|
---|
[3e66428] | 80 |
|
---|
[5d1cb8a] | 81 | rc = dnsr_set_srvaddr(&addr);
|
---|
| 82 | if (rc != EOK) {
|
---|
[3e66428] | 83 | printf("%s: Failed setting nameserver address '%s' (%s)\n",
|
---|
| 84 | NAME, srv_addr, str_error(rc));
|
---|
| 85 | return rc;
|
---|
[5d1cb8a] | 86 | }
|
---|
[3e66428] | 87 |
|
---|
[5d1cb8a] | 88 | return EOK;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[3e66428] | 91 | static int dnscfg_unset_ns(void)
|
---|
[5d1cb8a] | 92 | {
|
---|
[a2e3ee6] | 93 | inet_addr_t addr;
|
---|
| 94 | inet_addr_any(&addr);
|
---|
[3e66428] | 95 |
|
---|
| 96 | int rc = dnsr_set_srvaddr(&addr);
|
---|
[5d1cb8a] | 97 | if (rc != EOK) {
|
---|
[3e66428] | 98 | printf("%s: Failed unsetting server address (%s)\n",
|
---|
| 99 | NAME, str_error(rc));
|
---|
| 100 | return rc;
|
---|
[5d1cb8a] | 101 | }
|
---|
[3e66428] | 102 |
|
---|
[5d1cb8a] | 103 | return EOK;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | static int dnscfg_print(void)
|
---|
| 107 | {
|
---|
[a2e3ee6] | 108 | inet_addr_t addr;
|
---|
[3e66428] | 109 | int rc = dnsr_get_srvaddr(&addr);
|
---|
[5d1cb8a] | 110 | if (rc != EOK) {
|
---|
[3e66428] | 111 | printf("%s: Failed getting DNS server address.\n", NAME);
|
---|
[5d1cb8a] | 112 | return rc;
|
---|
| 113 | }
|
---|
[3e66428] | 114 |
|
---|
| 115 | char *addr_str;
|
---|
[a2e3ee6] | 116 | rc = inet_addr_format(&addr, &addr_str);
|
---|
[5d1cb8a] | 117 | if (rc != EOK) {
|
---|
[3e66428] | 118 | printf("%s: Out of memory.\n", NAME);
|
---|
[5d1cb8a] | 119 | return rc;
|
---|
| 120 | }
|
---|
[3e66428] | 121 |
|
---|
| 122 | printf("Nameserver: %s\n", addr_str);
|
---|
[5d1cb8a] | 123 | free(addr_str);
|
---|
| 124 | return EOK;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | int main(int argc, char *argv[])
|
---|
| 128 | {
|
---|
[3e66428] | 129 | if ((argc < 2) || (str_cmp(argv[1], "get-ns") == 0))
|
---|
| 130 | return dnscfg_print();
|
---|
| 131 | else if (str_cmp(argv[1], "set-ns") == 0)
|
---|
| 132 | return dnscfg_set_ns(argc - 2, argv + 2);
|
---|
| 133 | else if (str_cmp(argv[1], "unset-ns") == 0)
|
---|
| 134 | return dnscfg_unset_ns();
|
---|
| 135 | else {
|
---|
| 136 | printf("%s: Unknown command '%s'.\n", NAME, argv[1]);
|
---|
[5d1cb8a] | 137 | print_syntax();
|
---|
| 138 | return 1;
|
---|
| 139 | }
|
---|
[3e66428] | 140 |
|
---|
[5d1cb8a] | 141 | return 0;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /** @}
|
---|
| 145 | */
|
---|