3 * client.c -- connect to a server
11 #include <sys/socket.h>
12 #include <netinet/in.h>
15 #ifdef HAVE_ARPA_INET_H
16 # include <arpa/inet.h>
39 int a_addrtype; /* assumes AF_INET for inet_netof () */
46 #define a_net un.un_net
47 #define a_addr un.un_addr
49 static struct addrent *n1, *n2;
50 static struct addrent nets[MAXNETS];
51 static struct addrent *h1, *h2;
52 static struct addrent hosts[MAXHOSTS];
55 static CREDENTIALS cred;
56 static MSG_DAT msg_data;
57 static KTEXT ticket = (KTEXT) NULL;
58 static Key_schedule schedule;
59 static char *kservice; /* "pop" if using kpop */
60 char krb_realm[REALM_SZ];
61 char *PrincipalHostname();
65 # define h_addr h_addr_list[0]
68 #define inaddr_copy(hp,sin) \
69 memcpy(&((sin)->sin_addr), (hp)->h_addr, (hp)->h_length)
74 static int rcaux (struct servent *, struct hostent *, int, char *, int);
75 static int getport (int, int, char *, int);
76 static int inet (struct hostent *, int);
77 struct hostent *gethostbystring (char *s);
79 /* client's own static version of several nmh subroutines */
80 static char **client_brkstring (char *, char *, char *);
81 static int client_brkany (char, char *);
82 static char **client_copyip (char **, char **, int);
83 static char *client_getcpy (char *);
87 client (char *args, char *protocol, char *service, int rproto,
88 char *response, int len_response)
92 char *arguments[MAXARGS];
93 register struct hostent *hp;
94 register struct servent *sp;
95 #ifndef HAVE_GETHOSTBYNAME
96 register struct netent *np;
103 if (cp = strchr (service, '/')) { /* "pop/kpop" */
104 *cp++ = '\0'; /* kservice = "pop" */
105 service = cp; /* service = "kpop" */
107 kservice = NULL; /* not using KERBEROS */
112 if ((sp = getservbyname (service, protocol)) == NULL) {
114 if ((sp = hes_getservbyname (service, protocol)) == NULL) {
115 snprintf (response, len_response, "%s/%s: unknown service", protocol, service);
119 snprintf (response, len_response, "%s/%s: unknown service", protocol, service);
125 if (args != NULL && *args != 0) {
126 ap = client_copyip (client_brkstring (client_getcpy (args), " ", "\n"),
129 if (servers != NULL && *servers != 0)
130 ap = client_copyip (client_brkstring (client_getcpy (servers), " ", "\n"),
133 if (ap == arguments) {
134 *ap++ = client_getcpy ("localhost");
139 n2 = nets + sizeof(nets) / sizeof(nets[0]);
142 h2 = hosts + sizeof(hosts) / sizeof(hosts[0]);
144 for (ap = arguments; *ap; ap++) {
147 * the assumption here is that if the system doesn't have a
148 * gethostbyname() function, it must not use DNS. So we need to look
149 * into the /etc/hosts using gethostent(). There probablly aren't any
150 * systems still like this, but you never know. On every system I have
151 * access to, this section is ignored.
153 #ifndef HAVE_GETHOSTBYNAME
154 if ((np = getnetbyname (*ap + 1))) {
155 #ifdef HAVE_SETHOSTENT
157 #endif /* HAVE_SETHOSTENT */
158 while ((hp = gethostent()))
159 if (np->n_addrtype == hp->h_addrtype
160 && inet (hp, np->n_net)) {
161 switch (sd = rcaux (sp, hp, rproto, response, len_response)) {
175 #endif /* don't HAVE_GETHOSTBYNAME */
179 if ((hp = gethostbystring (*ap))) {
180 switch (sd = rcaux (sp, hp, rproto, response, len_response)) {
194 strncpy (response, "no servers available", len_response);
200 rcaux (struct servent *sp, struct hostent *hp, int rproto,
201 char *response, int len_response)
205 register struct addrent *ap;
206 struct sockaddr_in in_socket;
207 register struct sockaddr_in *isock = &in_socket;
214 for (ap = nets; ap < n1; ap++)
215 if (ap->a_addrtype == hp->h_addrtype && inet (hp, ap->a_net))
218 for (ap = hosts; ap < h1; ap++)
219 if (ap->a_addrtype == hp->h_addrtype
220 && memcmp(ap->a_addr, hp->h_addr, hp->h_length) == 0)
223 if ((sd = getport (rproto, hp->h_addrtype, response, len_response)) == NOTOK)
226 memset (isock, 0, sizeof(*isock));
227 isock->sin_family = hp->h_addrtype;
228 inaddr_copy (hp, isock);
229 isock->sin_port = sp->s_port;
231 if (connect (sd, (struct sockaddr *) isock, sizeof(*isock)) == NOTOK)
237 n1->a_addrtype = hp->h_addrtype;
238 memcpy(&in, hp->h_addr, sizeof(in));
239 n1->a_net = inet_netof (in);
249 h1->a_addrtype = hp->h_addrtype;
250 memcpy(h1->a_addr, hp->h_addr, hp->h_length);
257 if (kservice) { /* "pop" */
260 if (( hp2 = gethostbyaddr( hp->h_addr, hp->h_length, hp->h_addrtype ))
264 if ((instance = strdup (hp2->h_name)) == NULL) {
266 strncpy (response, "Out of memory.", len_response);
269 ticket = (KTEXT) malloc (sizeof(KTEXT_ST));
270 rem = krb_sendauth (0L, sd, ticket, kservice, instance,
271 (char *) krb_realmofhost (instance),
272 (unsigned long) 0, &msg_data, &cred, schedule,
273 (struct sockaddr_in *) NULL,
274 (struct sockaddr_in *) NULL,
277 if (rem != KSUCCESS) {
279 strncpy (response, "Post office refused connection: ", len_response);
280 strncat (response, krb_err_txt[rem], len_response - strlen(response));
291 getport (int rproto, int addrtype, char *response, int len_response)
294 struct sockaddr_in in_socket, *isock;
297 if (rproto && addrtype != AF_INET) {
298 snprintf (response, len_response, "reserved ports not supported for af=%d", addrtype);
303 if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == NOTOK) {
306 if ((s = strerror (errno)))
307 snprintf (response, len_response, "unable to create socket: %s", s);
309 snprintf (response, len_response, "unable to create socket: unknown error");
313 if (kservice) /* "pop" */
319 memset(isock, 0, sizeof(*isock));
320 isock->sin_family = addrtype;
321 for (port = IPPORT_RESERVED - 1;;) {
322 isock->sin_port = htons ((unsigned short) port);
323 if (bind (sd, (struct sockaddr *) isock, sizeof(*isock)) != NOTOK)
331 if (--port <= IPPORT_RESERVED / 2) {
332 strncpy (response, "ports available", len_response);
338 if ((s = strerror (errno)))
339 snprintf (response, len_response, "unable to bind socket: %s", s);
341 snprintf (response, len_response, "unable to bind socket: unknown error");
349 inet (struct hostent *hp, int net)
353 memcpy(&in, hp->h_addr, sizeof(in));
354 return (inet_netof (in) == net);
359 * taken from ISODE's compat/internet.c
362 static char *empty = NULL;
365 static char *addrs[2] = { NULL };
369 gethostbystring (char *s)
371 register struct hostent *h;
372 static struct hostent hs;
374 static struct in_addr iaddr;
376 static unsigned long iaddr;
379 iaddr = inet_addr (s);
381 if (iaddr.s_addr == NOTOK && strcmp (s, "255.255.255.255"))
383 if (((int) iaddr == NOTOK) && strcmp (s, "255.255.255.255"))
385 return gethostbyname (s);
389 h->h_aliases = ∅
390 h->h_addrtype = AF_INET;
391 h->h_length = sizeof(iaddr);
393 h->h_addr_list = addrs;
394 memset(addrs, 0, sizeof(addrs));
396 h->h_addr = (char *) &iaddr;
403 * static copies of three nmh subroutines
406 static char *broken[MAXARGS + 1];
409 client_brkstring (char *strg, char *brksep, char *brkterm)
412 register char c, *sp;
416 for (bi = 0; bi < MAXARGS; bi++) {
417 while (client_brkany (c = *sp, brksep))
419 if (!c || client_brkany (c, brkterm)) {
426 while ((c = *++sp) && !client_brkany (c, brksep) && !client_brkany (c, brkterm))
436 * returns 1 if chr in strg, 0 otherwise
439 client_brkany (char chr, char *strg)
444 for (sp = strg; *sp; sp++)
452 * copy a string array and return pointer to end
455 client_copyip (char **p, char **q, int len_q)
457 while (*p && --len_q > 0)
467 client_getcpy (char *str)
472 len = strlen(str) + 1;
473 if (!(cp = malloc(len)))
476 memcpy (cp, str, len);