Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / kadmin / kadm_server.c
1 /* 
2   Copyright (C) 1989 by the Massachusetts Institute of Technology
3
4    Export of this software from the United States of America is assumed
5    to require a specific license from the United States Government.
6    It is the responsibility of any person or organization contemplating
7    export to obtain such a license before exporting.
8
9 WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
10 distribute this software and its documentation for any purpose and
11 without fee is hereby granted, provided that the above copyright
12 notice appear in all copies and that both that copyright notice and
13 this permission notice appear in supporting documentation, and that
14 the name of M.I.T. not be used in advertising or publicity pertaining
15 to distribution of the software without specific, written prior
16 permission.  M.I.T. makes no representations about the suitability of
17 this software for any purpose.  It is provided "as is" without express
18 or implied warranty.
19
20   */
21
22 /*
23  * Kerberos administration server-side subroutines
24  */
25
26 #include "kadm_locl.h"
27
28 RCSID("$Id: kadm_server.c,v 1.9 1997/05/02 10:29:08 joda Exp $");
29
30 /* 
31 kadm_ser_cpw - the server side of the change_password routine
32   recieves    : KTEXT, {key}
33   returns     : CKSUM, RETCODE
34   acl         : caller can change only own password
35
36 Replaces the password (i.e. des key) of the caller with that specified in key.
37 Returns no actual data from the master server, since this is called by a user
38 */
39 int
40 kadm_ser_cpw(u_char *dat, int len, AUTH_DAT *ad, u_char **datout, int *outlen)
41 {
42     u_int32_t keylow, keyhigh;
43     des_cblock newkey;
44     int status;
45     int stvlen=0;
46     char *pw_msg;
47     char pword[MAX_KPW_LEN];
48     char *strings[4];
49
50     /* take key off the stream, and change the database */
51
52     if ((status = stv_long(dat, &keyhigh, 0, len)) < 0)
53         return(KADM_LENGTH_ERROR);
54     stvlen=status;
55     if ((status = stv_long(dat, &keylow, stvlen, len)) < 0)
56         return(KADM_LENGTH_ERROR);
57     stvlen+=status;
58
59     if((status = stv_string(dat, pword, stvlen, sizeof(pword), len))<0)
60       pword[0]=0;
61
62     keylow = ntohl(keylow);
63     keyhigh = ntohl(keyhigh);
64     memcpy(((char *)newkey) + 4, &keyhigh, 4);
65     memcpy(newkey, &keylow, 4);
66
67     strings[0] = ad->pname;
68     strings[1] = ad->pinst;
69     strings[2] = ad->prealm;
70     strings[3] = NULL;
71     status = kadm_pw_check(pword, &newkey, &pw_msg, strings);
72     
73     memset(pword, 0, sizeof(pword));
74     memset(dat, 0, len);
75
76     if(status != KADM_SUCCESS){
77       *datout=malloc(0);
78       *outlen=vts_string(pw_msg, datout, 0);
79       return status;
80     }
81     *datout=0;
82     *outlen=0;
83
84     return(kadm_change(ad->pname, ad->pinst, ad->prealm, newkey));
85 }
86
87
88 /*
89 kadm_ser_add - the server side of the add_entry routine
90   recieves    : KTEXT, {values}
91   returns     : CKSUM, RETCODE, {values}
92   acl         : su, sms (as alloc)
93
94 Adds and entry containing values to the database
95 returns the values of the entry, so if you leave certain fields blank you will
96    be able to determine the default values they are set to
97 */
98 int
99 kadm_ser_add(u_char *dat, int len, AUTH_DAT *ad, u_char **datout, int *outlen)
100 {
101   Kadm_vals values, retvals;
102   long status;
103
104   if ((status = stream_to_vals(dat, &values, len)) < 0)
105       return(KADM_LENGTH_ERROR);
106   if ((status = kadm_add_entry(ad->pname, ad->pinst, ad->prealm,
107                               &values, &retvals)) == KADM_DATA) {
108       *outlen = vals_to_stream(&retvals,datout);
109       return KADM_SUCCESS;
110   } else {
111       *outlen = 0;
112       return status;
113   }
114 }
115
116 /*
117 kadm_ser_mod - the server side of the mod_entry routine
118   recieves    : KTEXT, {values, values}
119   returns     : CKSUM, RETCODE, {values}
120   acl         : su, sms (as register or dealloc)
121
122 Modifies all entries corresponding to the first values so they match the
123    second values.
124 returns the values for the changed entries
125 */
126 int
127 kadm_ser_mod(u_char *dat, int len, AUTH_DAT *ad, u_char **datout, int *outlen)
128 {
129   Kadm_vals vals1, vals2, retvals;
130   int wh;
131   long status;
132
133   if ((wh = stream_to_vals(dat, &vals1, len)) < 0)
134       return KADM_LENGTH_ERROR;
135   if ((status = stream_to_vals(dat+wh,&vals2, len-wh)) < 0)
136       return KADM_LENGTH_ERROR;
137   if ((status = kadm_mod_entry(ad->pname, ad->pinst, ad->prealm, &vals1,
138                                &vals2, &retvals)) == KADM_DATA) {
139       *outlen = vals_to_stream(&retvals,datout);
140       return KADM_SUCCESS;
141   } else {
142       *outlen = 0;
143       return status;
144   }
145 }
146
147 int
148 kadm_ser_delete(u_char *dat, int len, AUTH_DAT *ad, 
149                 u_char **datout, int *outlen)
150 {
151     Kadm_vals values;
152     int wh;
153     int status;
154     
155     if((wh = stream_to_vals(dat, &values, len)) < 0)
156         return KADM_LENGTH_ERROR;
157     if(wh != len)
158         return KADM_LENGTH_ERROR;
159     status = kadm_delete_entry(ad->pname, ad->pinst, ad->prealm, 
160                                &values);
161     *outlen = 0;
162     return status;
163 }
164
165 /*
166 kadm_ser_get
167   recieves   : KTEXT, {values, flags}
168   returns    : CKSUM, RETCODE, {count, values, values, values}
169   acl        : su
170
171 gets the fields requested by flags from all entries matching values
172 returns this data for each matching recipient, after a count of how many such
173   matches there were
174 */
175 int
176 kadm_ser_get(u_char *dat, int len, AUTH_DAT *ad, u_char **datout, int *outlen)
177 {
178   Kadm_vals values, retvals;
179   u_char fl[FLDSZ];
180   int loop,wh;
181   long status;
182
183   if ((wh = stream_to_vals(dat, &values, len)) < 0)
184       return KADM_LENGTH_ERROR;
185   if (wh + FLDSZ > len)
186       return KADM_LENGTH_ERROR;
187   for (loop=FLDSZ-1; loop>=0; loop--)
188     fl[loop] = dat[wh++];
189   if ((status = kadm_get_entry(ad->pname, ad->pinst, ad->prealm,
190                               &values, fl, &retvals)) == KADM_DATA) {
191       *outlen = vals_to_stream(&retvals,datout);
192       return KADM_SUCCESS;
193   } else {
194       *outlen = 0;
195       return status;
196   }
197 }
198