]>
diplodocus.org Git - nmh/blob - sbr/client.c
3 * client.c -- connect to a server
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
15 #include <sys/socket.h>
16 #include <netinet/in.h>
19 #ifdef HAVE_ARPA_INET_H
20 # include <arpa/inet.h>
43 int a_addrtype
; /* assumes AF_INET for inet_netof () */
50 #define a_net un.un_net
51 #define a_addr un.un_addr
53 static struct addrent
*n1
, *n2
;
54 static struct addrent nets
[MAXNETS
];
55 static struct addrent
*h1
, *h2
;
56 static struct addrent hosts
[MAXHOSTS
];
59 static CREDENTIALS cred
;
60 static MSG_DAT msg_data
;
61 static KTEXT ticket
= (KTEXT
) NULL
;
62 static Key_schedule schedule
;
63 static char *kservice
; /* "pop" if using kpop */
64 char krb_realm
[REALM_SZ
];
65 char *PrincipalHostname();
69 # define h_addr h_addr_list[0]
72 #define inaddr_copy(hp,sin) \
73 memcpy(&((sin)->sin_addr), (hp)->h_addr, (hp)->h_length)
78 static int rcaux (struct servent
*, struct hostent
*, int, char *, int);
79 static int getport (int, int, char *, int);
80 static int inet (struct hostent
*, int);
81 struct hostent
*gethostbystring (char *s
);
83 /* client's own static version of several nmh subroutines */
84 static char **client_brkstring (char *, char *, char *);
85 static int client_brkany (char, char *);
86 static char **client_copyip (char **, char **, int);
87 static char *client_getcpy (char *);
91 client (char *args
, char *protocol
, char *service
, int rproto
,
92 char *response
, int len_response
)
96 char *arguments
[MAXARGS
];
97 register struct hostent
*hp
;
98 register struct servent
*sp
;
99 #ifndef HAVE_GETHOSTBYNAME
100 register struct netent
*np
;
107 if (cp
= strchr (service
, '/')) { /* "pop/kpop" */
108 *cp
++ = '\0'; /* kservice = "pop" */
109 service
= cp
; /* service = "kpop" */
111 kservice
= NULL
; /* not using KERBEROS */
116 if ((sp
= getservbyname (service
, protocol
)) == NULL
) {
118 if ((sp
= hes_getservbyname (service
, protocol
)) == NULL
) {
119 snprintf (response
, len_response
, "%s/%s: unknown service", protocol
, service
);
123 snprintf (response
, len_response
, "%s/%s: unknown service", protocol
, service
);
129 if (args
!= NULL
&& *args
!= 0) {
130 ap
= client_copyip (client_brkstring (client_getcpy (args
), " ", "\n"),
133 if (servers
!= NULL
&& *servers
!= 0)
134 ap
= client_copyip (client_brkstring (client_getcpy (servers
), " ", "\n"),
137 if (ap
== arguments
) {
138 *ap
++ = client_getcpy ("localhost");
143 n2
= nets
+ sizeof(nets
) / sizeof(nets
[0]);
146 h2
= hosts
+ sizeof(hosts
) / sizeof(hosts
[0]);
148 for (ap
= arguments
; *ap
; ap
++) {
151 * the assumption here is that if the system doesn't have a
152 * gethostbyname() function, it must not use DNS. So we need to look
153 * into the /etc/hosts using gethostent(). There probablly aren't any
154 * systems still like this, but you never know. On every system I have
155 * access to, this section is ignored.
157 #ifndef HAVE_GETHOSTBYNAME
158 if ((np
= getnetbyname (*ap
+ 1))) {
159 #ifdef HAVE_SETHOSTENT
161 #endif /* HAVE_SETHOSTENT */
162 while ((hp
= gethostent()))
163 if (np
->n_addrtype
== hp
->h_addrtype
164 && inet (hp
, np
->n_net
)) {
165 switch (sd
= rcaux (sp
, hp
, rproto
, response
, len_response
)) {
179 #endif /* don't HAVE_GETHOSTBYNAME */
183 if ((hp
= gethostbystring (*ap
))) {
184 switch (sd
= rcaux (sp
, hp
, rproto
, response
, len_response
)) {
198 strncpy (response
, "no servers available", len_response
);
204 rcaux (struct servent
*sp
, struct hostent
*hp
, int rproto
,
205 char *response
, int len_response
)
209 register struct addrent
*ap
;
210 struct sockaddr_in in_socket
;
211 register struct sockaddr_in
*isock
= &in_socket
;
218 for (ap
= nets
; ap
< n1
; ap
++)
219 if (ap
->a_addrtype
== hp
->h_addrtype
&& inet (hp
, ap
->a_net
))
222 for (ap
= hosts
; ap
< h1
; ap
++)
223 if (ap
->a_addrtype
== hp
->h_addrtype
224 && memcmp(ap
->a_addr
, hp
->h_addr
, hp
->h_length
) == 0)
227 if ((sd
= getport (rproto
, hp
->h_addrtype
, response
, len_response
)) == NOTOK
)
230 memset (isock
, 0, sizeof(*isock
));
231 isock
->sin_family
= hp
->h_addrtype
;
232 inaddr_copy (hp
, isock
);
233 isock
->sin_port
= sp
->s_port
;
235 if (connect (sd
, (struct sockaddr
*) isock
, sizeof(*isock
)) == NOTOK
)
241 n1
->a_addrtype
= hp
->h_addrtype
;
242 memcpy(&in
, hp
->h_addr
, sizeof(in
));
243 n1
->a_net
= inet_netof (in
);
253 h1
->a_addrtype
= hp
->h_addrtype
;
254 memcpy(h1
->a_addr
, hp
->h_addr
, hp
->h_length
);
261 if (kservice
) { /* "pop" */
264 if (( hp2
= gethostbyaddr( hp
->h_addr
, hp
->h_length
, hp
->h_addrtype
))
268 if ((instance
= strdup (hp2
->h_name
)) == NULL
) {
270 strncpy (response
, "Out of memory.", len_response
);
273 ticket
= (KTEXT
) malloc (sizeof(KTEXT_ST
));
274 rem
= krb_sendauth (0L, sd
, ticket
, kservice
, instance
,
275 (char *) krb_realmofhost (instance
),
276 (unsigned long) 0, &msg_data
, &cred
, schedule
,
277 (struct sockaddr_in
*) NULL
,
278 (struct sockaddr_in
*) NULL
,
281 if (rem
!= KSUCCESS
) {
283 strncpy (response
, "Post office refused connection: ", len_response
);
284 strncat (response
, krb_err_txt
[rem
], len_response
- strlen(response
));
295 getport (int rproto
, int addrtype
, char *response
, int len_response
)
298 struct sockaddr_in in_socket
, *isock
;
301 if (rproto
&& addrtype
!= AF_INET
) {
302 snprintf (response
, len_response
, "reserved ports not supported for af=%d", addrtype
);
307 if ((sd
= socket (AF_INET
, SOCK_STREAM
, 0)) == NOTOK
) {
310 if ((s
= strerror (errno
)))
311 snprintf (response
, len_response
, "unable to create socket: %s", s
);
313 snprintf (response
, len_response
, "unable to create socket: unknown error");
317 if (kservice
) /* "pop" */
323 memset(isock
, 0, sizeof(*isock
));
324 isock
->sin_family
= addrtype
;
325 for (port
= IPPORT_RESERVED
- 1;;) {
326 isock
->sin_port
= htons ((unsigned short) port
);
327 if (bind (sd
, (struct sockaddr
*) isock
, sizeof(*isock
)) != NOTOK
)
335 if (--port
<= IPPORT_RESERVED
/ 2) {
336 strncpy (response
, "ports available", len_response
);
342 if ((s
= strerror (errno
)))
343 snprintf (response
, len_response
, "unable to bind socket: %s", s
);
345 snprintf (response
, len_response
, "unable to bind socket: unknown error");
353 inet (struct hostent
*hp
, int net
)
357 memcpy(&in
, hp
->h_addr
, sizeof(in
));
358 return (inet_netof (in
) == net
);
363 * taken from ISODE's compat/internet.c
366 static char *empty
= NULL
;
369 static char *addrs
[2] = { NULL
};
373 gethostbystring (char *s
)
375 register struct hostent
*h
;
376 static struct hostent hs
;
378 static struct in_addr iaddr
;
380 static unsigned long iaddr
;
383 iaddr
= inet_addr (s
);
385 if (iaddr
.s_addr
== NOTOK
&& strcmp (s
, "255.255.255.255"))
387 if (((int) iaddr
== NOTOK
) && strcmp (s
, "255.255.255.255"))
389 return gethostbyname (s
);
393 h
->h_aliases
= &empty
;
394 h
->h_addrtype
= AF_INET
;
395 h
->h_length
= sizeof(iaddr
);
397 h
->h_addr_list
= addrs
;
398 memset(addrs
, 0, sizeof(addrs
));
400 h
->h_addr
= (char *) &iaddr
;
407 * static copies of three nmh subroutines
410 static char *broken
[MAXARGS
+ 1];
413 client_brkstring (char *strg
, char *brksep
, char *brkterm
)
416 register char c
, *sp
;
420 for (bi
= 0; bi
< MAXARGS
; bi
++) {
421 while (client_brkany (c
= *sp
, brksep
))
423 if (!c
|| client_brkany (c
, brkterm
)) {
430 while ((c
= *++sp
) && !client_brkany (c
, brksep
) && !client_brkany (c
, brkterm
))
440 * returns 1 if chr in strg, 0 otherwise
443 client_brkany (char chr
, char *strg
)
448 for (sp
= strg
; *sp
; sp
++)
456 * copy a string array and return pointer to end
459 client_copyip (char **p
, char **q
, int len_q
)
461 while (*p
&& --len_q
> 0)
471 client_getcpy (char *str
)
476 len
= strlen(str
) + 1;
477 if (!(cp
= malloc(len
)))
480 memcpy (cp
, str
, len
);