1 /*
2  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 /*
6  * Copyright 2008 by the Massachusetts Institute of Technology.
7  * All Rights Reserved.
8  *
9  * Export of this software from the United States of America may
10  *   require a specific license from the United States Government.
11  *   It is the responsibility of any person or organization contemplating
12  *   export to obtain such a license before exporting.
13  *
14  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
15  * distribute this software and its documentation for any purpose and
16  * without fee is hereby granted, provided that the above copyright
17  * notice appear in all copies and that both that copyright notice and
18  * this permission notice appear in supporting documentation, and that
19  * the name of M.I.T. not be used in advertising or publicity pertaining
20  * to distribution of the software without specific, written prior
21  * permission.  Furthermore if you modify this software you must label
22  * your software as modified software and not distribute it in such a
23  * fashion that it might be confused with the original M.I.T. software.
24  * M.I.T. makes no representations about the suitability of
25  * this software for any purpose.  It is provided "as is" without express
26  * or implied warranty.
27  *
28  */
29 
30 #include "gssapiP_generic.h"
31 #include "mglueP.h"
32 #include <stdio.h>
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #include <string.h>
37 #include <errno.h>
38 
generic_gss_create_empty_buffer_set(OM_uint32 * minor_status,gss_buffer_set_t * buffer_set)39 OM_uint32 generic_gss_create_empty_buffer_set
40 	   (OM_uint32 * minor_status,
41 	    gss_buffer_set_t *buffer_set)
42 {
43     gss_buffer_set_t set;
44 
45     set = (gss_buffer_set_desc *) malloc(sizeof(*set));
46     if (set == GSS_C_NO_BUFFER_SET) {
47 	*minor_status = ENOMEM;
48 	return GSS_S_FAILURE;
49     }
50 
51     set->count = 0;
52     set->elements = NULL;
53 
54     *buffer_set = set;
55 
56     *minor_status = 0;
57     return GSS_S_COMPLETE;
58 }
59 
generic_gss_add_buffer_set_member(OM_uint32 * minor_status,const gss_buffer_t member_buffer,gss_buffer_set_t * buffer_set)60 OM_uint32 generic_gss_add_buffer_set_member
61 	   (OM_uint32 * minor_status,
62 	    const gss_buffer_t member_buffer,
63 	    gss_buffer_set_t *buffer_set)
64 {
65     gss_buffer_set_t set;
66     gss_buffer_t p;
67     OM_uint32 ret;
68 
69     if (*buffer_set == GSS_C_NO_BUFFER_SET) {
70 	ret = generic_gss_create_empty_buffer_set(minor_status,
71 						  buffer_set);
72 	if (ret) {
73 	    return ret;
74 	}
75     }
76 
77     set = *buffer_set;
78     set->elements = (gss_buffer_desc *)realloc(set->elements,
79 					       (set->count + 1) *
80 						sizeof(gss_buffer_desc));
81     if (set->elements == NULL) {
82 	free(set);  /* SUNW17PACresync - MIT17 bug */
83 	*minor_status = ENOMEM;
84 	return GSS_S_FAILURE;
85     }
86 
87     p = &set->elements[set->count];
88 
89     p->value = malloc(member_buffer->length);
90     if (p->value == NULL) {
91 	free(set->elements); /* SUNW17PACresync - MIT17 bug */
92 	free(set); /* SUNW17PACresync - MIT17 bug */
93 	*minor_status = ENOMEM;
94 	return GSS_S_FAILURE;
95     }
96     (void) memcpy(p->value, member_buffer->value, member_buffer->length);
97     p->length = member_buffer->length;
98 
99     set->count++;
100 
101     *minor_status = 0;
102     return GSS_S_COMPLETE;
103 }
104 
generic_gss_release_buffer_set(OM_uint32 * minor_status,gss_buffer_set_t * buffer_set)105 OM_uint32 generic_gss_release_buffer_set
106 	   (OM_uint32 * minor_status,
107 	    gss_buffer_set_t *buffer_set)
108 {
109     int i;
110     OM_uint32 minor;
111 
112     *minor_status = 0;
113 
114     if (*buffer_set == GSS_C_NO_BUFFER_SET) {
115 	return GSS_S_COMPLETE;
116     }
117 
118     for (i = 0; i < (*buffer_set)->count; i++) {
119       (void) generic_gss_release_buffer(&minor, &((*buffer_set)->elements[i]));
120     }
121 
122     if ((*buffer_set)->elements != NULL) {
123 	free((*buffer_set)->elements);
124 	(*buffer_set)->elements = NULL;
125     }
126 
127     (*buffer_set)->count = 0;
128 
129     free(*buffer_set);
130     *buffer_set = GSS_C_NO_BUFFER_SET;
131 
132     return GSS_S_COMPLETE;
133 }
134 
135