2828#pragma clang diagnostic ignored "-Wdeprecated-declarations"
2929#endif
3030
31- static gss_result* gss_success_result (int ret);
32- static gss_result* gss_error_result (OM_uint32 err_maj, OM_uint32 err_min);
33- static gss_result* gss_error_result_with_message (const char * message);
34- static gss_result* gss_error_result_with_message_and_code (const char * mesage, int code);
31+ static gss_result gss_success_result (int ret);
32+ static gss_result gss_error_result (OM_uint32 err_maj, OM_uint32 err_min);
33+ static gss_result gss_error_result_with_message (const char * message);
34+ static gss_result gss_error_result_with_message_and_code (const char * mesage, int code);
3535
3636gss_client_state* gss_client_state_new () {
3737 gss_client_state* state = (gss_client_state*)malloc (sizeof (gss_client_state));
@@ -53,11 +53,11 @@ gss_server_state* gss_server_state_new() {
5353 return state;
5454}
5555
56- gss_result* server_principal_details (const char * service, const char * hostname) {
56+ gss_result server_principal_details (const char * service, const char * hostname) {
5757 char match[1024 ];
5858 size_t match_len = 0 ;
59- char * details = NULL ;
60- gss_result* result = NULL ;
59+ std::string details;
60+ gss_result result {} ;
6161
6262 int code;
6363 krb5_context kcontext;
@@ -96,8 +96,7 @@ gss_result* server_principal_details(const char* service, const char* hostname)
9696 }
9797
9898 if (strncmp (pname, match, match_len) == 0 ) {
99- details = (char *)malloc (strlen (pname) + 1 );
100- strcpy (details, pname);
99+ details = pname;
101100 krb5_free_unparsed_name (kcontext, pname);
102101 krb5_free_keytab_entry_contents (kcontext, &entry);
103102 break ;
@@ -107,11 +106,11 @@ gss_result* server_principal_details(const char* service, const char* hostname)
107106 krb5_free_keytab_entry_contents (kcontext, &entry);
108107 }
109108
110- if (details == NULL ) {
109+ if (details. empty () ) {
111110 result = gss_error_result_with_message_and_code (" Principal not found in keytab" , -1 );
112111 } else {
113112 result = gss_success_result (AUTH_GSS_COMPLETE);
114- result-> data = details;
113+ result. data = std::move ( details) ;
115114 }
116115end:
117116 if (cursor)
@@ -123,7 +122,7 @@ gss_result* server_principal_details(const char* service, const char* hostname)
123122 return result;
124123}
125124
126- gss_result* authenticate_gss_client_init (const char * service,
125+ gss_result authenticate_gss_client_init (const char * service,
127126 const char * principal,
128127 long int gss_flags,
129128 gss_server_state* delegatestate,
@@ -133,7 +132,7 @@ gss_result* authenticate_gss_client_init(const char* service,
133132 OM_uint32 min_stat;
134133 gss_buffer_desc name_token = GSS_C_EMPTY_BUFFER;
135134 gss_buffer_desc principal_token = GSS_C_EMPTY_BUFFER;
136- gss_result* ret = NULL ;
135+ gss_result ret {} ;
137136
138137 state->server_name = GSS_C_NO_NAME;
139138 state->mech_oid = mech_oid;
@@ -218,14 +217,14 @@ int authenticate_gss_client_clean(gss_client_state* state) {
218217 return ret;
219218}
220219
221- gss_result* authenticate_gss_client_step (gss_client_state* state,
220+ gss_result authenticate_gss_client_step (gss_client_state* state,
222221 const char * challenge,
223222 struct gss_channel_bindings_struct * channel_bindings) {
224223 OM_uint32 maj_stat;
225224 OM_uint32 min_stat;
226225 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
227226 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
228- gss_result* ret = NULL ;
227+ gss_result ret {} ;
229228 int temp_ret = AUTH_GSS_CONTINUE;
230229
231230 // Always clear out the old response
@@ -330,13 +329,13 @@ gss_result* authenticate_gss_client_step(gss_client_state* state,
330329 return ret;
331330}
332331
333- gss_result* authenticate_gss_client_unwrap (gss_client_state* state, const char * challenge) {
332+ gss_result authenticate_gss_client_unwrap (gss_client_state* state, const char * challenge) {
334333 OM_uint32 maj_stat;
335334 OM_uint32 min_stat;
336335 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
337336 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
338337 int conf = 0 ;
339- gss_result* ret = NULL ;
338+ gss_result ret {} ;
340339
341340 // Always clear out the old response
342341 if (state->response != NULL ) {
@@ -378,7 +377,7 @@ gss_result* authenticate_gss_client_unwrap(gss_client_state* state, const char*
378377 return ret;
379378}
380379
381- gss_result* authenticate_gss_client_wrap (gss_client_state* state,
380+ gss_result authenticate_gss_client_wrap (gss_client_state* state,
382381 const char * challenge,
383382 const char * user,
384383 int protect) {
@@ -388,7 +387,7 @@ gss_result* authenticate_gss_client_wrap(gss_client_state* state,
388387 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
389388 char buf[4096 ];
390389 unsigned long buf_size;
391- gss_result* ret = NULL ;
390+ gss_result ret {} ;
392391
393392 // Always clear out the old response
394393 if (state->response != NULL ) {
@@ -422,9 +421,10 @@ gss_result* authenticate_gss_client_wrap(gss_client_state* state,
422421 memcpy (buf, &buf_size, 4 );
423422 buf[0 ] = GSS_AUTH_P_NONE;
424423 // server decides if principal can log in as user
425- strncpy (buf + 4 , user, sizeof (buf) - 4 );
424+ strncpy (buf + 4 , user, sizeof (buf) - 5 );
425+ buf[sizeof (buf) - 1 ] = ' \0 ' ;
426426 input_token.value = buf;
427- input_token.length = 4 + strlen (user );
427+ input_token.length = 4 + strlen (buf + 4 );
428428 }
429429
430430 // Do GSSAPI wrap
@@ -455,12 +455,12 @@ gss_result* authenticate_gss_client_wrap(gss_client_state* state,
455455 return ret;
456456}
457457
458- gss_result* authenticate_gss_server_init (const char * service, gss_server_state* state) {
458+ gss_result authenticate_gss_server_init (const char * service, gss_server_state* state) {
459459 OM_uint32 maj_stat;
460460 OM_uint32 min_stat;
461461 size_t service_len;
462462 gss_buffer_desc name_token = GSS_C_EMPTY_BUFFER;
463- gss_result* ret = NULL ;
463+ gss_result ret {} ;
464464
465465 state->context = GSS_C_NO_CONTEXT;
466466 state->server_name = GSS_C_NO_NAME;
@@ -537,14 +537,14 @@ int authenticate_gss_server_clean(gss_server_state* state) {
537537 return ret;
538538}
539539
540- gss_result* authenticate_gss_server_step (gss_server_state* state, const char * challenge) {
540+ gss_result authenticate_gss_server_step (gss_server_state* state, const char * challenge) {
541541 OM_uint32 maj_stat;
542542 OM_uint32 min_stat;
543543 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
544544 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
545545 gss_name_t target_name = GSS_C_NO_NAME;
546546 // int ret = AUTH_GSS_CONTINUE;
547- gss_result* ret = NULL ;
547+ gss_result ret {} ;
548548
549549 // Always clear out the old response
550550 if (state->response != NULL ) {
@@ -632,15 +632,15 @@ gss_result* authenticate_gss_server_step(gss_server_state* state, const char* ch
632632 return ret;
633633}
634634
635- gss_result* authenticate_user_krb5pwd (const char * user,
635+ gss_result authenticate_user_krb5pwd (const char * user,
636636 const char * pswd,
637637 const char * service,
638638 const char * default_realm) {
639639 krb5_context kcontext = NULL ;
640640 krb5_error_code code;
641641 krb5_principal client = NULL ;
642642 krb5_principal server = NULL ;
643- gss_result* result = NULL ;
643+ gss_result result {} ;
644644 int ret = 0 ;
645645 char * name = NULL ;
646646 char * p = NULL ;
@@ -727,58 +727,53 @@ gss_result* authenticate_user_krb5pwd(const char* user,
727727 return result;
728728}
729729
730- static gss_result* gss_success_result (int ret) {
731- gss_result* result = (gss_result*)malloc (sizeof (gss_result));
732- result->code = ret;
733- result->message = NULL ;
734- return result;
730+ static gss_result gss_success_result (int ret) {
731+ return { ret, " " , " " };
735732}
736733
737- static gss_result* gss_error_result (OM_uint32 err_maj, OM_uint32 err_min) {
734+ static gss_result gss_error_result (OM_uint32 err_maj, OM_uint32 err_min) {
738735 OM_uint32 maj_stat, min_stat;
739736 OM_uint32 msg_ctx = 0 ;
740737 gss_buffer_desc status_string;
741- char buf_maj[512 ];
742- char buf_min[512 ];
743- gss_result* result;
738+ std::string buf_maj;
739+ std::string buf_min;
744740
745741 do {
746742 maj_stat = gss_display_status (
747743 &min_stat, err_maj, GSS_C_GSS_CODE, GSS_C_NO_OID, &msg_ctx, &status_string);
748744 if (GSS_ERROR (maj_stat))
749745 break ;
750- strncpy ( buf_maj, (char *)status_string.value , sizeof (buf_maj)) ;
746+ buf_maj = (char *)status_string.value ;
751747 gss_release_buffer (&min_stat, &status_string);
752748
753749 maj_stat = gss_display_status (
754750 &min_stat, err_min, GSS_C_MECH_CODE, GSS_C_NULL_OID, &msg_ctx, &status_string);
755751 if (!GSS_ERROR (maj_stat)) {
756- strncpy (buf_min, (char *)status_string.value , sizeof (buf_min));
757- gss_release_buffer (&min_stat, &status_string);
752+ buf_min = (char *)status_string.value ;
758753 }
759754 } while (!GSS_ERROR (maj_stat) && msg_ctx != 0 );
760755
761- result = (gss_result*)malloc (sizeof (gss_result));
762- result->code = AUTH_GSS_ERROR;
763- result->message = (char *)malloc (sizeof (char ) * 1024 + 2 );
764- sprintf (result->message , " %s: %s" , buf_maj, buf_min);
765-
766- return result;
756+ return {
757+ AUTH_GSS_ERROR,
758+ buf_maj + " : " + buf_min,
759+ " "
760+ };
767761}
768762
769- static gss_result* gss_error_result_with_message (const char * message) {
770- gss_result* result = (gss_result*)malloc (sizeof (gss_result));
771- result->code = AUTH_GSS_ERROR;
772- result->message = strdup (message);
773- return result;
763+ static gss_result gss_error_result_with_message (const char * message) {
764+ return {
765+ AUTH_GSS_ERROR,
766+ message,
767+ " "
768+ };
774769}
775770
776- static gss_result* gss_error_result_with_message_and_code (const char * message, int code) {
777- gss_result* result = (gss_result*) malloc ( sizeof (gss_result));
778- result-> code = AUTH_GSS_ERROR;
779- result-> message = ( char *) malloc ( strlen (message ) + 20 );
780- sprintf (result-> message , " %s (%d) " , message, code);
781- return result ;
771+ static gss_result gss_error_result_with_message_and_code (const char * message, int code) {
772+ return {
773+ AUTH_GSS_ERROR,
774+ std::string (message) + " ( " + std::to_string (code ) + " ) " ,
775+ " "
776+ } ;
782777}
783778
784779#if defined(__clang__)
0 commit comments