1 /*
2  *
3   ***** BEGIN LICENSE BLOCK *****
4 
5   Copyright (C) 2009-2016 Olof Hagsand and Benny Holmgren
6   Copyright (C) 2017-2019 Olof Hagsand
7 
8   This file is part of CLIXON.
9 
10   Licensed under the Apache License, Version 2.0 (the "License");
11   you may not use this file except in compliance with the License.
12   You may obtain a copy of the License at
13 
14     http://www.apache.org/licenses/LICENSE-2.0
15 
16   Unless required by applicable law or agreed to in writing, software
17   distributed under the License is distributed on an "AS IS" BASIS,
18   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   See the License for the specific language governing permissions and
20   limitations under the License.
21 
22   Alternatively, the contents of this file may be used under the terms of
23   the GNU General Public License Version 3 or later (the "GPL"),
24   in which case the provisions of the GPL are applicable instead
25   of those above. If you wish to allow use of your version of this file only
26   under the terms of the GPL, and not to allow others to
27   use your version of this file under the terms of Apache License version 2,
28   indicate your decision by deleting the provisions above and replace them with
29   the  notice and other provisions required by the GPL. If you do not delete
30   the provisions above, a recipient may use your version of this file under
31   the terms of any one of the Apache License version 2 or the GPL.
32 
33   ***** END LICENSE BLOCK *****
34 
35  */
36 
37 #ifdef HAVE_CONFIG_H
38 #include "clixon_config.h" /* generated by config & autoconf */
39 #endif
40 
41 #include <stdio.h>
42 #include <stdarg.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <inttypes.h>
46 #include <dirent.h>
47 #include <errno.h>
48 #include <unistd.h>
49 #include <assert.h>
50 #include <sys/types.h>
51 #include <sys/time.h>
52 #include <regex.h>
53 #include <syslog.h>
54 #include <netinet/in.h>
55 #include <limits.h>
56 
57 /* cligen */
58 #include <cligen/cligen.h>
59 
60 /* clicon */
61 #include <clixon/clixon.h>
62 
63 #include "clixon_backend_handle.h"
64 #include "backend_client.h"
65 #include "backend_handle.h"
66 
67 /* header part is copied from struct clicon_handle in lib/src/clicon_handle.c */
68 
69 #define CLICON_MAGIC 0x99aafabe
70 
71 #define handle(h) (assert(clicon_handle_check(h)==0),(struct backend_handle *)(h))
72 
73 /* Clicon_handle for backends.
74  * First part of this is header, same for clicon_handle and cli_handle.
75  * Access functions for common fields are found in clicon lib: clicon_options.[ch]
76  * This file should only contain access functions for the _specific_
77  * entries in the struct below.
78  */
79 /*! Backend specific handle added to header CLICON handle
80  * This file should only contain access functions for the _specific_
81  * entries in the struct below.
82  * @note The top part must be equivalent to struct clicon_handle in clixon_handle.c
83  * @see struct clicon_handle, struct cli_handle
84  */
85 struct backend_handle {
86     int                      bh_magic;     /* magic (HDR)*/
87     clicon_hash_t           *bh_copt;      /* clicon option list (HDR) */
88     clicon_hash_t           *bh_data;      /* internal clicon data (HDR) */
89     clicon_hash_t           *ch_db_elmnt;  /* xml datastore element cache data */
90     event_stream_t          *bh_stream;    /* notification streams, see clixon_stream.[ch] */
91 
92     /* ------ end of common handle ------ */
93     struct client_entry     *bh_ce_list;   /* The client list */
94     int                      bh_ce_nr;     /* Number of clients, just increment */
95 };
96 
97 /*! Creates and returns a clicon config handle for other CLICON API calls
98  */
99 clicon_handle
backend_handle_init(void)100 backend_handle_init(void)
101 {
102     return clicon_handle_init0(sizeof(struct backend_handle));
103 }
104 
105 /*! Deallocates a backend handle, including all client structs
106  * @Note: handle 'h' cannot be used in calls after this
107  * @see backend_client_rm
108  */
109 int
backend_handle_exit(clicon_handle h)110 backend_handle_exit(clicon_handle h)
111 {
112     struct client_entry   *ce;
113 
114     /* only delete client structs, not close sockets, etc, see backend_client_rm WHY NOT? */
115     while ((ce = backend_client_list(h)) != NULL){
116 	if (ce->ce_s){
117 	    close(ce->ce_s);
118 	    ce->ce_s = 0;
119 	}
120 	backend_client_delete(h, ce);
121     }
122     clicon_handle_exit(h); /* frees h and options (and streams) */
123     return 0;
124 }
125 
126 /*! Add new client, typically frontend such as cli, netconf, restconf
127  * @param[in]  h        Clicon handle
128  * @param[in]  addr     Address of client
129  * @retval     ce       Client entry
130  * @retval     NULL     Error
131  */
132 struct client_entry *
backend_client_add(clicon_handle h,struct sockaddr * addr)133 backend_client_add(clicon_handle    h,
134 		   struct sockaddr *addr)
135 {
136     struct backend_handle *bh = handle(h);
137     struct client_entry   *ce;
138 
139     if ((ce = (struct client_entry *)malloc(sizeof(*ce))) == NULL){
140 	clicon_err(OE_PLUGIN, errno, "malloc");
141 	return NULL;
142     }
143     memset(ce, 0, sizeof(*ce));
144     ce->ce_nr = bh->bh_ce_nr++; /* Session-id ? */
145     memcpy(&ce->ce_addr, addr, sizeof(*addr));
146     ce->ce_next = bh->bh_ce_list;
147     bh->bh_ce_list = ce;
148     return ce;
149 }
150 
151 /*! Return client list
152  * @param[in]  h        Clicon handle
153  * @retval     ce_list  Client entry list (all sessions)
154  */
155 struct client_entry *
backend_client_list(clicon_handle h)156 backend_client_list(clicon_handle h)
157 {
158     struct backend_handle *bh = handle(h);
159 
160     return bh->bh_ce_list;
161 }
162 
163 /*! Actually remove client from client list
164  * @param[in]  h   Clicon handle
165  * @param[in]  ce  Client handle
166  * @see backend_client_rm which is more high-level
167  */
168 int
backend_client_delete(clicon_handle h,struct client_entry * ce)169 backend_client_delete(clicon_handle        h,
170 		      struct client_entry *ce)
171 {
172     struct client_entry   *c;
173     struct client_entry  **ce_prev;
174     struct backend_handle *bh = handle(h);
175 
176     ce_prev = &bh->bh_ce_list;
177     for (c = *ce_prev; c; c = c->ce_next){
178 	if (c == ce){
179 	    *ce_prev = c->ce_next;
180 	    if (ce->ce_username)
181 		free(ce->ce_username);
182 	    free(ce);
183 	    break;
184 	}
185 	ce_prev = &c->ce_next;
186     }
187     return 0;
188 }
189 
190