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