]>
diplodocus.org Git - nmh/blob - zotnet/mts/client.c
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
;
213 for (ap
= nets
; ap
< n1
; ap
++)
214 if (ap
->a_addrtype
== hp
->h_addrtype
&& inet (hp
, ap
->a_net
))
217 for (ap
= hosts
; ap
< h1
; ap
++)
218 if (ap
->a_addrtype
== hp
->h_addrtype
219 && memcmp(ap
->a_addr
, hp
->h_addr
, hp
->h_length
) == 0)
222 if ((sd
= getport (rproto
, hp
->h_addrtype
, response
, len_response
)) == NOTOK
)
225 memset (isock
, 0, sizeof(*isock
));
226 isock
->sin_family
= hp
->h_addrtype
;
227 inaddr_copy (hp
, isock
);
228 isock
->sin_port
= sp
->s_port
;
230 if (connect (sd
, (struct sockaddr
*) isock
, sizeof(*isock
)) == NOTOK
)
236 n1
->a_addrtype
= hp
->h_addrtype
;
237 memcpy(&in
, hp
->h_addr
, sizeof(in
));
238 n1
->a_net
= inet_netof (in
);
248 h1
->a_addrtype
= hp
->h_addrtype
;
249 memcpy(h1
->a_addr
, hp
->h_addr
, hp
->h_length
);
256 if (kservice
) { /* "pop" */
259 if ((instance
= strdup (hp
->h_name
)) == NULL
) {
261 strncpy (response
, "Out of memory.", len_response
);
264 ticket
= (KTEXT
) malloc (sizeof(KTEXT_ST
));
265 rem
= krb_sendauth (0L, sd
, ticket
, kservice
, instance
,
266 (char *) krb_realmofhost (instance
),
267 (unsigned long) 0, &msg_data
, &cred
, schedule
,
268 (struct sockaddr_in
*) NULL
,
269 (struct sockaddr_in
*) NULL
,
272 if (rem
!= KSUCCESS
) {
274 strncpy (response
, "Post office refused connection: ", len_response
);
275 strncat (response
, krb_err_txt
[rem
], len_response
- strlen(response
));
286 getport (int rproto
, int addrtype
, char *response
, int len_response
)
289 struct sockaddr_in in_socket
, *isock
;
292 if (rproto
&& addrtype
!= AF_INET
) {
293 snprintf (response
, len_response
, "reserved ports not supported for af=%d", addrtype
);
298 if ((sd
= socket (AF_INET
, SOCK_STREAM
, 0)) == NOTOK
) {
301 if ((s
= strerror (errno
)))
302 snprintf (response
, len_response
, "unable to create socket: %s", s
);
304 snprintf (response
, len_response
, "unable to create socket: unknown error");
308 if (kservice
) /* "pop" */
314 memset(isock
, 0, sizeof(*isock
));
315 isock
->sin_family
= addrtype
;
316 for (port
= IPPORT_RESERVED
- 1;;) {
317 isock
->sin_port
= htons ((unsigned short) port
);
318 if (bind (sd
, (struct sockaddr
*) isock
, sizeof(*isock
)) != NOTOK
)
326 if (--port
<= IPPORT_RESERVED
/ 2) {
327 strncpy (response
, "ports available", len_response
);
333 if ((s
= strerror (errno
)))
334 snprintf (response
, len_response
, "unable to bind socket: %s", s
);
336 snprintf (response
, len_response
, "unable to bind socket: unknown error");
344 inet (struct hostent
*hp
, int net
)
348 memcpy(&in
, hp
->h_addr
, sizeof(in
));
349 return (inet_netof (in
) == net
);
354 * taken from ISODE's compat/internet.c
357 static char *empty
= NULL
;
360 static char *addrs
[2] = { NULL
};
364 gethostbystring (char *s
)
366 register struct hostent
*h
;
367 static struct hostent hs
;
369 static struct in_addr iaddr
;
371 static unsigned long iaddr
;
374 iaddr
= inet_addr (s
);
376 if (iaddr
.s_addr
== NOTOK
&& strcmp (s
, "255.255.255.255"))
378 if (((int) iaddr
== NOTOK
) && strcmp (s
, "255.255.255.255"))
380 return gethostbyname (s
);
384 h
->h_aliases
= &empty
;
385 h
->h_addrtype
= AF_INET
;
386 h
->h_length
= sizeof(iaddr
);
388 h
->h_addr_list
= addrs
;
389 memset(addrs
, 0, sizeof(addrs
));
391 h
->h_addr
= (char *) &iaddr
;
398 * static copies of three nmh subroutines
401 static char *broken
[MAXARGS
+ 1];
404 client_brkstring (char *strg
, char *brksep
, char *brkterm
)
407 register char c
, *sp
;
411 for (bi
= 0; bi
< MAXARGS
; bi
++) {
412 while (client_brkany (c
= *sp
, brksep
))
414 if (!c
|| client_brkany (c
, brkterm
)) {
421 while ((c
= *++sp
) && !client_brkany (c
, brksep
) && !client_brkany (c
, brkterm
))
431 * returns 1 if chr in strg, 0 otherwise
434 client_brkany (char chr
, char *strg
)
439 for (sp
= strg
; *sp
; sp
++)
447 * copy a string array and return pointer to end
450 client_copyip (char **p
, char **q
, int len_q
)
452 while (*p
&& --len_q
> 0)
462 client_getcpy (char *str
)
467 len
= strlen(str
) + 1;
468 if (!(cp
= malloc(len
)))
471 memcpy (cp
, str
, len
);