Code

Initial commit.
[libjunos.git] / src / junosc.c
1 /*
2  * libJUNOS - src/junosc.c
3  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 /*
29  * A JUNOScript client application.
30  */
32 #if HAVE_CONFIG_H
33 #       include "config.h"
34 #endif /* HAVE_CONFIG_H */
36 #include "junos.h"
37 #include "libjunos_features.h"
39 #if HAVE_LIBGEN_H
40 #       include <libgen.h>
41 #else /* HAVE_LIBGEN_H */
42 #       define basename(path) (path)
43 #endif /* ! HAVE_LIBGEN_H */
45 #include <stdio.h>
46 #include <stdlib.h>
48 #include <unistd.h>
50 static void
51 exit_usage(char *name, int status)
52 {
53         printf(
54 "Usage: %s -H <host> -u <user> [<options>] <command>\n"
56 "\nOptions:\n"
57 "  -H <host>    hostname to connect to\n"
58 "  -u <user>    username to connect with\n"
59 "  -p <pass>    password to connect with\n"
60 "  -n           do not use .netrc for username/password lookup\n"
61 "\n"
62 "  -h           display this help and exit\n"
63 "  -V           display the version number and copyright\n"
65 "\njunosc "LIBJUNOS_VERSION_STRING LIBJUNOS_VERSION_EXTRA", "PACKAGE_URL"\n",
66 basename(name));
67         exit(status);
68 } /* exit_usage */
70 static void
71 exit_version(void)
72 {
73         printf("junosc version "LIBJUNOS_VERSION_STRING LIBJUNOS_VERSION_EXTRA", "
74                         "built "BUILD_DATE"\n"
75                         "Copyright (C) 2012 "PACKAGE_MAINTAINER"\n"
77                         "\nThis is free software under the terms of the BSD license, see "
78                         "the source for\ncopying conditions. There is NO WARRANTY; not "
79                         "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
80                         "PURPOSE.\n");
81         exit(0);
82 } /* exit_version */
84 int
85 main(int argc, char **argv)
86 {
87         junos_netrc_t *netrc;
89         int use_netrc  = 1;
91         char *hostname = NULL;
92         char *username = NULL;
93         char *password = NULL;
95         char *command  = NULL;
97         junos_t *junos;
98         xmlDocPtr doc;
100         while (42) {
101                 int opt = getopt(argc, argv, "H:u:p:nhV");
103                 if (-1 == opt)
104                         break;
106                 switch (opt) {
107                         case 'H':
108                                 hostname = optarg;
109                                 break;
110                         case 'u':
111                                 username = optarg;
112                                 break;
113                         case 'p':
114                                 password = optarg;
115                                 break;
116                         case 'n':
117                                 use_netrc = 0;
118                                 break;
120                         case 'h':
121                                 exit_usage(argv[0], 0);
122                                 break;
123                         case 'V':
124                                 exit_version();
125                                 break;
126                         default:
127                                 exit_usage(argv[0], 1);
128                 }
129         }
131         if (! hostname) {
132                 fprintf(stderr, "Missing hostname\n");
133                 exit_usage(argv[0], 1);
134         }
136         if (optind != argc - 1) {
137                 fprintf(stderr, "Missing command name\n");
138                 exit_usage(argv[0], 1);
139         }
141         command = argv[optind];
142         ++optind;
144         if (use_netrc
145                         && (netrc = junos_netrc_read(/* filename = default */ NULL))) {
146                 const junos_netrc_entry_t *entry;
148                 entry = junos_netrc_lookup(netrc, hostname);
149                 if (entry) {
150                         if ((! username) && (entry->login)) {
151                                 dprintf("Using username '%s' from netrc\n", entry->login);
152                                 username = entry->login;
153                         }
155                         if ((! password) && (entry->password)) {
156                                 dprintf("Using password from netrc\n");
157                                 password = entry->password;
158                         }
159                 }
160         }
162         if (! username)
163                 username = getlogin();
165         if (! username) {
166                 fprintf(stderr, "Missing username\n");
167                 if (netrc)
168                         junos_netrc_free(netrc);
169                 exit_usage(argv[0], 1);
170         }
172         if (junos_init()) {
173                 fprintf(stderr, "FATAL: Failed to initialize libJUNOS. Aborting.\n");
174                 exit(1);
175         }
177         junos = junos_new(hostname, username, password);
178         if (! junos) {
179                 fprintf(stderr, "FATAL: Failed to create JUNOS object!\n");
180                 exit(1);
181         }
183         if (junos_connect(junos)) {
184                 fprintf(stderr, "Failed to connect: %s\n", junos_get_errstr(junos));
185                 junos_free(junos);
186                 exit(1);
187         }
189         junos_clear_error(junos);
190         doc = junos_simple_command(junos, command);
191         if (doc) {
192                 xmlDocFormatDump(stderr, doc, /* format = */ 1);
193                 xmlFreeDoc(doc);
194         }
195         else {
196                 fprintf(stderr, "Command failed: %s\n", junos_get_errstr(junos));
197         }
199         junos_disconnect(junos);
200         junos_free(junos);
202         if (netrc)
203                 junos_netrc_free(netrc);
204         return 0;
205 } /* main */
207 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */