]>
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 | ||
8b7874ce | 36 | #ifndef FD_SET |
37 | #define FD_ZERO(p) ((p)->fds_bits[0] = 0) | |
38 | #define FD_SET(n, p) ((p)->fds_bits[0] |= (1 << (n))) | |
39 | #define FD_ISSET(n, p) ((p)->fds_bits[0] & (1 << (n))) | |
40 | #endif /* FD_SET */ | |
41 | ||
6e6374cb | 42 | ureg_init() |
43 | { | |
44 | struct servent *sp; | |
14ef68d0 | 45 | char *host, **p, *s; |
6e6374cb | 46 | struct hostent *hp; |
47 | struct sockaddr_in sin; | |
028ff1c1 | 48 | extern char *getenv(), **hes_resolve(); |
6e6374cb | 49 | |
e0cddd92 | 50 | initialize_ureg_error_table(); |
a85aba2f | 51 | initialize_krb_error_table(); |
52 | initialize_sms_error_table(); | |
7f0e4572 | 53 | initialize_kadm_error_table(); |
a85aba2f | 54 | |
8edc0757 | 55 | seq_no = getpid(); |
56 | ||
028ff1c1 | 57 | host = NULL; |
58 | host = getenv("REGSERVER"); | |
59 | #ifdef HESIOD | |
60 | if (!host || (strlen(host) == 0)) { | |
61 | p = hes_resolve("registration", "sloc"); | |
62 | if (p) host = *p; | |
63 | } | |
64 | #endif HESIOD | |
65 | if (!host || (strlen(host) == 0)) { | |
8defc06b | 66 | host = strsave(MOIRA_SERVER); |
14ef68d0 | 67 | s = index(host, ':'); |
68 | if (s) *s = 0; | |
028ff1c1 | 69 | } |
70 | hp = gethostbyname(host); | |
6e6374cb | 71 | if (hp == NULL) return UNKNOWN_HOST; |
72 | ||
73 | sp = getservbyname("sms_ureg", "udp"); | |
74 | ||
75 | if (sp == NULL) return UNKNOWN_SERVICE; | |
76 | ||
77 | (void) close(reg_sock); | |
78 | reg_sock = socket(AF_INET, SOCK_DGRAM, 0); | |
79 | if (reg_sock < 0) return errno; | |
80 | ||
81 | bzero((char *)&sin, sizeof(sin)); | |
82 | sin.sin_port = sp->s_port; | |
83 | bcopy(hp->h_addr, (char *)&sin.sin_addr, sizeof(struct in_addr)); | |
84 | sin.sin_family = AF_INET; | |
85 | ||
86 | if (connect(reg_sock, &sin, sizeof(sin)) < 0) | |
87 | return errno; | |
88 | return 0; | |
89 | } | |
90 | ||
6e6374cb | 91 | int |
92 | verify_user(first, last, idnumber, hashidnumber, login) | |
93 | char *first, *last, *idnumber, *hashidnumber, *login; | |
94 | { | |
95 | char buf[1024]; | |
96 | int version = ntohl((u_long)1); | |
97 | int call = ntohl((u_long)UREG_VERIFY_USER); | |
c1b9c340 | 98 | des_cblock key; |
99 | des_key_schedule ks; | |
6e6374cb | 100 | register char *bp = buf; |
101 | register int len; | |
102 | char crypt_src[1024]; | |
103 | ||
104 | bcopy((char *)&version, bp, sizeof(int)); | |
105 | bp += sizeof(int); | |
106 | seq_no++; | |
107 | bcopy((char *)&seq_no, bp, sizeof(int)); | |
108 | ||
109 | bp += sizeof(int); | |
110 | ||
111 | bcopy((char *)&call, bp, sizeof(int)); | |
112 | ||
113 | bp += sizeof(int); | |
114 | ||
115 | (void) strcpy(bp, first); | |
116 | bp += strlen(bp)+1; | |
117 | ||
118 | (void) strcpy(bp, last); | |
119 | bp += strlen(bp)+1; | |
120 | ||
121 | len = strlen(idnumber) + 1; | |
122 | bcopy(idnumber, crypt_src, len); | |
123 | ||
124 | bcopy(hashidnumber, crypt_src+len, 13); | |
125 | ||
c1b9c340 | 126 | des_string_to_key(hashidnumber, key); |
127 | des_key_sched(key, ks); | |
e4b6711e | 128 | des_pcbc_encrypt(crypt_src, bp, len+13, ks, key, DES_ENCRYPT); |
6e6374cb | 129 | bp += len+14+8; |
130 | len = bp - buf; | |
131 | return do_call(buf, len, seq_no, login); | |
132 | } | |
133 | ||
5dee7862 | 134 | do_operation(first, last, idnumber, hashidnumber, data, opcode) |
135 | char *first, *last, *idnumber, *hashidnumber, *data; | |
136 | u_long opcode; | |
6e6374cb | 137 | { |
138 | char buf[1024]; | |
139 | int version = ntohl((u_long)1); | |
5dee7862 | 140 | int call = ntohl(opcode); |
c1b9c340 | 141 | des_cblock key; |
142 | des_key_schedule ks; | |
6e6374cb | 143 | register char *bp = buf; |
144 | register int len; | |
6e6374cb | 145 | |
146 | char crypt_src[1024]; | |
147 | char *cbp; | |
148 | ||
149 | bcopy((char *)&version, bp, sizeof(int)); | |
150 | bp += sizeof(int); | |
151 | seq_no++; | |
152 | bcopy((char *)&seq_no, bp, sizeof(int)); | |
153 | ||
154 | bp += sizeof(int); | |
155 | ||
156 | bcopy((char *)&call, bp, sizeof(int)); | |
157 | ||
158 | bp += sizeof(int); | |
159 | ||
160 | (void) strcpy(bp, first); | |
161 | bp += strlen(bp)+1; | |
162 | ||
163 | (void) strcpy(bp, last); | |
164 | bp += strlen(bp)+1; | |
165 | ||
166 | len = strlen(idnumber) + 1; | |
167 | cbp = crypt_src; | |
168 | ||
169 | bcopy(idnumber, crypt_src, len); | |
170 | cbp += len; | |
171 | ||
172 | bcopy(hashidnumber, cbp, 14); | |
173 | cbp += 14; | |
174 | ||
5dee7862 | 175 | len = strlen(data) + 1; |
176 | bcopy(data, cbp, len); | |
6e6374cb | 177 | cbp += len; |
178 | ||
179 | len = cbp - crypt_src; | |
c1b9c340 | 180 | des_string_to_key(hashidnumber, key); |
181 | des_key_sched(key, ks); | |
182 | des_pcbc_encrypt(crypt_src, bp, len, ks, key, 1); | |
6e6374cb | 183 | len = ((len + 7) >> 3) << 3; |
184 | bp += len; | |
185 | ||
186 | len = bp - buf; | |
187 | return do_call(buf, len, seq_no, 0); | |
188 | ||
189 | } | |
190 | ||
5dee7862 | 191 | grab_login(first, last, idnumber, hashidnumber, login) |
192 | char *first, *last, *idnumber, *hashidnumber, *login; | |
6e6374cb | 193 | { |
5dee7862 | 194 | return(do_operation(first, last, idnumber, hashidnumber, login, |
195 | UREG_RESERVE_LOGIN)); | |
196 | } | |
6e6374cb | 197 | |
5dee7862 | 198 | enroll_login(first, last, idnumber, hashidnumber, login) |
199 | char *first, *last, *idnumber, *hashidnumber, *login; | |
200 | { | |
201 | return(do_operation(first, last, idnumber, hashidnumber, login, | |
202 | UREG_SET_IDENT)); | |
203 | } | |
6e6374cb | 204 | |
5dee7862 | 205 | set_password(first, last, idnumber, hashidnumber, password) |
206 | char *first, *last, *idnumber, *hashidnumber, *password; | |
207 | { | |
208 | return(do_operation(first, last, idnumber, hashidnumber, password, | |
209 | UREG_SET_PASSWORD)); | |
210 | } | |
6e6374cb | 211 | |
5dee7862 | 212 | get_krb(first, last, idnumber, hashidnumber, password) |
213 | char *first, *last, *idnumber, *hashidnumber, *password; | |
214 | { | |
215 | return(do_operation(first, last, idnumber, hashidnumber, password, | |
216 | UREG_GET_KRB)); | |
6e6374cb | 217 | } |
218 | ||
219 | static do_call(buf, len, seq_no, login) | |
220 | char *buf; | |
221 | char *login; | |
222 | int seq_no; | |
223 | int len; | |
224 | { | |
225 | struct timeval timeout; | |
226 | char ibuf[1024]; | |
227 | fd_set set; | |
228 | ||
229 | int retry = 0; | |
230 | ||
231 | do { | |
232 | if (write(reg_sock, buf, len) != len) return errno; | |
233 | ||
234 | FD_ZERO(&set); | |
235 | FD_SET(reg_sock, &set); | |
aea2c8ee | 236 | timeout.tv_sec = 30; |
6e6374cb | 237 | timeout.tv_usec = 0; |
238 | do { | |
239 | int rtn; | |
240 | struct sockaddr_in sin; | |
241 | int addrlen = sizeof(sin); | |
242 | int vno; | |
243 | int sno; | |
244 | int stat; | |
245 | ||
246 | rtn = select(reg_sock+1, &set, (fd_set *)0, (fd_set *)0, &timeout); | |
247 | if (rtn == 0) | |
248 | break; | |
249 | else if (rtn < 0) return errno; | |
250 | ||
251 | len = recvfrom(reg_sock, ibuf, BUFSIZ, 0, &sin, &addrlen); | |
252 | if (len < 0) return errno; | |
253 | if (len < 12) return UREG_BROKEN_PACKET; | |
254 | bcopy(ibuf, (char *)&vno, sizeof(long)); | |
255 | vno = ntohl((u_long)vno); | |
256 | if (vno != 1) continue; | |
257 | bcopy(ibuf + 4, (char *)&sno, sizeof(long)); | |
258 | ||
259 | if (sno != seq_no) continue; | |
260 | ||
261 | bcopy(ibuf + 8, (char *)&stat, sizeof(long)); | |
262 | stat = ntohl((u_long)stat); | |
51ccb3ec | 263 | if (login && len > 12) { |
6e6374cb | 264 | bcopy(ibuf+12, login, len-12); |
265 | login[len-12] = '\0'; | |
51ccb3ec | 266 | } else if (login) |
267 | *login = '\0'; | |
6e6374cb | 268 | return stat; |
269 | } while (1); | |
0c8192b9 | 270 | } while (++retry < 10); |
6e6374cb | 271 | return ETIMEDOUT; |
272 | } |