@@ -30,108 +30,98 @@ void machine_credential_free_all(MachineCredential *creds, size_t n) {
3030
3131int machine_credential_set (MachineCredential * * credentials , size_t * n_credentials , const char * cred_string ) {
3232 _cleanup_free_ char * word = NULL , * data = NULL ;
33- MachineCredential * creds = * ASSERT_PTR (credentials );
3433 ssize_t l ;
35- size_t n_creds = * ASSERT_PTR (n_credentials );
3634 int r ;
3735 const char * p = ASSERT_PTR (cred_string );
3836
39- assert (creds || n_creds == 0 );
37+ assert (credentials && n_credentials );
38+ assert (* credentials || * n_credentials == 0 );
4039
4140 r = extract_first_word (& p , & word , ":" , EXTRACT_DONT_COALESCE_SEPARATORS );
42- if (r == - ENOMEM )
43- return r ;
4441 if (r < 0 )
4542 return log_error_errno (r , "Failed to parse --set-credential= parameter: %m" );
4643 if (r == 0 || !p )
4744 return log_error_errno (SYNTHETIC_ERRNO (EINVAL ), "Missing value for --set-credential=: %s" , cred_string );
4845
4946 if (!credential_name_valid (word ))
50- return log_error_errno (SYNTHETIC_ERRNO (EINVAL ), "MachineCredential name is not valid: %s" , word );
47+ return log_error_errno (SYNTHETIC_ERRNO (EINVAL ), "Credential name is not valid: %s" , word );
5148
52- FOREACH_ARRAY (cred , creds , n_creds )
49+ FOREACH_ARRAY (cred , * credentials , * n_credentials )
5350 if (streq (cred -> id , word ))
5451 return log_error_errno (SYNTHETIC_ERRNO (EEXIST ), "Duplicate credential '%s', refusing." , word );
5552
5653 l = cunescape (p , UNESCAPE_ACCEPT_NUL , & data );
5754 if (l < 0 )
5855 return log_error_errno (l , "Failed to unescape credential data: %s" , p );
5956
60- GREEDY_REALLOC (creds , n_creds + 1 );
61- if (!creds )
62- return - ENOMEM ;
57+ if (!GREEDY_REALLOC (* credentials , * n_credentials + 1 ))
58+ return log_oom ();
6359
64- creds [ n_creds ++ ] = (MachineCredential ) {
60+ ( * credentials )[( * n_credentials ) ++ ] = (MachineCredential ) {
6561 .id = TAKE_PTR (word ),
6662 .data = TAKE_PTR (data ),
6763 .size = l ,
6864 };
6965
70- * credentials = creds ;
71- * n_credentials = n_creds ;
72-
7366 return 0 ;
7467}
7568
7669int machine_credential_load (MachineCredential * * credentials , size_t * n_credentials , const char * cred_path ) {
7770 ReadFullFileFlags flags = READ_FULL_FILE_SECURE ;
7871 _cleanup_ (erase_and_freep ) char * data = NULL ;
7972 _cleanup_free_ char * word = NULL , * j = NULL ;
80- MachineCredential * creds = * ASSERT_PTR (credentials );
81- size_t size , n_creds = * ASSERT_PTR (n_credentials );
82- int r ;
8373 const char * p = ASSERT_PTR (cred_path );
74+ size_t size ;
75+ int r ;
8476
85- assert (creds || n_creds == 0 );
77+ assert (credentials && n_credentials );
78+ assert (* credentials || * n_credentials == 0 );
8679
8780 r = extract_first_word (& p , & word , ":" , EXTRACT_DONT_COALESCE_SEPARATORS );
88- if (r == - ENOMEM )
89- return - ENOMEM ;
9081 if (r < 0 )
9182 return log_error_errno (r , "Failed to parse --load-credential= parameter: %m" );
9283 if (r == 0 || !p )
9384 return log_error_errno (SYNTHETIC_ERRNO (EINVAL ), "Missing value for --load-credential=: %s" , cred_path );
9485
9586 if (!credential_name_valid (word ))
96- return log_error_errno (SYNTHETIC_ERRNO (EINVAL ), "MachineCredential name is not valid: %s" , word );
87+ return log_error_errno (SYNTHETIC_ERRNO (EINVAL ), "Credential name is not valid: %s" , word );
9788
98- FOREACH_ARRAY (cred , creds , n_creds )
89+ FOREACH_ARRAY (cred , * credentials , * n_credentials )
9990 if (streq (cred -> id , word ))
10091 return log_error_errno (SYNTHETIC_ERRNO (EEXIST ), "Duplicate credential '%s', refusing." , word );
10192
102- if (path_is_absolute (p ))
93+ if (is_path ( p ) && path_is_valid (p ))
10394 flags |= READ_FULL_FILE_CONNECT_SOCKET ;
104- else {
95+ else if ( credential_name_valid ( p )) {
10596 const char * e ;
10697
10798 r = get_credentials_dir (& e );
10899 if (r < 0 )
109- return log_error_errno (r , "MachineCredential not available (no credentials passed at all): %s" , word );
100+ return log_error_errno (r , "Credential not available (no credentials passed at all): %s" , word );
110101
111102 j = path_join (e , p );
112103 if (!j )
113- return - ENOMEM ;
114- }
104+ return log_oom ();
115105
116- r = read_full_file_full (AT_FDCWD , j ?: p , UINT64_MAX , SIZE_MAX ,
106+ p = j ;
107+ } else
108+ return log_error_errno (SYNTHETIC_ERRNO (EINVAL ), "Credential source appears to be neither a valid path nor a credential name: %s" , p );
109+
110+ r = read_full_file_full (AT_FDCWD , p , UINT64_MAX , SIZE_MAX ,
117111 flags ,
118112 NULL ,
119113 & data , & size );
120114 if (r < 0 )
121- return log_error_errno (r , "Failed to read credential '%s': %m" , j ?: p );
115+ return log_error_errno (r , "Failed to read credential '%s': %m" , p );
122116
123- GREEDY_REALLOC (creds , n_creds + 1 );
124- if (!creds )
125- return - ENOMEM ;
117+ if (!GREEDY_REALLOC (* credentials , * n_credentials + 1 ))
118+ return log_oom ();
126119
127- creds [ n_creds ++ ] = (MachineCredential ) {
120+ ( * credentials )[( * n_credentials ) ++ ] = (MachineCredential ) {
128121 .id = TAKE_PTR (word ),
129122 .data = TAKE_PTR (data ),
130123 .size = size ,
131124 };
132125
133- * credentials = creds ;
134- * n_credentials = n_creds ;
135-
136126 return 0 ;
137127}
0 commit comments