lutil.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 libraries 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. /* $XConsortium: lutil.c /main/4 1996/11/21 20:00:35 drk $ */
  24. /*
  25. * (c) Copyright 1993, 1994 Hewlett-Packard Company
  26. * (c) Copyright 1993, 1994 International Business Machines Corp.
  27. * (c) Copyright 1993, 1994 Novell, Inc.
  28. * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  29. */
  30. #include <sys/param.h>
  31. #include <EUSCompat.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <rpc/rpc.h>
  36. #include <unistd.h>
  37. #if defined(sun)
  38. #include <netdb.h>
  39. #include <sys/systeminfo.h>
  40. #endif
  41. #define X_INCLUDE_PWD_H
  42. #define XOS_USE_XT_LOCKING
  43. #if defined(__linux__)
  44. #undef SVR4
  45. #endif
  46. #include <X11/Xos_r.h>
  47. #if defined(__linux__)
  48. #define SVR4
  49. #endif
  50. #include "lutil.h"
  51. #if !defined(__linux__)
  52. extern char * strdup(const char *);
  53. #endif
  54. extern char *
  55. _DtCmGetPrefix(char *str, char sep)
  56. {
  57. char buf[BUFSIZ];
  58. char *ptr;
  59. if (str == NULL)
  60. return(NULL);
  61. ptr = buf;
  62. while (*str && *str != sep)
  63. *ptr++ = *str++;
  64. if (ptr == buf)
  65. return(NULL);
  66. else {
  67. *ptr = '\0';
  68. return(strdup(buf));
  69. }
  70. }
  71. extern char *
  72. _DtCmGetLocalHost(void)
  73. {
  74. static char *host = NULL;
  75. if (host == NULL) {
  76. host = (char *)malloc(MAXHOSTNAMELEN+1);
  77. #if defined(sun)
  78. (void)sysinfo(SI_HOSTNAME, host, MAXHOSTNAMELEN);
  79. #else
  80. (void)gethostname(host, MAXHOSTNAMELEN);
  81. #endif /* sun */
  82. }
  83. return (host);
  84. }
  85. extern char *
  86. _DtCmGetLocalDomain(char *hostname)
  87. {
  88. static char *domain = NULL;
  89. char buf[BUFSIZ], *ptr;
  90. CLIENT *cl;
  91. if (domain == NULL) {
  92. domain = (char *)malloc(BUFSIZ);
  93. #if defined(sun)
  94. sysinfo(SI_SRPC_DOMAIN, domain, BUFSIZ - 1);
  95. #else
  96. getdomainname(domain, BUFSIZ - 1);
  97. #endif /* sun */
  98. /* check domain name */
  99. /* this is a hack to find out the domain name that
  100. * is acceptable to the rpc interface, e.g.
  101. * DGDO.Eng.Sun.COM is returned by sysinfo but
  102. * this name is not acceptable to the rpc interface
  103. * hence we need to stripe out the first component
  104. */
  105. ptr = domain;
  106. if (hostname == NULL) hostname = _DtCmGetLocalHost();
  107. while (1) {
  108. snprintf(buf, sizeof buf, "%s.%s", hostname, ptr);
  109. if ((cl = clnt_create(buf, 100068, 5, "udp")) == NULL) {
  110. ptr = strchr(ptr, '.');
  111. if (ptr)
  112. ptr++;
  113. else
  114. break;
  115. } else {
  116. clnt_destroy(cl);
  117. break;
  118. }
  119. }
  120. if (ptr && ptr != domain)
  121. domain = ptr;
  122. }
  123. return (domain);
  124. }
  125. extern char *
  126. _DtCmGetHostAtDomain(void)
  127. {
  128. static char *hostname = NULL;
  129. char *host;
  130. if (hostname == NULL) {
  131. hostname = malloc(BUFSIZ);
  132. host = _DtCmGetLocalHost();
  133. if (strchr(host, '.') == NULL)
  134. snprintf(hostname, BUFSIZ, "%s.%s", host,
  135. _DtCmGetLocalDomain(host));
  136. else
  137. /* XXX strcpy unsafe here */
  138. strcpy(hostname, host);
  139. }
  140. return (hostname);
  141. }
  142. extern char *
  143. _DtCmGetUserName(void)
  144. {
  145. static char *name = NULL;
  146. _Xgetpwparams pwd_buf;
  147. struct passwd * pwd_ret;
  148. if (name == NULL) {
  149. name = malloc(BUFSIZ);
  150. if ((pwd_ret = _XGetpwuid(geteuid(), pwd_buf)) == NULL)
  151. strcpy(name, "nobody");
  152. else
  153. strcpy(name, pwd_ret->pw_name);
  154. }
  155. return name;
  156. }
  157. /*
  158. * this routine checks whether the given name is a valid user name
  159. */
  160. extern boolean_t
  161. _DtCmIsUserName(char *user)
  162. {
  163. _Xgetpwparams pwd_buf;
  164. struct passwd * pwd_ret;
  165. pwd_ret = _XGetpwnam(user, pwd_buf);
  166. if (pwd_ret == NULL)
  167. return (B_FALSE);
  168. else
  169. return (B_TRUE);
  170. }