1 /* client.c - connect to a server */
3 static char ident[] = "@(#)$Id: client.c,v 1.22 1995/12/06 22:15:26 jromine Exp shettich $";
6 #if defined(SYS5) && defined(AUX)
12 #if defined(BSD42) || defined(SOCKETS)
13 #include "../h/strings.h"
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
22 #include <arpa/inet.h>
33 static CREDENTIALS cred;
34 static MSG_DAT msg_data;
35 static KTEXT ticket = (KTEXT)NULL;
36 static Key_schedule schedule;
58 extern char *sys_errlist[];
63 int a_addrtype; /* assumes AF_INET for inet_netof () */
69 #define a_net un.un_net
70 #define a_addr un.un_addr
73 static struct addrent *ne, *nz;
74 static struct addrent nets[MAXNETS];
76 static struct addrent *he, *hz;
77 static struct addrent hosts[MAXHOSTS];
80 char krb_realm[REALM_SZ];
81 char *PrincipalHostname();
82 static char *kservice; /* "pop" if using kpop */
86 static int rcaux (struct servent *, struct hostent *, char *);
87 static int getport (int, char *);
88 static int inet (struct hostent *, int);
90 static int rcaux (), getport (), inet ();
94 #if defined(BIND) && !defined(h_addr)
95 #define h_addr h_addr_list[0]
98 #define inaddr_copy(hp,sin) \
99 bcopy ((hp) -> h_addr, (char *) &((sin) -> sin_addr), (hp) -> h_length)
102 struct hostent *gethostbystring ();
105 static int brkany ();
106 static char *getcpy (), **copyip (), **brkstring ();
110 int client (args, protocol, service, response)
113 *service, /* "pop" or "pop/kpop" */
118 char *arguments[MAXARGS];
119 register struct hostent *hp;
121 register struct netent *np;
123 register struct servent *sp;
127 if (cp = index (kservice = service, '/')) { /* "pop/kpop" */
128 *cp++ = '\0'; /* kservice = "pop" */
129 service = cp; /* service = "kpop" */
132 kservice = NULL; /* not using KERBEROS */
136 if ((sp = getservbyname (service, protocol)) == NULL) {
138 if ((sp = hes_getservbyname (service, protocol)) == NULL) {
139 (void) sprintf (response, "%s/%s: unknown service",
144 (void) sprintf (response, "%s/%s: unknown service", protocol, service);
150 if (args != NULL && *args != 0)
151 ap = copyip (brkstring (getcpy (args), " ", "\n"), ap);
153 if (servers != NULL && *servers != 0)
154 ap = copyip (brkstring (getcpy (servers), " ", "\n"), ap);
155 if (ap == arguments) {
156 *ap++ = getcpy ("localhost");
160 nz = (ne = nets) + sizeof nets / sizeof nets[0];
161 hz = (he = hosts) + sizeof hosts / sizeof hosts[0];
163 for (ap = arguments; *ap; ap++) {
166 if (np = getnetbyname (*ap + 1)) {
168 while (hp = gethostent ())
169 if (np -> n_addrtype == hp -> h_addrtype
170 && inet (hp, np -> n_net)) {
171 switch (sd = rcaux (sp, hp, response)) {
189 if (hp = gethostbystring (*ap)) {
190 switch (sd = rcaux (sp, hp, response)) {
204 (void) strcpy (response, "no servers available");
210 static int rcaux (sp, hp, response)
211 register struct servent *sp;
212 register struct hostent *hp;
213 register char *response;
217 register struct addrent *ap;
218 struct sockaddr_in in_socket;
219 register struct sockaddr_in *isock = &in_socket;
224 for (ap = nets; ap < ne; ap++)
225 if (ap -> a_addrtype == hp -> h_addrtype && inet (hp, ap -> a_net))
228 for (ap = hosts; ap < he; ap++)
229 if (ap -> a_addrtype == hp -> h_addrtype
230 && bcmp (ap -> a_addr, hp -> h_addr, hp -> h_length) == 0)
233 if ((sd = getport (hp -> h_addrtype, response)) == NOTOK)
236 bzero ((char *) isock, sizeof *isock);
237 isock -> sin_family = hp -> h_addrtype;
238 inaddr_copy (hp, isock);
239 isock -> sin_port = sp -> s_port;
241 if (connect (sd, (struct sockaddr *) isock, sizeof *isock) == NOTOK)
247 ne -> a_addrtype = hp -> h_addrtype;
248 bcopy (hp -> h_addr, (char *) &in, sizeof in);
249 ne -> a_net = inet_netof (in);
259 he -> a_addrtype = hp -> h_addrtype;
260 bcopy (hp -> h_addr, he -> a_addr, hp -> h_length);
267 if (kservice) { /* "pop" */
268 ticket = (KTEXT)malloc( sizeof(KTEXT_ST) );
269 rem = krb_sendauth(0L, sd, ticket, kservice, hp->h_name,
270 (char *) krb_realmofhost(hp->h_name),
271 (unsigned long)0, &msg_data, &cred, schedule,
272 (struct sockaddr_in *)NULL,
273 (struct sockaddr_in *)NULL,
275 if (rem != KSUCCESS) {
277 (void) strcpy(response, "Post office refused connection: ");
278 (void) strcat(response, krb_err_txt[rem]);
289 static int getport (addrtype, response)
291 register char *response;
295 struct sockaddr_in in_socket,
299 if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == NOTOK) {
300 (void) sprintf (response, "unable to create socket: %s",
301 errno > 0 && errno < sys_nerr ? sys_errlist[errno]
306 if (kservice) /* "pop" */
315 static int inet (hp, net)
316 register struct hostent *hp;
321 bcopy (hp -> h_addr, (char *) &in, sizeof in);
322 return (inet_netof (in) == net);
327 /* taken from ISODE's compat/internet.c */
333 struct in_addr inet_addr ();
337 static char *empty = NULL;
339 static char *addrs[2] = { NULL };
342 struct hostent *gethostbystring (s)
345 register struct hostent *h;
349 static struct in_addr iaddr;
351 static struct hostent hs;
353 iaddr = inet_addr (s);
355 if ((int)iaddr == NOTOK && strcmp (s, "255.255.255.255"))
357 if (iaddr.s_addr == NOTOK && strcmp (s, "255.255.255.255"))
359 return gethostbyname (s);
363 h -> h_aliases = ∅
364 h -> h_addrtype = AF_INET;
365 h -> h_length = sizeof (iaddr);
367 h -> h_addr_list = addrs;
368 bzero ((char *) addrs, sizeof addrs);
370 h -> h_addr = (char *) &iaddr;
377 /* static copies of three MH subroutines... (sigh) */
379 static char *broken[MAXARGS + 1];
382 static char **brkstring (strg, brksep, brkterm)
384 register char *brksep,
393 for (bi = 0; bi < MAXARGS; bi++) {
394 while (brkany (c = *sp, brksep))
396 if (!c || brkany (c, brkterm)) {
403 while ((c = *++sp) && !brkany (c, brksep) && !brkany (c, brkterm))
412 static brkany (chr, strg)
419 for (sp = strg; *sp; sp++)
426 static char **copyip (p, q)
438 static char *getcpy (str)
443 if ((cp = malloc ((unsigned) (strlen (str) + 1))) == NULL)
446 (void) strcpy (cp, str);
449 #endif /* BSD42 or SOCKETS */