@@ -51,89 +51,122 @@ static char *filerd(const char *fn, size_t len)
5151 return buf ;
5252}
5353
54- int ietf_keystore_change ( sr_session_ctx_t * session , struct lyd_node * config , struct lyd_node * diff , sr_event_t event , struct confd * confd )
54+ static int gen_hostkey ( const char * name , struct lyd_node * change )
5555{
56- struct lyd_node * changes , * change ;
57- sr_val_t * list = NULL ;
56+ const char * private_key , * public_key ;
5857 int rc = SR_ERR_OK ;
58+
59+ private_key = lydx_get_cattr (change , "cleartext-private-key" );
60+ public_key = lydx_get_cattr (change , "public-key" );
61+
62+ if (mkdir (SSH_HOSTKEYS_NEXT , 0600 ) && (errno != EEXIST )) {
63+ ERRNO ("Failed creating %s" , SSH_HOSTKEYS_NEXT );
64+ rc = SR_ERR_INTERNAL ;
65+ }
66+
67+ if (systemf ("/usr/libexec/infix/mksshkey %s %s %s %s" , name , SSH_HOSTKEYS_NEXT , public_key , private_key ))
68+ rc = SR_ERR_INTERNAL ;
69+
70+ return rc ;
71+ }
72+
73+ static int keystore_update (sr_session_ctx_t * session , struct lyd_node * config , struct lyd_node * diff )
74+ {
75+ const char * xpath = "/ietf-keystore:keystore/asymmetric-keys/asymmetric-key" ;
76+ sr_val_t * list = NULL ;
5977 size_t count = 0 ;
60- if (diff && !lydx_find_xpathf (diff , XPATH_KEYSTORE_ ))
61- return SR_ERR_OK ;
78+ int rc ;
6279
63- switch (event ) {
64- case SR_EV_UPDATE :
65- rc = sr_get_items (session , "/ietf-keystore:keystore/asymmetric-keys/asymmetric-key" , 0 , 0 , & list , & count );
66- if (rc != SR_ERR_OK ) {
67- ERROR ("Cannot find any asymmetric keys in configuration" );
68- return 0 ;
80+ rc = sr_get_items (session , xpath , 0 , 0 , & list , & count );
81+ if (rc != SR_ERR_OK ) {
82+ ERROR ("Cannot find any asymmetric keys in configuration" );
83+ return 0 ;
84+ }
85+
86+ for (size_t i = 0 ; i < count ; i ++ ) {
87+ char * name = srx_get_str (session , "%s/name" , list [i ].xpath );
88+ char * public_key_format , * private_key_format ;
89+ char * pub_key = NULL , * priv_key = NULL ;
90+ sr_val_t * entry = & list [i ];
91+
92+ if (srx_isset (session , "%s/cleartext-private-key" , entry -> xpath ) ||
93+ srx_isset (session , "%s/public-key" , entry -> xpath ))
94+ continue ;
95+
96+ public_key_format = srx_get_str (session , "%s/public-key-format" , entry -> xpath );
97+ if (!public_key_format )
98+ continue ;
99+
100+ private_key_format = srx_get_str (session , "%s/private-key-format" , entry -> xpath );
101+ if (!private_key_format ) {
102+ free (public_key_format );
103+ continue ;
69104 }
70105
106+ if (strcmp (private_key_format , "infix-crypto-types:rsa-private-key-format" ) ||
107+ strcmp (public_key_format , "infix-crypto-types:ssh-public-key-format" ))
108+ continue ;
71109
72- for (size_t i = 0 ; i < count ; ++ i ) {
73- sr_val_t * entry = & list [i ];
74-
75- if (!srx_isset (session , "%s/cleartext-private-key" , entry -> xpath ) && !srx_isset (session , "%s/public-key" , entry -> xpath )) {
76- char * private_key_format , * public_key_format ;
77-
78- public_key_format = srx_get_str (session , "%s/public-key-format" , entry -> xpath );
79- if (!public_key_format )
80- continue ;
81- private_key_format = srx_get_str (session , "%s/private-key-format" , entry -> xpath );
82- if (!private_key_format ) {
83- free (public_key_format );
84- continue ;
85- }
86-
87- if (!strcmp (private_key_format , "infix-crypto-types:rsa-private-key-format" ) &&
88- !strcmp (public_key_format , "infix-crypto-types:ssh-public-key-format" )) {
89- char * pub_key = NULL , * priv_key = NULL , * name ;
90-
91- name = srx_get_str (session , "%s/name" , entry -> xpath );
92- NOTE ("SSH key (%s) does not exist, generating..." , name );
93- if (systemf ("/usr/libexec/infix/mkkeys %s %s" , SSH_PRIVATE_KEY , SSH_PUBLIC_KEY )) {
94- ERROR ("Failed to generate SSH keys for %s" , name );
95- goto next ;
96- }
97-
98- priv_key = filerd (SSH_PRIVATE_KEY , filesz (SSH_PRIVATE_KEY ));
99- if (!priv_key )
100- goto next ;
101-
102- pub_key = filerd (SSH_PUBLIC_KEY , filesz (SSH_PUBLIC_KEY ));
103- if (!pub_key )
104- goto next ;
105-
106- rc = srx_set_str (session , priv_key , 0 , "%s/cleartext-private-key" , entry -> xpath );
107- if (rc ) {
108- ERROR ("Failed setting private key for %s... rc: %d" , name , rc );
109- goto next ;
110- }
111- rc = srx_set_str (session , pub_key , 0 , "%s/public-key" , entry -> xpath );
112- if (rc != SR_ERR_OK ) {
113- ERROR ("Failed setting public key for %s... rc: %d" , name , rc );
114- goto next ;
115- }
116- next :
117- if (erase (SSH_PRIVATE_KEY ))
118- ERRNO ("Failed removing SSH server private key" );
119- if (erase (SSH_PUBLIC_KEY ))
120- ERRNO ("Failed removing SSH server public key" );
121-
122- if (priv_key )
123- free (priv_key );
124-
125- if (pub_key )
126- free (pub_key );
127-
128- free (name );
129- }
130- free (public_key_format );
131- free (private_key_format );
132- }
110+ NOTE ("SSH key (%s) does not exist, generating..." , name );
111+ if (systemf ("/usr/libexec/infix/mkkeys %s %s" , SSH_PRIVATE_KEY , SSH_PUBLIC_KEY )) {
112+ ERROR ("Failed generating SSH keys for %s" , name );
113+ goto next ;
133114 }
134- if (list )
135- sr_free_values (list , count );
136115
116+ priv_key = filerd (SSH_PRIVATE_KEY , filesz (SSH_PRIVATE_KEY ));
117+ if (!priv_key )
118+ goto next ;
119+
120+ pub_key = filerd (SSH_PUBLIC_KEY , filesz (SSH_PUBLIC_KEY ));
121+ if (!pub_key )
122+ goto next ;
123+
124+ rc = srx_set_str (session , priv_key , 0 , "%s/cleartext-private-key" , entry -> xpath );
125+ if (rc ) {
126+ ERROR ("Failed setting private key for %s... rc: %d" , name , rc );
127+ goto next ;
128+ }
129+
130+ rc = srx_set_str (session , pub_key , 0 , "%s/public-key" , entry -> xpath );
131+ if (rc != SR_ERR_OK ) {
132+ ERROR ("Failed setting public key for %s... rc: %d" , name , rc );
133+ goto next ;
134+ }
135+ next :
136+ if (erase (SSH_PRIVATE_KEY ))
137+ ERRNO ("Failed removing SSH server private key" );
138+ if (erase (SSH_PUBLIC_KEY ))
139+ ERRNO ("Failed removing SSH server public key" );
140+
141+ if (priv_key )
142+ free (priv_key );
143+
144+ if (pub_key )
145+ free (pub_key );
146+
147+ free (name );
148+ free (public_key_format );
149+ free (private_key_format );
150+ }
151+
152+ if (list )
153+ sr_free_values (list , count );
154+
155+ return 0 ;
156+ }
157+
158+ int ietf_keystore_change (sr_session_ctx_t * session , struct lyd_node * config , struct lyd_node * diff ,
159+ sr_event_t event , struct confd * confd )
160+ {
161+ struct lyd_node * changes , * change ;
162+ int rc = SR_ERR_OK ;
163+
164+ if (diff && !lydx_find_xpathf (diff , XPATH_KEYSTORE_ ))
165+ return SR_ERR_OK ;
166+
167+ switch (event ) {
168+ case SR_EV_UPDATE :
169+ rc = keystore_update (session , config , diff );
137170 break ;
138171 case SR_EV_CHANGE :
139172 case SR_EV_ENABLED :
@@ -142,49 +175,35 @@ int ietf_keystore_change(sr_session_ctx_t *session, struct lyd_node *config, str
142175 rmrf (SSH_HOSTKEYS_NEXT );
143176 return SR_ERR_OK ;
144177 case SR_EV_DONE :
145- if (fexist (SSH_HOSTKEYS_NEXT )) {
146- if (rmrf (SSH_HOSTKEYS )) {
178+ if (fexist (SSH_HOSTKEYS_NEXT )) {
179+ if (rmrf (SSH_HOSTKEYS ))
147180 ERRNO ("Failed to remove old SSH hostkeys: %d" , errno );
148- }
149-
150181 if (rename (SSH_HOSTKEYS_NEXT , SSH_HOSTKEYS ))
151182 ERRNO ("Failed switching to new %s" , SSH_HOSTKEYS );
152183 }
153184 return SR_ERR_OK ;
154-
155185 default :
156186 return SR_ERR_OK ;
157187 }
158188
159189 changes = lydx_get_descendant (config , "keystore" , "asymmetric-keys" , "asymmetric-key" , NULL );
160-
161190 LYX_LIST_FOR_EACH (changes , change , "asymmetric-key" ) {
162- const char * name , * private_key_type , * public_key_type ;
163- const char * private_key , * public_key ;
191+ const char * name = lydx_get_cattr ( change , "name" ) ;
192+ const char * type ;
164193
165- name = lydx_get_cattr (change , "name" );
166- private_key_type = lydx_get_cattr (change , "private-key-format" );
167- public_key_type = lydx_get_cattr (change , "public-key-format" );
168-
169- if (strcmp (private_key_type , "infix-crypto-types:rsa-private-key-format" )) {
170- INFO ("Private key %s is not of SSH type" , name );
194+ type = lydx_get_cattr (change , "private-key-format" );
195+ if (strcmp (type , "infix-crypto-types:rsa-private-key-format" )) {
196+ INFO ("Private key %s is not of SSH type (%s)" , name , type );
171197 continue ;
172198 }
173199
174- if (strcmp (public_key_type , "infix-crypto-types:ssh-public-key-format" )) {
175- INFO ("Public key %s is not of SSH type" , name );
200+ type = lydx_get_cattr (change , "public-key-format" );
201+ if (strcmp (type , "infix-crypto-types:ssh-public-key-format" )) {
202+ INFO ("Public key %s is not of SSH type (%s)" , name , type );
176203 continue ;
177204 }
178- private_key = lydx_get_cattr (change , "cleartext-private-key" );
179- public_key = lydx_get_cattr (change , "public-key" );
180-
181- if (mkdir (SSH_HOSTKEYS_NEXT , 0600 ) && (errno != EEXIST )) {
182- ERRNO ("Failed creating %s" , SSH_HOSTKEYS_NEXT );
183- rc = SR_ERR_INTERNAL ;
184- }
185205
186- if (systemf ("/usr/libexec/infix/mksshkey %s %s %s %s" , name , SSH_HOSTKEYS_NEXT , public_key , private_key ))
187- rc = SR_ERR_INTERNAL ;
206+ gen_hostkey (name , change );
188207 }
189208
190209 return rc ;
0 commit comments