Merge branch 'linux1'
[oweals/cde.git] / cde / lib / csa / lutil.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23
24 /* $XConsortium: lutil.c /main/4 1996/11/21 20:00:35 drk $ */
25 /*
26  *  (c) Copyright 1993, 1994 Hewlett-Packard Company
27  *  (c) Copyright 1993, 1994 International Business Machines Corp.
28  *  (c) Copyright 1993, 1994 Novell, Inc.
29  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
30  */
31
32 #include <EUSCompat.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <rpc/rpc.h>
37 #include <unistd.h>
38 #if defined(sun) || defined(USL) || defined(__uxp__)
39 #include <netdb.h>
40 #include <sys/systeminfo.h>
41 #endif
42 #define X_INCLUDE_PWD_H
43 #define XOS_USE_XT_LOCKING
44 #if defined(linux)
45 #undef SVR4
46 #endif
47 #include <X11/Xos_r.h>
48 #if defined(linux)
49 #define SVR4
50 #endif
51
52 #if !defined(linux)
53 extern char * strdup(const char *);
54 #endif
55
56 extern char *
57 _DtCmGetPrefix(char *str, char sep)
58 {
59         char buf[BUFSIZ];
60         char *ptr;
61
62         if (str == NULL)
63                 return(NULL);
64
65         ptr = buf;
66         while (*str && *str != sep)
67                 *ptr++ = *str++;
68         if (ptr == buf)
69                 return(NULL);
70         else {
71                 *ptr = NULL;
72                 return(strdup(buf));
73         }
74 }
75
76 extern char *
77 _DtCmGetLocalHost()
78 {
79         static char *host = NULL;
80
81         if (host == NULL) {
82                 host = (char *)malloc(MAXHOSTNAMELEN+1);
83 #if defined(sun) || defined(USL) || defined(__uxp__)
84                 (void)sysinfo(SI_HOSTNAME, host, MAXHOSTNAMELEN);
85 #else
86                 (void)gethostname(host, MAXHOSTNAMELEN);
87 #endif /* sun || USL || __uxp__ */
88         }
89
90         return (host);
91 }
92
93 extern char *
94 _DtCmGetLocalDomain(char *hostname)
95 {
96         static char     *domain = NULL;
97         char            buf[BUFSIZ], *ptr;
98         CLIENT          *cl;
99
100         if (domain == NULL) {
101                 domain = (char *)malloc(BUFSIZ);
102 #if defined(sun) || defined(USL) || defined(__uxp__)
103                 sysinfo(SI_SRPC_DOMAIN, domain, BUFSIZ - 1);
104 #else
105                 getdomainname(domain, BUFSIZ - 1);
106 #endif /* sun || USL || __uxp__ */
107
108                 /* check domain name */
109                 /* this is a hack to find out the domain name that
110                  * is acceptable to the rpc interface, e.g.
111                  * DGDO.Eng.Sun.COM is returned by sysinfo but
112                  * this name is not acceptable to the rpc interface
113                  * hence we need to stripe out the first component
114                  */
115                 ptr = domain;
116                 if (hostname == NULL) hostname = _DtCmGetLocalHost();
117                 while (1) {
118                         sprintf(buf, "%s.%s", hostname, ptr);
119                         if ((cl = clnt_create(buf, 100068, 5, "udp")) == NULL) {
120                                 ptr = strchr(ptr, '.');
121                                 if (ptr)
122                                         ptr++;
123                                 else
124                                         break;
125                         } else {
126                                 clnt_destroy(cl);
127                                 break;
128                         }
129                 }
130                 if (ptr && ptr != domain)
131                         domain = ptr;
132         }
133
134         return (domain);
135 }
136
137 extern char *
138 _DtCmGetHostAtDomain()
139 {
140         static char     *hostname = NULL;
141         char            *host;
142
143         if (hostname == NULL) {
144                 hostname = malloc(BUFSIZ);
145
146                 host = _DtCmGetLocalHost();
147                 if (strchr(host, '.') == NULL)
148                         sprintf(hostname, "%s.%s", host,
149                                 _DtCmGetLocalDomain(host));
150                 else
151                         strcpy(hostname, host);
152         }
153
154         return (hostname);
155 }
156
157 extern char *
158 _DtCmGetUserName()
159 {
160         static char *name = NULL;
161         _Xgetpwparams   pwd_buf;
162         struct passwd * pwd_ret;
163
164         if (name == NULL) {
165           name = malloc(BUFSIZ);
166
167           if ((pwd_ret = _XGetpwuid(geteuid(), pwd_buf)) == NULL)
168             strcpy(name, "nobody");
169           else
170             strcpy(name, pwd_ret->pw_name);
171         }
172
173         return name;
174 }
175
176 /*
177  * this routine checks whether the given name is a valid user name
178  */
179 extern boolean_t
180 _DtCmIsUserName(char *user)
181 {
182         _Xgetpwparams   pwd_buf;
183         struct passwd * pwd_ret;
184
185         pwd_ret = _XGetpwnam(user, pwd_buf);
186         if (pwd_ret == NULL)
187                 return (B_FALSE);
188         else
189                 return (B_TRUE);
190 }
191