source: trunk/third/ksrvutil/kadm_stream.c @ 11760

Revision 11760, 7.8 KB checked in by ghudson, 26 years ago (diff)
Some CNS libkadm sources to build the CNS kpasswd against. In some files, KRB_INT32 needed to be redefined to KRB4_32 for krb5 build system.
Line 
1/*
2 * Copyright 1988 by the Massachusetts Institute of Technology.
3 *
4 * For copying and distribution information, please see the file
5 * <mit-copyright.h>.
6 *
7 * Stream conversion functions for Kerberos administration server
8 */
9
10#include <mit-copyright.h>
11/*
12  kadm_stream.c
13  this holds the stream support routines for the kerberos administration server
14
15    vals_to_stream: converts a vals struct to a stream for transmission
16       internals build_field_header, vts_[string, char, long, short]
17    stream_to_vals: converts a stream to a vals struct
18       internals check_field_header, stv_[string, char, long, short]
19    error: prints out a kadm error message, returns
20    fatal: prints out a kadm fatal error message, exits
21*/
22
23#include <string.h>
24
25#include "kadm.h"
26
27#ifndef KRB_INT32
28#define KRB_INT32 KRB4_32
29#endif
30
31#define min(a,b) (((a) < (b)) ? (a) : (b))
32
33/*
34vals_to_stream
35  recieves    : kadm_vals *, u_char *
36  returns     : a realloced and filled in u_char *
37     
38this function creates a byte-stream representation of the kadm_vals structure
39*/
40int vals_to_stream(dt_in, dt_out)
41Kadm_vals *dt_in;
42u_char **dt_out;
43{
44  int vsloop, stsize;           /* loop counter, stream size */
45
46  stsize = build_field_header(dt_in->fields, dt_out);
47  for (vsloop=31; vsloop>=0; vsloop--)
48    if (IS_FIELD(vsloop,dt_in->fields)) {
49      switch (vsloop) {
50      case KADM_NAME:
51          stsize+=vts_string(dt_in->name, dt_out, stsize);
52          break;
53      case KADM_INST:
54          stsize+=vts_string(dt_in->instance, dt_out, stsize);
55          break;
56      case KADM_EXPDATE:
57          stsize+=vts_long((unsigned KRB_INT32) dt_in->exp_date, dt_out, stsize);
58          break;
59      case KADM_ATTR:
60          stsize+=vts_short(dt_in->attributes, dt_out, stsize);
61          break;
62      case KADM_MAXLIFE:
63          stsize+=vts_char(dt_in->max_life, dt_out, stsize);
64          break;
65      case KADM_DESKEY:
66          stsize+=vts_long(dt_in->key_high, dt_out, stsize);
67          stsize+=vts_long(dt_in->key_low, dt_out, stsize);
68          break;
69      default:
70          break;
71      }
72}
73  return(stsize);
74
75
76int build_field_header(cont, st)
77u_char *cont;                   /* container for fields data */
78u_char **st;                    /* stream */
79{
80  extern char *malloc();
81
82  *st = (u_char *) malloc (4);
83  memcpy((char *) *st, (char *) cont, 4);
84  return 4;                     /* return pointer to current stream location */
85}
86
87int vts_string(dat, st, loc)
88char *dat;                      /* a string to put on the stream */
89u_char **st;                    /* base pointer to the stream */
90int loc;                        /* offset into the stream for current data */
91{
92  extern char *realloc();
93
94  *st = (u_char *) realloc ((char *)*st, (unsigned) (loc + strlen(dat) + 1));
95  memcpy((char *)(*st + loc), dat, strlen(dat)+1);
96  return strlen(dat)+1;
97}
98
99int vts_short(dat, st, loc)
100u_short dat;                    /* the attributes field */
101u_char **st;                    /* a base pointer to the stream */
102int loc;                        /* offset into the stream for current data */
103{
104  u_short temp;                 /* to hold the net order short */
105  extern char *realloc();
106
107  temp = htons(dat);            /* convert to network order */
108  *st = (u_char *) realloc ((char *)*st, (unsigned)(loc + sizeof(u_short)));
109  memcpy((char *)(*st + loc), (char *) &temp, sizeof(u_short));
110  return sizeof(u_short);
111}
112
113int vts_long(dat, st, loc)
114unsigned KRB_INT32 dat;         /* the attributes field */
115u_char **st;                    /* a base pointer to the stream */
116int loc;                        /* offset into the stream for current data */
117{
118  KRB_INT32 temp;                       /* to hold the net order short */
119  extern char *realloc();
120
121  temp = htonl(dat);            /* convert to network order */
122  *st = (u_char *) realloc ((char *)*st, (unsigned)(loc + sizeof(KRB_INT32)));
123  memcpy((char *)(*st + loc), (char *) &temp, sizeof(KRB_INT32));
124  return sizeof(KRB_INT32);
125}
126
127   
128int vts_char(dat, st, loc)
129u_char dat;                     /* the attributes field */
130u_char **st;                    /* a base pointer to the stream */
131int loc;                        /* offset into the stream for current data */
132{
133  extern char *realloc();
134
135  *st = (u_char *) realloc ((char *)*st, (unsigned)(loc + sizeof(u_char)));
136  (*st)[loc] = (u_char) dat;
137  return 1;
138}
139   
140/*
141stream_to_vals
142  recieves    : u_char *, kadm_vals *
143  returns     : a kadm_vals filled in according to u_char *
144     
145this decodes a byte stream represntation of a vals struct into kadm_vals
146*/
147int stream_to_vals(dt_in, dt_out, maxlen)
148u_char *dt_in;
149Kadm_vals *dt_out;
150int maxlen;                             /* max length to use */
151{
152  register int vsloop, stsize;          /* loop counter, stream size */
153  register int status;
154
155  memset((char *) dt_out, 0, sizeof(*dt_out));
156
157  stsize = check_field_header(dt_in, dt_out->fields, maxlen);
158  if (stsize < 0)
159      return(-1);
160  for (vsloop=31; vsloop>=0; vsloop--)
161    if (IS_FIELD(vsloop,dt_out->fields))
162      switch (vsloop) {
163      case KADM_NAME:
164          if ((status = stv_string(dt_in, dt_out->name, stsize,
165                                   sizeof(dt_out->name), maxlen)) < 0)
166              return(-1);
167          stsize += status;
168          break;
169      case KADM_INST:
170          if ((status = stv_string(dt_in, dt_out->instance, stsize,
171                                   sizeof(dt_out->instance), maxlen)) < 0)
172              return(-1);
173          stsize += status;
174          break;
175      case KADM_EXPDATE:
176          {
177            unsigned KRB_INT32 exp_date;
178
179            if ((status = stv_long(dt_in, &exp_date, stsize,
180                                   maxlen)) < 0)
181                return(-1);
182            dt_out->exp_date = exp_date;
183            stsize += status;
184          }
185          break;
186      case KADM_ATTR:
187          if ((status = stv_short(dt_in, &dt_out->attributes, stsize,
188                                  maxlen)) < 0)
189              return(-1);
190          stsize += status;
191          break;
192      case KADM_MAXLIFE:
193          if ((status = stv_char(dt_in, &dt_out->max_life, stsize,
194                                 maxlen)) < 0)
195              return(-1);
196          stsize += status;
197          break;
198      case KADM_DESKEY:
199          if ((status = stv_long(dt_in, &dt_out->key_high, stsize,
200                                    maxlen)) < 0)
201              return(-1);
202          stsize += status;
203          if ((status = stv_long(dt_in, &dt_out->key_low, stsize,
204                                    maxlen)) < 0)
205              return(-1);
206          stsize += status;
207          break;
208      default:
209          break;
210      }
211  return stsize;
212
213
214int check_field_header(st, cont, maxlen)
215u_char *st;                     /* stream */
216u_char *cont;                   /* container for fields data */
217int maxlen;
218{
219  if (4 > maxlen)
220      return(-1);
221  memcpy((char *) cont, (char *) st, 4);
222  return 4;                     /* return pointer to current stream location */
223}
224
225int stv_string(st, dat, loc, stlen, maxlen)
226register u_char *st;            /* base pointer to the stream */
227char *dat;                      /* a string to read from the stream */
228register int loc;               /* offset into the stream for current data */
229int stlen;                      /* max length of string to copy in */
230int maxlen;                     /* max length of input stream */
231{
232  int maxcount;                         /* max count of chars to copy */
233
234  maxcount = min(maxlen - loc, stlen);
235  if (maxcount <= 0)            /* No strings left in the input stream */
236      return -1;
237
238  (void) strncpy(dat, (char *)st + loc, maxcount);
239
240  if (dat[maxcount-1]) /* not null-term --> not enuf room */
241      return(-1);
242  return strlen(dat)+1;
243}
244
245int stv_short(st, dat, loc, maxlen)
246u_char *st;                     /* a base pointer to the stream */
247u_short *dat;                   /* the attributes field */
248int loc;                        /* offset into the stream for current data */
249int maxlen;
250{
251  u_short temp;                 /* to hold the net order short */
252
253  if (loc + sizeof(u_short) > maxlen)
254      return(-1);
255  memcpy((char *) &temp, (char *) st + loc, sizeof(u_short));
256  *dat = ntohs(temp);           /* convert to network order */
257  return sizeof(u_short);
258}
259
260int stv_long(st, dat, loc, maxlen)
261u_char *st;                     /* a base pointer to the stream */
262unsigned KRB_INT32 *dat;        /* the attributes field */
263int loc;                        /* offset into the stream for current data */
264int maxlen;                     /* maximum length of st */
265{
266  KRB_INT32 temp;               /* to hold the net order short */
267
268  if (loc + sizeof(KRB_INT32) > maxlen)
269      return(-1);
270  memcpy((char *) &temp, (char *) st + loc, sizeof(KRB_INT32));
271  *dat = ntohl(temp);           /* convert to network order */
272  return sizeof(KRB_INT32);
273}
274   
275int stv_char(st, dat, loc, maxlen)
276u_char *st;                     /* a base pointer to the stream */
277u_char *dat;                    /* the attributes field */
278int loc;                        /* offset into the stream for current data */
279int maxlen;
280{
281  if (loc + 1 > maxlen)
282      return(-1);
283  *dat = *(st + loc);
284  return 1;
285}
286
Note: See TracBrowser for help on using the repository browser.