]>
Commit | Line | Data |
---|---|---|
6e6374cb | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
7 | * | |
8 | * $Log$ | |
0c8192b9 | 9 | * Revision 1.2 1987-09-04 22:57:33 wesommer |
10 | * Rearranged timeouts, max retransmits. | |
6e6374cb | 11 | * |
0c8192b9 | 12 | * Revision 1.1 87/08/22 18:39:29 wesommer |
13 | * Initial revision | |
14 | * | |
6e6374cb | 15 | */ |
16 | ||
17 | #ifndef lint | |
18 | static char *rcsid_reg_stubs_c = "$Header$"; | |
19 | #endif lint | |
20 | #include <stdio.h> | |
21 | #include <sys/types.h> | |
22 | #include <sys/time.h> | |
23 | #include <sys/socket.h> | |
24 | #include <netinet/in.h> | |
25 | #include <netdb.h> | |
26 | #include <des.h> | |
27 | #include <errno.h> | |
28 | #include "ureg_err.h" | |
29 | #include "ureg_proto.h" | |
30 | #include <strings.h> | |
31 | ||
32 | static int reg_sock = -1; | |
33 | extern errno; | |
34 | #define UNKNOWN_HOST -1 | |
35 | #define UNKNOWN_SERVICE -2 | |
36 | ||
37 | ureg_init() | |
38 | { | |
39 | struct servent *sp; | |
40 | struct hostent *hp; | |
41 | struct sockaddr_in sin; | |
42 | ||
43 | init_ureg_err_tbl(); | |
44 | init_sms_err_tbl(); | |
45 | ||
46 | hp = gethostbyname("sms.mit.edu"); | |
47 | if (hp == NULL) return UNKNOWN_HOST; | |
48 | ||
49 | sp = getservbyname("sms_ureg", "udp"); | |
50 | ||
51 | if (sp == NULL) return UNKNOWN_SERVICE; | |
52 | ||
53 | (void) close(reg_sock); | |
54 | reg_sock = socket(AF_INET, SOCK_DGRAM, 0); | |
55 | if (reg_sock < 0) return errno; | |
56 | ||
57 | bzero((char *)&sin, sizeof(sin)); | |
58 | sin.sin_port = sp->s_port; | |
59 | bcopy(hp->h_addr, (char *)&sin.sin_addr, sizeof(struct in_addr)); | |
60 | sin.sin_family = AF_INET; | |
61 | ||
62 | if (connect(reg_sock, &sin, sizeof(sin)) < 0) | |
63 | return errno; | |
64 | return 0; | |
65 | } | |
66 | ||
67 | static int seq_no = 0; | |
68 | ||
69 | int | |
70 | verify_user(first, last, idnumber, hashidnumber, login) | |
71 | char *first, *last, *idnumber, *hashidnumber, *login; | |
72 | { | |
73 | char buf[1024]; | |
74 | int version = ntohl((u_long)1); | |
75 | int call = ntohl((u_long)UREG_VERIFY_USER); | |
76 | C_Block key; | |
77 | Key_schedule ks; | |
78 | register char *bp = buf; | |
79 | register int len; | |
80 | char crypt_src[1024]; | |
81 | ||
82 | bcopy((char *)&version, bp, sizeof(int)); | |
83 | bp += sizeof(int); | |
84 | seq_no++; | |
85 | bcopy((char *)&seq_no, bp, sizeof(int)); | |
86 | ||
87 | bp += sizeof(int); | |
88 | ||
89 | bcopy((char *)&call, bp, sizeof(int)); | |
90 | ||
91 | bp += sizeof(int); | |
92 | ||
93 | (void) strcpy(bp, first); | |
94 | bp += strlen(bp)+1; | |
95 | ||
96 | (void) strcpy(bp, last); | |
97 | bp += strlen(bp)+1; | |
98 | ||
99 | len = strlen(idnumber) + 1; | |
100 | bcopy(idnumber, crypt_src, len); | |
101 | ||
102 | bcopy(hashidnumber, crypt_src+len, 13); | |
103 | ||
104 | string_to_key(hashidnumber, key); | |
105 | key_sched(key, ks); | |
106 | pcbc_encrypt(crypt_src, bp, len+14, ks, key, 1); | |
107 | bp += len+14+8; | |
108 | len = bp - buf; | |
109 | return do_call(buf, len, seq_no, login); | |
110 | } | |
111 | ||
112 | grab_login(first, last, idnumber, hashidnumber, login) | |
113 | char *first, *last, *idnumber, *hashidnumber, *login; | |
114 | { | |
115 | char buf[1024]; | |
116 | int version = ntohl((u_long)1); | |
117 | int call = ntohl((u_long)UREG_RESERVE_LOGIN); | |
118 | C_Block key; | |
119 | Key_schedule ks; | |
120 | register char *bp = buf; | |
121 | register int len; | |
122 | int i; | |
123 | ||
124 | char crypt_src[1024]; | |
125 | char *cbp; | |
126 | ||
127 | bcopy((char *)&version, bp, sizeof(int)); | |
128 | bp += sizeof(int); | |
129 | seq_no++; | |
130 | bcopy((char *)&seq_no, bp, sizeof(int)); | |
131 | ||
132 | bp += sizeof(int); | |
133 | ||
134 | bcopy((char *)&call, bp, sizeof(int)); | |
135 | ||
136 | bp += sizeof(int); | |
137 | ||
138 | (void) strcpy(bp, first); | |
139 | bp += strlen(bp)+1; | |
140 | ||
141 | (void) strcpy(bp, last); | |
142 | bp += strlen(bp)+1; | |
143 | ||
144 | len = strlen(idnumber) + 1; | |
145 | cbp = crypt_src; | |
146 | ||
147 | bcopy(idnumber, crypt_src, len); | |
148 | cbp += len; | |
149 | ||
150 | bcopy(hashidnumber, cbp, 14); | |
151 | cbp += 14; | |
152 | ||
153 | len = strlen(login) + 1; | |
154 | bcopy(login, cbp, len); | |
155 | cbp += len; | |
156 | ||
157 | len = cbp - crypt_src; | |
158 | string_to_key(hashidnumber, key); | |
159 | key_sched(key, ks); | |
160 | pcbc_encrypt(crypt_src, bp, len, ks, key, 1); | |
161 | #ifdef notdef | |
162 | for (i = 0; i < len; i++) { | |
163 | printf("%02.2x ", (unsigned char)bp[i]); | |
164 | } | |
165 | printf("\n"); | |
166 | #endif notdef | |
167 | len = ((len + 7) >> 3) << 3; | |
168 | bp += len; | |
169 | ||
170 | len = bp - buf; | |
171 | return do_call(buf, len, seq_no, 0); | |
172 | ||
173 | } | |
174 | ||
175 | set_password(first, last, idnumber, hashidnumber, password) | |
176 | char *first, *last, *idnumber, *hashidnumber, *password; | |
177 | { | |
178 | char buf[1024]; | |
179 | int version = ntohl((u_long)1); | |
180 | int call = ntohl((u_long)UREG_SET_PASSWORD); | |
181 | C_Block key; | |
182 | Key_schedule ks; | |
183 | register char *bp = buf; | |
184 | register int len; | |
185 | int i; | |
186 | ||
187 | char crypt_src[1024]; | |
188 | char *cbp; | |
189 | ||
190 | bcopy((char *)&version, bp, sizeof(int)); | |
191 | bp += sizeof(int); | |
192 | seq_no++; | |
193 | bcopy((char *)&seq_no, bp, sizeof(int)); | |
194 | ||
195 | bp += sizeof(int); | |
196 | ||
197 | bcopy((char *)&call, bp, sizeof(int)); | |
198 | ||
199 | bp += sizeof(int); | |
200 | ||
201 | (void) strcpy(bp, first); | |
202 | bp += strlen(bp)+1; | |
203 | ||
204 | (void) strcpy(bp, last); | |
205 | bp += strlen(bp)+1; | |
206 | ||
207 | len = strlen(idnumber) + 1; | |
208 | cbp = crypt_src; | |
209 | ||
210 | bcopy(idnumber, crypt_src, len); | |
211 | cbp += len; | |
212 | ||
213 | bcopy(hashidnumber, cbp, 14); | |
214 | cbp += 14; | |
215 | ||
216 | len = strlen(password) + 1; | |
217 | bcopy(password, cbp, len); | |
218 | cbp += len; | |
219 | ||
220 | len = cbp - crypt_src; | |
221 | string_to_key(hashidnumber, key); | |
222 | key_sched(key, ks); | |
223 | pcbc_encrypt(crypt_src, bp, len, ks, key, 1); | |
224 | #ifdef notdef | |
225 | for (i = 0; i < len; i++) { | |
226 | printf("%02.2x ", (unsigned char)bp[i]); | |
227 | } | |
228 | printf("\n"); | |
229 | #endif notdef | |
230 | len = ((len + 7) >> 3) << 3; | |
231 | bp += len; | |
232 | ||
233 | len = bp - buf; | |
234 | return do_call(buf, len, seq_no, 0); | |
235 | ||
236 | } | |
237 | ||
238 | static do_call(buf, len, seq_no, login) | |
239 | char *buf; | |
240 | char *login; | |
241 | int seq_no; | |
242 | int len; | |
243 | { | |
244 | struct timeval timeout; | |
245 | char ibuf[1024]; | |
246 | fd_set set; | |
247 | ||
248 | int retry = 0; | |
249 | ||
250 | do { | |
251 | if (write(reg_sock, buf, len) != len) return errno; | |
252 | ||
253 | FD_ZERO(&set); | |
254 | FD_SET(reg_sock, &set); | |
0c8192b9 | 255 | timeout.tv_sec = 25; |
6e6374cb | 256 | timeout.tv_usec = 0; |
257 | do { | |
258 | int rtn; | |
259 | struct sockaddr_in sin; | |
260 | int addrlen = sizeof(sin); | |
261 | int vno; | |
262 | int sno; | |
263 | int stat; | |
264 | ||
265 | rtn = select(reg_sock+1, &set, (fd_set *)0, (fd_set *)0, &timeout); | |
266 | if (rtn == 0) | |
267 | break; | |
268 | else if (rtn < 0) return errno; | |
269 | ||
270 | len = recvfrom(reg_sock, ibuf, BUFSIZ, 0, &sin, &addrlen); | |
271 | if (len < 0) return errno; | |
272 | if (len < 12) return UREG_BROKEN_PACKET; | |
273 | bcopy(ibuf, (char *)&vno, sizeof(long)); | |
274 | vno = ntohl((u_long)vno); | |
275 | if (vno != 1) continue; | |
276 | bcopy(ibuf + 4, (char *)&sno, sizeof(long)); | |
277 | ||
278 | if (sno != seq_no) continue; | |
279 | ||
280 | bcopy(ibuf + 8, (char *)&stat, sizeof(long)); | |
281 | stat = ntohl((u_long)stat); | |
282 | if (login) { | |
283 | bcopy(ibuf+12, login, len-12); | |
284 | login[len-12] = '\0'; | |
285 | } | |
286 | return stat; | |
287 | } while (1); | |
0c8192b9 | 288 | } while (++retry < 10); |
6e6374cb | 289 | return ETIMEDOUT; |
290 | } | |
291 | ||
292 | /* | |
293 | * Local Variables: | |
294 | * mode: c | |
295 | * c-indent-level: 4 | |
296 | * c-continued-statement-offset: 4 | |
297 | * c-brace-offset: -4 | |
298 | * c-argdecl-indent: 4 | |
299 | * c-label-offset: -4 | |
300 | * End: | |
301 | */ |