]>
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.
16 #include <sys/socket.h>
17 #include <netinet/in.h>
20 #ifdef HAVE_ARPA_INET_H
21 # include <arpa/inet.h>
44 int a_addrtype
; /* assumes AF_INET for inet_netof () */
51 #define a_net un.un_net
52 #define a_addr un.un_addr
54 static struct addrent
*n1
, *n2
;
55 static struct addrent nets
[MAXNETS
];
56 static struct addrent
*h1
, *h2
;
57 static struct addrent hosts
[MAXHOSTS
];
60 static CREDENTIALS cred
;
61 static MSG_DAT msg_data
;
62 static KTEXT ticket
= (KTEXT
) NULL
;
63 static Key_schedule schedule
;
64 static char *kservice
; /* "pop" if using kpop */
65 char krb_realm
[REALM_SZ
];
66 char *PrincipalHostname();
70 # define h_addr h_addr_list[0]
73 #define inaddr_copy(hp,sin) \
74 memcpy(&((sin)->sin_addr), (hp)->h_addr, (hp)->h_length)
79 static int rcaux (struct servent
*, struct hostent
*, int, char *, int);
80 static int getport (int, int, char *, int);
81 static int inet (struct hostent
*, int);
82 struct hostent
*gethostbystring (char *s
);
84 /* client's own static version of several nmh subroutines */
85 static char **client_brkstring (char *, char *, char *);
86 static int client_brkany (char, char *);
87 static char **client_copyip (char **, char **, int);
88 static char *client_getcpy (char *);
92 client (char *args
, char *protocol
, char *service
, int rproto
,
93 char *response
, int len_response
)
97 char *arguments
[MAXARGS
];
98 register struct hostent
*hp
;
99 register struct servent
*sp
;
100 #ifndef HAVE_GETHOSTBYNAME
101 register struct netent
*np
;
108 if (cp
= strchr (service
, '/')) { /* "pop/kpop" */
109 *cp
++ = '\0'; /* kservice = "pop" */
110 service
= cp
; /* service = "kpop" */
112 kservice
= NULL
; /* not using KERBEROS */
117 if ((sp
= getservbyname (service
, protocol
)) == NULL
) {
119 if ((sp
= hes_getservbyname (service
, protocol
)) == NULL
) {
120 snprintf (response
, len_response
, "%s/%s: unknown service", protocol
, service
);
124 snprintf (response
, len_response
, "%s/%s: unknown service", protocol
, service
);
130 if (args
!= NULL
&& *args
!= 0) {
131 ap
= client_copyip (client_brkstring (client_getcpy (args
), " ", "\n"),
134 if (servers
!= NULL
&& *servers
!= 0)
135 ap
= client_copyip (client_brkstring (client_getcpy (servers
), " ", "\n"),
138 if (ap
== arguments
) {
139 *ap
++ = client_getcpy ("localhost");
144 n2
= nets
+ sizeof(nets
) / sizeof(nets
[0]);
147 h2
= hosts
+ sizeof(hosts
) / sizeof(hosts
[0]);
149 for (ap
= arguments
; *ap
; ap
++) {
152 * the assumption here is that if the system doesn't have a
153 * gethostbyname() function, it must not use DNS. So we need to look
154 * into the /etc/hosts using gethostent(). There probablly aren't any
155 * systems still like this, but you never know. On every system I have
156 * access to, this section is ignored.
158 #ifndef HAVE_GETHOSTBYNAME
159 if ((np
= getnetbyname (*ap
+ 1))) {
160 #ifdef HAVE_SETHOSTENT
162 #endif /* HAVE_SETHOSTENT */
163 while ((hp
= gethostent()))
164 if (np
->n_addrtype
== hp
->h_addrtype
165 && inet (hp
, np
->n_net
)) {
166 switch (sd
= rcaux (sp
, hp
, rproto
, response
, len_response
)) {
180 #endif /* don't HAVE_GETHOSTBYNAME */
184 if ((hp
= gethostbystring (*ap
))) {
185 switch (sd
= rcaux (sp
, hp
, rproto
, response
, len_response
)) {
199 strncpy (response
, "no servers available", len_response
);
205 rcaux (struct servent
*sp
, struct hostent
*hp
, int rproto
,
206 char *response
, int len_response
)
210 register struct addrent
*ap
;
211 struct sockaddr_in in_socket
;
212 register struct sockaddr_in
*isock
= &in_socket
;
219 for (ap
= nets
; ap
< n1
; ap
++)
220 if (ap
->a_addrtype
== hp
->h_addrtype
&& inet (hp
, ap
->a_net
))
223 for (ap
= hosts
; ap
< h1
; ap
++)
224 if (ap
->a_addrtype
== hp
->h_addrtype
225 && memcmp(ap
->a_addr
, hp
->h_addr
, hp
->h_length
) == 0)
228 if ((sd
= getport (rproto
, hp
->h_addrtype
, response
, len_response
)) == NOTOK
)
231 memset (isock
, 0, sizeof(*isock
));
232 isock
->sin_family
= hp
->h_addrtype
;
233 inaddr_copy (hp
, isock
);
234 isock
->sin_port
= sp
->s_port
;
236 if (connect (sd
, (struct sockaddr
*) isock
, sizeof(*isock
)) == NOTOK
)
242 n1
->a_addrtype
= hp
->h_addrtype
;
243 memcpy(&in
, hp
->h_addr
, sizeof(in
));
244 n1
->a_net
= inet_netof (in
);
254 h1
->a_addrtype
= hp
->h_addrtype
;
255 memcpy(h1
->a_addr
, hp
->h_addr
, hp
->h_length
);
262 if (kservice
) { /* "pop" */
265 if (( hp2
= gethostbyaddr( hp
->h_addr
, hp
->h_length
, hp
->h_addrtype
))
269 if ((instance
= strdup (hp2
->h_name
)) == NULL
) {
271 strncpy (response
, "Out of memory.", len_response
);
274 ticket
= (KTEXT
) mh_xmalloc (sizeof(KTEXT_ST
));
275 rem
= krb_sendauth (0L, sd
, ticket
, kservice
, instance
,
276 (char *) krb_realmofhost (instance
),
277 (unsigned long) 0, &msg_data
, &cred
, schedule
,
278 (struct sockaddr_in
*) NULL
,
279 (struct sockaddr_in
*) NULL
,
282 if (rem
!= KSUCCESS
) {
284 strncpy (response
, "Post office refused connection: ", len_response
);
285 strncat (response
, krb_err_txt
[rem
], len_response
- strlen(response
));
296 getport (int rproto
, int addrtype
, char *response
, int len_response
)
299 struct sockaddr_in in_socket
, *isock
;
302 if (rproto
&& addrtype
!= AF_INET
) {
303 snprintf (response
, len_response
, "reserved ports not supported for af=%d", addrtype
);
308 if ((sd
= socket (AF_INET
, SOCK_STREAM
, 0)) == NOTOK
) {
311 if ((s
= strerror (errno
)))
312 snprintf (response
, len_response
, "unable to create socket: %s", s
);
314 snprintf (response
, len_response
, "unable to create socket: unknown error");
318 if (kservice
) /* "pop" */
324 memset(isock
, 0, sizeof(*isock
));
325 isock
->sin_family
= addrtype
;
326 for (port
= IPPORT_RESERVED
- 1;;) {
327 isock
->sin_port
= htons ((unsigned short) port
);
328 if (bind (sd
, (struct sockaddr
*) isock
, sizeof(*isock
)) != NOTOK
)
336 if (--port
<= IPPORT_RESERVED
/ 2) {
337 strncpy (response
, "ports available", len_response
);
343 if ((s
= strerror (errno
)))
344 snprintf (response
, len_response
, "unable to bind socket: %s", s
);
346 snprintf (response
, len_response
, "unable to bind socket: unknown error");
354 inet (struct hostent
*hp
, int net
)
358 memcpy(&in
, hp
->h_addr
, sizeof(in
));
359 return (inet_netof (in
) == net
);
364 * taken from ISODE's compat/internet.c
367 static char *empty
= NULL
;
370 static char *addrs
[2] = { NULL
};
374 gethostbystring (char *s
)
376 register struct hostent
*h
;
377 static struct hostent hs
;
379 static struct in_addr iaddr
;
381 static unsigned long iaddr
;
384 iaddr
= inet_addr (s
);
386 if (iaddr
.s_addr
== NOTOK
&& strcmp (s
, "255.255.255.255"))
388 if (((int) iaddr
== NOTOK
) && strcmp (s
, "255.255.255.255"))
390 return gethostbyname (s
);
394 h
->h_aliases
= &empty
;
395 h
->h_addrtype
= AF_INET
;
396 h
->h_length
= sizeof(iaddr
);
398 h
->h_addr_list
= addrs
;
399 memset(addrs
, 0, sizeof(addrs
));
401 h
->h_addr
= (char *) &iaddr
;
408 * static copies of three nmh subroutines
411 static char *broken
[MAXARGS
+ 1];
414 client_brkstring (char *strg
, char *brksep
, char *brkterm
)
417 register char c
, *sp
;
421 for (bi
= 0; bi
< MAXARGS
; bi
++) {
422 while (client_brkany (c
= *sp
, brksep
))
424 if (!c
|| client_brkany (c
, brkterm
)) {
431 while ((c
= *++sp
) && !client_brkany (c
, brksep
) && !client_brkany (c
, brkterm
))
441 * returns 1 if chr in strg, 0 otherwise
444 client_brkany (char chr
, char *strg
)
449 for (sp
= strg
; *sp
; sp
++)
457 * copy a string array and return pointer to end
460 client_copyip (char **p
, char **q
, int len_q
)
462 while (*p
&& --len_q
> 0)
472 client_getcpy (char *str
)
477 len
= strlen(str
) + 1;
478 cp
= mh_xmalloc(len
);
480 memcpy (cp
, str
, len
);