]>
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> | |
8defc06b | 24 | #include <moira.h> |
25 | #include <moira_site.h> | |
6e6374cb | 26 | #include "ureg_err.h" |
27 | #include "ureg_proto.h" | |
28 | #include <strings.h> | |
29 | ||
30 | static int reg_sock = -1; | |
8edc0757 | 31 | static int seq_no = 0; |
6e6374cb | 32 | extern errno; |
33 | #define UNKNOWN_HOST -1 | |
34 | #define UNKNOWN_SERVICE -2 | |
35 | ||
36 | ureg_init() | |
37 | { | |
38 | struct servent *sp; | |
14ef68d0 | 39 | char *host, **p, *s; |
6e6374cb | 40 | struct hostent *hp; |
41 | struct sockaddr_in sin; | |
028ff1c1 | 42 | extern char *getenv(), **hes_resolve(); |
6e6374cb | 43 | |
e0cddd92 | 44 | initialize_ureg_error_table(); |
6e6374cb | 45 | |
8edc0757 | 46 | seq_no = getpid(); |
47 | ||
028ff1c1 | 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)) { | |
8defc06b | 57 | host = strsave(MOIRA_SERVER); |
14ef68d0 | 58 | s = index(host, ':'); |
59 | if (s) *s = 0; | |
028ff1c1 | 60 | } |
61 | hp = gethostbyname(host); | |
6e6374cb | 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 | ||
6e6374cb | 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); | |
c1b9c340 | 89 | des_cblock key; |
90 | des_key_schedule ks; | |
6e6374cb | 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 | ||
c1b9c340 | 117 | des_string_to_key(hashidnumber, key); |
118 | des_key_sched(key, ks); | |
e4b6711e | 119 | des_pcbc_encrypt(crypt_src, bp, len+13, ks, key, DES_ENCRYPT); |
6e6374cb | 120 | bp += len+14+8; |
121 | len = bp - buf; | |
122 | return do_call(buf, len, seq_no, login); | |
123 | } | |
124 | ||
5dee7862 | 125 | do_operation(first, last, idnumber, hashidnumber, data, opcode) |
126 | char *first, *last, *idnumber, *hashidnumber, *data; | |
127 | u_long opcode; | |
6e6374cb | 128 | { |
129 | char buf[1024]; | |
130 | int version = ntohl((u_long)1); | |
5dee7862 | 131 | int call = ntohl(opcode); |
c1b9c340 | 132 | des_cblock key; |
133 | des_key_schedule ks; | |
6e6374cb | 134 | register char *bp = buf; |
135 | register int len; | |
6e6374cb | 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 | ||
5dee7862 | 166 | len = strlen(data) + 1; |
167 | bcopy(data, cbp, len); | |
6e6374cb | 168 | cbp += len; |
169 | ||
170 | len = cbp - crypt_src; | |
c1b9c340 | 171 | des_string_to_key(hashidnumber, key); |
172 | des_key_sched(key, ks); | |
173 | des_pcbc_encrypt(crypt_src, bp, len, ks, key, 1); | |
6e6374cb | 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 | ||
5dee7862 | 182 | grab_login(first, last, idnumber, hashidnumber, login) |
183 | char *first, *last, *idnumber, *hashidnumber, *login; | |
6e6374cb | 184 | { |
5dee7862 | 185 | return(do_operation(first, last, idnumber, hashidnumber, login, |
186 | UREG_RESERVE_LOGIN)); | |
187 | } | |
6e6374cb | 188 | |
5dee7862 | 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 | } | |
6e6374cb | 195 | |
5dee7862 | 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 | } | |
6e6374cb | 202 | |
5dee7862 | 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)); | |
6e6374cb | 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); | |
aea2c8ee | 227 | timeout.tv_sec = 30; |
6e6374cb | 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); | |
51ccb3ec | 254 | if (login && len > 12) { |
6e6374cb | 255 | bcopy(ibuf+12, login, len-12); |
256 | login[len-12] = '\0'; | |
51ccb3ec | 257 | } else if (login) |
258 | *login = '\0'; | |
6e6374cb | 259 | return stat; |
260 | } while (1); | |
0c8192b9 | 261 | } while (++retry < 10); |
6e6374cb | 262 | return ETIMEDOUT; |
263 | } |