Code

Initial commit.
[libjunos.git] / src / netrc.c
1 /*
2  * libJUNOS - src/netrc.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  * Access .netrc information.
30  */
32 #include "junos.h"
34 #include <stdio.h>
35 #include <stdlib.h>
37 #include <string.h>
38 #include <strings.h>
40 /*
41  * private data structures
42  */
44 enum {
45         TOKEN_NONE = 0,
46         TOKEN_MACHINE,
47         TOKEN_LOGIN,
48         TOKEN_PASSWORD,
49         TOKEN_ACCOUNT,
50         TOKEN_MACDEF,
51 };
53 struct junos_netrc {
54         char *filename;
56         junos_netrc_entry_t *entries;
57         size_t               entries_num;
58 };
60 /*
61  * private helper functions
62  */
64 static int
65 netrc_add_machine(junos_netrc_t *netrc)
66 {
67         junos_netrc_entry_t *tmp;
69         tmp = realloc(netrc->entries, netrc->entries_num + 1);
70         if (! tmp)
71                 return -1;
73         netrc->entries = tmp;
74         ++netrc->entries_num;
76         memset(netrc->entries + (netrc->entries_num - 1), 0,
77                         sizeof(*netrc->entries));
78         return 0;
79 } /* netrc_add_machine */
81 static int
82 netrc_set_value(junos_netrc_t *netrc, int token, char *value)
83 {
84         junos_netrc_entry_t *entry;
85         char **target = NULL;
87         if (! netrc->entries_num)
88                 return -1;
90         entry = netrc->entries + (netrc->entries_num - 1);
92         switch (token) {
93                 case TOKEN_NONE: /* fall thru */
94                 case TOKEN_MACDEF: /* fall thru */
95                 case TOKEN_ACCOUNT:
96                         break;
97                 case TOKEN_MACHINE:
98                         target = &entry->machine;
99                         break;
100                 case TOKEN_LOGIN:
101                         target = &entry->login;
102                         break;
103                 case TOKEN_PASSWORD:
104                         target = &entry->password;
105                         break;
106         }
108         if (target) {
109                 if (*target)
110                         free(*target);
111                 *target = strdup(value);
112         }
113         return 0;
114 } /* netrc_set_value */
116 static int
117 netrc_parse_line(char *line, junos_netrc_t *netrc, int *last_token)
119         char *lasts_ptr = NULL;
120         char *token;
122         if ((*last_token == TOKEN_MACDEF) && (line[0] != '\0'))
123                 return 0; /* skip line */
125         while ((token = strtok_r(line, " \t\r\n", &lasts_ptr))) {
126                 line = NULL;
128                 if (*last_token != TOKEN_NONE) {
129                         netrc_set_value(netrc, *last_token, token);
130                         *last_token = TOKEN_NONE;
131                 }
132                 else if (! strcasecmp(token, "machine")) {
133                         *last_token = TOKEN_MACHINE;
134                         if (netrc_add_machine(netrc))
135                                 return -1;
136                 }
137                 else if (! strcasecmp(token, "default")) {
138                         *last_token = TOKEN_NONE;
139                         if (netrc_add_machine(netrc))
140                                 return -1;
141                 }
142                 else if (! strcasecmp(token, "login"))
143                         *last_token = TOKEN_LOGIN;
144                 else if (! strcasecmp(token, "password"))
145                         *last_token = TOKEN_PASSWORD;
146                 else if (! strcasecmp(token, "account"))
147                         *last_token = TOKEN_ACCOUNT;
148                 else if (! strcasecmp(token, "macdef")) {
149                         *last_token = TOKEN_MACDEF;
150                         return 0;
151                 }
152         }
153         return 0;
154 } /* netrc_parse_line */
156 static int
157 netrc_read(FILE *fh, junos_netrc_t *netrc)
159         char  line_buf[1024];
160         char *line;
162         int status;
163         int last_token = TOKEN_NONE;
165         while ((line = fgets(line_buf, sizeof(line_buf), fh)))
166                 if ((status = netrc_parse_line(line, netrc, &last_token)))
167                         return status;
169         if (! feof(fh))
170                 return -1;
171         return 0;
172 } /* netrc_read */
174 /*
175  * public API
176  */
178 junos_netrc_t *
179 junos_netrc_read(char *filename)
181         char  buf[1024];
182         FILE *fh;
184         junos_netrc_t *netrc;
186         if (! filename) {
187                 char *home_dir = getenv("HOME");
188                 if (! home_dir) {
189                         dprintf("Failed to determine home directory\n");
190                         return NULL;
191                 }
193                 snprintf(buf, sizeof(buf), "%s/.netrc", home_dir);
194                 filename = buf;
195         }
197         fh = fopen(filename, "r");
198         if (! fh) {
199                 dprintf("Failed to open '%s'\n", filename);
200                 return NULL;
201         }
203         netrc = calloc(1, sizeof(*netrc));
204         if (netrc)
205                 netrc->filename = strdup(filename);
207         if ((! netrc) || (! netrc->filename)) {
208                 dprintf("Failed to allocate libJUNOS netrc object\n");
209                 fclose(fh);
210                 junos_netrc_free(netrc);
211                 return NULL;
212         }
214         if (netrc_read(fh, netrc)) {
215                 dprintf("Failed to parse .netrc\n");
216                 fclose(fh);
217                 junos_netrc_free(netrc);
218                 return NULL;
219         }
221         fclose(fh);
222         return netrc;
223 } /* junos_netrc_read */
225 void
226 junos_netrc_free(junos_netrc_t *netrc)
228         size_t i;
230         if (! netrc)
231                 return;
233         if (netrc->filename)
234                 free(netrc->filename);
236         for (i = 0; i < netrc->entries_num; ++i) {
237                 junos_netrc_entry_t *entry = netrc->entries + i;
239                 if (entry->machine)
240                         free(entry->machine);
241                 if (entry->login)
242                         free(entry->login);
243                 if (entry->password)
244                         free(entry->password);
245         }
246         free(netrc->entries);
247         free(netrc);
248 } /* junos_netrc_free */
250 const junos_netrc_entry_t *
251 junos_netrc_lookup(junos_netrc_t *netrc, char *hostname)
253         size_t i;
255         if ((! netrc) || (! hostname))
256                 return NULL;
258         for (i = 0; i < netrc->entries_num; ++i) {
259                 junos_netrc_entry_t *entry = netrc->entries + i;
261                 if ((! entry->machine) || (! strcasecmp(hostname, entry->machine)))
262                         return entry;
263         }
264         return NULL;
265 } /* junos_netrc_lookup */
267 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */