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