1*d316f7c9SJohn Marino /*-
2f41183adSMatthew Dillon * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
3f41183adSMatthew Dillon * Based on original work by Atsushi Murai <amurai@FreeBSD.org>
4f41183adSMatthew Dillon * All rights reserved.
5f41183adSMatthew Dillon *
6f41183adSMatthew Dillon * Redistribution and use in source and binary forms, with or without
7f41183adSMatthew Dillon * modification, are permitted provided that the following conditions
8f41183adSMatthew Dillon * are met:
9f41183adSMatthew Dillon * 1. Redistributions of source code must retain the above copyright
10f41183adSMatthew Dillon * notice, this list of conditions and the following disclaimer.
11f41183adSMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
12f41183adSMatthew Dillon * notice, this list of conditions and the following disclaimer in the
13f41183adSMatthew Dillon * documentation and/or other materials provided with the distribution.
14f41183adSMatthew Dillon *
15f41183adSMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16f41183adSMatthew Dillon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17f41183adSMatthew Dillon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18f41183adSMatthew Dillon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19f41183adSMatthew Dillon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20f41183adSMatthew Dillon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21f41183adSMatthew Dillon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22f41183adSMatthew Dillon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23f41183adSMatthew Dillon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24f41183adSMatthew Dillon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25f41183adSMatthew Dillon * SUCH DAMAGE.
26f41183adSMatthew Dillon *
27*d316f7c9SJohn Marino * $FreeBSD: head/lib/libutil/trimdomain.c 150955 2005-10-05 04:42:20Z brooks $
28f41183adSMatthew Dillon */
29f41183adSMatthew Dillon
30f41183adSMatthew Dillon #include <sys/param.h>
31f41183adSMatthew Dillon
32*d316f7c9SJohn Marino #include <libutil.h>
33f41183adSMatthew Dillon #include <string.h>
34f41183adSMatthew Dillon #include <unistd.h>
35f41183adSMatthew Dillon
36f41183adSMatthew Dillon static int isDISP(const char *);
37f41183adSMatthew Dillon
38f41183adSMatthew Dillon /*-
39f41183adSMatthew Dillon * Trim the current domain name from fullhost, but only if the result
40f41183adSMatthew Dillon * is less than or equal to hostsize in length.
41f41183adSMatthew Dillon *
42f41183adSMatthew Dillon * This function understands $DISPLAY type fullhosts.
43f41183adSMatthew Dillon *
44f41183adSMatthew Dillon * For example:
45f41183adSMatthew Dillon *
46f41183adSMatthew Dillon * trimdomain("abcde.my.domain", 5) -> "abcde"
47f41183adSMatthew Dillon * trimdomain("abcde.my.domain", 4) -> "abcde.my.domain"
48f41183adSMatthew Dillon * trimdomain("abcde.my.domain:0.0", 9) -> "abcde:0.0"
49f41183adSMatthew Dillon * trimdomain("abcde.my.domain:0.0", 8) -> "abcde.my.domain:0.0"
50f41183adSMatthew Dillon */
51f41183adSMatthew Dillon void
trimdomain(char * fullhost,int hostsize)52f41183adSMatthew Dillon trimdomain(char *fullhost, int hostsize)
53f41183adSMatthew Dillon {
54f41183adSMatthew Dillon static size_t dlen;
55f41183adSMatthew Dillon static int first = 1;
56f41183adSMatthew Dillon static char domain[MAXHOSTNAMELEN];
57f41183adSMatthew Dillon char *end, *s;
58f41183adSMatthew Dillon size_t len;
59f41183adSMatthew Dillon
60f41183adSMatthew Dillon if (first) {
61f41183adSMatthew Dillon /* XXX: Should we assume that our domain is this persistent ? */
62f41183adSMatthew Dillon first = 0;
63f41183adSMatthew Dillon if (gethostname(domain, sizeof(domain) - 1) == 0 &&
64f41183adSMatthew Dillon (s = strchr(domain, '.')) != NULL)
65f41183adSMatthew Dillon memmove(domain, s + 1, strlen(s + 1) + 1);
66f41183adSMatthew Dillon else
67f41183adSMatthew Dillon domain[0] = '\0';
68f41183adSMatthew Dillon dlen = strlen(domain);
69f41183adSMatthew Dillon }
70f41183adSMatthew Dillon
71f41183adSMatthew Dillon if (domain[0] == '\0')
72f41183adSMatthew Dillon return;
73f41183adSMatthew Dillon
74f41183adSMatthew Dillon s = fullhost;
75f41183adSMatthew Dillon end = s + hostsize + 1;
76*d316f7c9SJohn Marino if ((s = memchr(s, '.', (size_t)(end - s))) != NULL) {
77f41183adSMatthew Dillon if (strncasecmp(s + 1, domain, dlen) == 0) {
78f41183adSMatthew Dillon if (s[dlen + 1] == '\0') {
79f41183adSMatthew Dillon /* Found -- lose the domain. */
80f41183adSMatthew Dillon *s = '\0';
81f41183adSMatthew Dillon } else if (s[dlen + 1] == ':' &&
82f41183adSMatthew Dillon isDISP(s + dlen + 2) &&
83f41183adSMatthew Dillon (len = strlen(s + dlen + 1)) < (size_t)(end - s)) {
84f41183adSMatthew Dillon /* Found -- shuffle the DISPLAY back. */
85f41183adSMatthew Dillon memmove(s, s + dlen + 1, len + 1);
86f41183adSMatthew Dillon }
87f41183adSMatthew Dillon }
88f41183adSMatthew Dillon }
89f41183adSMatthew Dillon }
90f41183adSMatthew Dillon
91f41183adSMatthew Dillon /*
92f41183adSMatthew Dillon * Is the given string NN or NN.NN where ``NN'' is an all-numeric string ?
93f41183adSMatthew Dillon */
94f41183adSMatthew Dillon static int
isDISP(const char * disp)95f41183adSMatthew Dillon isDISP(const char *disp)
96f41183adSMatthew Dillon {
97f41183adSMatthew Dillon size_t w;
98f41183adSMatthew Dillon int res;
99f41183adSMatthew Dillon
100f41183adSMatthew Dillon w = strspn(disp, "0123456789");
101f41183adSMatthew Dillon res = 0;
102f41183adSMatthew Dillon if (w > 0) {
103f41183adSMatthew Dillon if (disp[w] == '\0')
104f41183adSMatthew Dillon res = 1; /* NN */
105f41183adSMatthew Dillon else if (disp[w] == '.') {
106f41183adSMatthew Dillon disp += w + 1;
107f41183adSMatthew Dillon w = strspn(disp, "0123456789");
108f41183adSMatthew Dillon if (w > 0 && disp[w] == '\0')
109f41183adSMatthew Dillon res = 1; /* NN.NN */
110f41183adSMatthew Dillon }
111f41183adSMatthew Dillon }
112f41183adSMatthew Dillon return (res);
113f41183adSMatthew Dillon }
114