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