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