]> andersk Git - moira.git/blob - clients/userreg/reg_stubs.c
8c0ea1741cd7f5f14c9f824e7e686dca97838346
[moira.git] / clients / userreg / reg_stubs.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *  (c) Copyright 1988 by the Massachusetts Institute of Technology.
7  *  For copying and distribution information, please see the file
8  *  <mit-copyright.h>.
9  */
10
11 #ifndef lint
12 static char *rcsid_reg_stubs_c = "$Header$";
13 #endif lint
14
15 #include <mit-copyright.h>
16 #include <stdio.h>
17 #include <sys/types.h>
18 #include <sys/time.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <netdb.h>
22 #include <des.h>
23 #include <errno.h>
24 #include <sms.h>
25 #include <sms_app.h>
26 #include "ureg_err.h"
27 #include "ureg_proto.h"
28 #include <strings.h>
29
30 static int reg_sock = -1;
31 static int seq_no = 0;
32 extern errno;
33 #define UNKNOWN_HOST -1
34 #define UNKNOWN_SERVICE -2
35
36 ureg_init()
37 {
38     struct servent *sp;
39     char *host, **p, *s;
40     struct hostent *hp;
41     struct sockaddr_in sin;
42     extern char *getenv(), **hes_resolve();
43     
44     initialize_ureg_error_table();
45     
46     seq_no = getpid();
47
48     host = NULL;
49     host = getenv("REGSERVER");
50 #ifdef HESIOD
51     if (!host || (strlen(host) == 0)) {
52         p = hes_resolve("registration", "sloc");
53         if (p) host = *p;
54     }
55 #endif HESIOD
56     if (!host || (strlen(host) == 0)) {
57         host = strsave(SMS_SERVER);
58         s = index(host, ':');
59         if (s) *s = 0;
60     }
61     hp = gethostbyname(host);
62     if (hp == NULL) return UNKNOWN_HOST;
63
64     sp = getservbyname("sms_ureg", "udp");
65
66     if (sp == NULL) return UNKNOWN_SERVICE;
67     
68     (void) close(reg_sock);
69     reg_sock = socket(AF_INET, SOCK_DGRAM, 0);
70     if (reg_sock < 0) return errno;
71
72     bzero((char *)&sin, sizeof(sin));
73     sin.sin_port = sp->s_port;
74     bcopy(hp->h_addr, (char *)&sin.sin_addr, sizeof(struct in_addr));
75     sin.sin_family = AF_INET;
76
77     if (connect(reg_sock, &sin, sizeof(sin)) < 0)
78         return errno;
79     return 0;
80 }
81
82 int
83 verify_user(first, last, idnumber, hashidnumber, login)
84     char *first, *last, *idnumber, *hashidnumber, *login;
85 {
86     char buf[1024];
87     int version = ntohl((u_long)1);
88     int call = ntohl((u_long)UREG_VERIFY_USER);
89     des_cblock key;
90     des_key_schedule ks;
91     register char *bp = buf;
92     register int len;
93     char crypt_src[1024];
94     
95     bcopy((char *)&version, bp, sizeof(int));
96     bp += sizeof(int);
97     seq_no++;
98     bcopy((char *)&seq_no, bp, sizeof(int));
99
100     bp += sizeof(int);
101
102     bcopy((char *)&call, bp, sizeof(int));
103
104     bp += sizeof(int);
105
106     (void) strcpy(bp, first);
107     bp += strlen(bp)+1;
108
109     (void) strcpy(bp, last);
110     bp += strlen(bp)+1;
111
112     len = strlen(idnumber) + 1;
113     bcopy(idnumber, crypt_src, len);
114
115     bcopy(hashidnumber, crypt_src+len, 13);
116
117     des_string_to_key(hashidnumber, key);
118     des_key_sched(key, ks);
119     des_pcbc_encrypt(crypt_src, bp, len+13, ks, key, DES_ENCRYPT);
120     bp += len+14+8;
121     len = bp - buf;
122     return do_call(buf, len, seq_no, login);
123 }
124
125 do_operation(first, last, idnumber, hashidnumber, data, opcode)
126     char *first, *last, *idnumber, *hashidnumber, *data;
127     u_long opcode;
128 {
129     char buf[1024];
130     int version = ntohl((u_long)1);
131     int call = ntohl(opcode);
132     des_cblock key;
133     des_key_schedule ks;
134     register char *bp = buf;
135     register int len;
136     
137     char crypt_src[1024];
138     char *cbp;
139     
140     bcopy((char *)&version, bp, sizeof(int));
141     bp += sizeof(int);
142     seq_no++;
143     bcopy((char *)&seq_no, bp, sizeof(int));
144
145     bp += sizeof(int);
146
147     bcopy((char *)&call, bp, sizeof(int));
148
149     bp += sizeof(int);
150
151     (void) strcpy(bp, first);
152     bp += strlen(bp)+1;
153
154     (void) strcpy(bp, last);
155     bp += strlen(bp)+1;
156
157     len = strlen(idnumber) + 1;
158     cbp = crypt_src;
159     
160     bcopy(idnumber, crypt_src, len);
161     cbp += len;
162     
163     bcopy(hashidnumber, cbp, 14);
164     cbp += 14;
165     
166     len = strlen(data) + 1;
167     bcopy(data, cbp, len);
168     cbp += len;
169
170     len = cbp - crypt_src;
171     des_string_to_key(hashidnumber, key);
172     des_key_sched(key, ks);
173     des_pcbc_encrypt(crypt_src, bp, len, ks, key, 1);
174     len = ((len + 7) >> 3) << 3;
175     bp += len;
176     
177     len = bp - buf;
178     return do_call(buf, len, seq_no, 0);
179
180 }
181
182 grab_login(first, last, idnumber, hashidnumber, login)
183     char *first, *last, *idnumber, *hashidnumber, *login;
184 {
185     return(do_operation(first, last, idnumber, hashidnumber, login,
186                         UREG_RESERVE_LOGIN));
187 }
188
189 enroll_login(first, last, idnumber, hashidnumber, login)
190     char *first, *last, *idnumber, *hashidnumber, *login;
191 {
192     return(do_operation(first, last, idnumber, hashidnumber, login,
193                         UREG_SET_IDENT));
194 }
195
196 set_password(first, last, idnumber, hashidnumber, password)
197     char *first, *last, *idnumber, *hashidnumber, *password;
198 {
199     return(do_operation(first, last, idnumber, hashidnumber, password,
200                         UREG_SET_PASSWORD));
201 }
202
203 get_krb(first, last, idnumber, hashidnumber, password)
204     char *first, *last, *idnumber, *hashidnumber, *password;
205 {
206     return(do_operation(first, last, idnumber, hashidnumber, password,
207                         UREG_GET_KRB));
208 }
209
210 static do_call(buf, len, seq_no, login)
211     char *buf;
212     char *login;
213     int seq_no;
214     int len;
215 {
216     struct timeval timeout;
217     char ibuf[1024];
218     fd_set set;
219     
220     int retry = 0;
221
222     do {
223         if (write(reg_sock, buf, len) != len) return errno;
224
225         FD_ZERO(&set);
226         FD_SET(reg_sock, &set);
227         timeout.tv_sec = 30;
228         timeout.tv_usec = 0;
229         do {
230             int rtn;
231             struct sockaddr_in sin;
232             int addrlen = sizeof(sin);
233             int vno;
234             int sno;
235             int stat;
236             
237             rtn = select(reg_sock+1, &set, (fd_set *)0, (fd_set *)0, &timeout);
238             if (rtn == 0)
239                 break;
240             else if (rtn < 0) return errno;
241         
242             len = recvfrom(reg_sock, ibuf, BUFSIZ, 0, &sin, &addrlen);
243             if (len < 0) return errno;
244             if (len < 12) return UREG_BROKEN_PACKET;
245             bcopy(ibuf, (char *)&vno, sizeof(long));
246             vno = ntohl((u_long)vno);
247             if (vno != 1) continue;
248             bcopy(ibuf + 4, (char *)&sno, sizeof(long));
249
250             if (sno != seq_no) continue;
251
252             bcopy(ibuf + 8, (char *)&stat, sizeof(long));
253             stat = ntohl((u_long)stat);
254             if (login && len > 12) {
255                 bcopy(ibuf+12, login, len-12);
256                 login[len-12] = '\0';
257             } else if (login)
258                 *login = '\0';
259             return stat;
260         } while (1);
261     } while (++retry < 10);
262     return ETIMEDOUT;
263 }    
This page took 0.207785 seconds and 3 git commands to generate.