netname.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Copyright (C) 1997-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <rpc/rpc.h>
  19. #include <shlib-compat.h>
  20. #include "nsswitch.h"
  21. #define OPSYS_LEN 4
  22. #define MAXIPRINT (11) /* max length of printed integer */
  23. static const char OPSYS[] = "unix";
  24. int
  25. user2netname (char netname[MAXNETNAMELEN + 1], const uid_t uid,
  26. const char *domain)
  27. {
  28. char dfltdom[MAXNETNAMELEN + 1];
  29. size_t i;
  30. if (domain == NULL)
  31. {
  32. if (getdomainname (dfltdom, sizeof (dfltdom)) < 0)
  33. return 0;
  34. }
  35. else
  36. {
  37. strncpy (dfltdom, domain, MAXNETNAMELEN);
  38. dfltdom[MAXNETNAMELEN] = '\0';
  39. }
  40. if ((strlen (dfltdom) + OPSYS_LEN + 3 + MAXIPRINT) > (size_t) MAXNETNAMELEN)
  41. return 0;
  42. sprintf (netname, "%s.%d@%s", OPSYS, uid, dfltdom);
  43. i = strlen (netname);
  44. if (netname[i - 1] == '.')
  45. netname[i - 1] = '\0';
  46. return 1;
  47. }
  48. libc_hidden_nolink_sunrpc (user2netname, GLIBC_2_1)
  49. int
  50. host2netname (char netname[MAXNETNAMELEN + 1], const char *host,
  51. const char *domain)
  52. {
  53. char *p;
  54. char hostname[MAXHOSTNAMELEN + 1];
  55. char domainname[MAXHOSTNAMELEN + 1];
  56. char *dot_in_host;
  57. size_t i;
  58. netname[0] = '\0'; /* make null first (no need for memset) */
  59. if (host == NULL)
  60. __gethostname (hostname, MAXHOSTNAMELEN);
  61. else
  62. {
  63. strncpy (hostname, host, MAXHOSTNAMELEN);
  64. hostname[MAXHOSTNAMELEN] = '\0';
  65. }
  66. dot_in_host = strchr (hostname, '.');
  67. if (domain == NULL)
  68. {
  69. p = dot_in_host;
  70. if (p)
  71. {
  72. ++p;
  73. strncpy (domainname, p, MAXHOSTNAMELEN);
  74. domainname[MAXHOSTNAMELEN] = '\0';
  75. }
  76. else
  77. {
  78. domainname[0] = 0;
  79. getdomainname (domainname, MAXHOSTNAMELEN);
  80. }
  81. }
  82. else
  83. {
  84. strncpy (domainname, domain, MAXHOSTNAMELEN);
  85. domainname[MAXHOSTNAMELEN] = '\0';
  86. }
  87. i = strlen (domainname);
  88. if (i == 0)
  89. /* No domainname */
  90. return 0;
  91. if (domainname[i - 1] == '.')
  92. domainname[i - 1] = 0;
  93. if (dot_in_host) /* strip off rest of name */
  94. *dot_in_host = '\0';
  95. if ((strlen (domainname) + strlen (hostname) + OPSYS_LEN + 3)
  96. > MAXNETNAMELEN)
  97. return 0;
  98. sprintf (netname, "%s.%s@%s", OPSYS, hostname, domainname);
  99. return 1;
  100. }
  101. #ifdef EXPORT_RPC_SYMBOLS
  102. libc_hidden_def (host2netname)
  103. #else
  104. libc_hidden_nolink_sunrpc (host2netname, GLIBC_2_1)
  105. #endif
  106. int
  107. getnetname (char name[MAXNETNAMELEN + 1])
  108. {
  109. uid_t uid;
  110. int dummy;
  111. uid = __geteuid ();
  112. if (uid == 0)
  113. dummy = host2netname (name, NULL, NULL);
  114. else
  115. dummy = user2netname (name, uid, NULL);
  116. return (dummy);
  117. }
  118. libc_hidden_nolink_sunrpc (getnetname, GLIBC_2_1)
  119. /* Type of the lookup function for netname2user. */
  120. typedef int (*netname2user_function) (const char netname[MAXNETNAMELEN + 1],
  121. uid_t *, gid_t *, int *, gid_t *);
  122. int
  123. netname2user (const char netname[MAXNETNAMELEN + 1], uid_t * uidp, gid_t * gidp,
  124. int *gidlenp, gid_t * gidlist)
  125. {
  126. static service_user *startp;
  127. static netname2user_function start_fct;
  128. service_user *nip;
  129. union
  130. {
  131. netname2user_function f;
  132. void *ptr;
  133. } fct;
  134. enum nss_status status = NSS_STATUS_UNAVAIL;
  135. int no_more;
  136. if (startp == NULL)
  137. {
  138. no_more = __nss_publickey_lookup2 (&nip, "netname2user", NULL, &fct.ptr);
  139. if (no_more)
  140. startp = (service_user *) - 1;
  141. else
  142. {
  143. startp = nip;
  144. start_fct = fct.f;
  145. }
  146. }
  147. else
  148. {
  149. fct.f = start_fct;
  150. no_more = (nip = startp) == (service_user *) - 1;
  151. }
  152. while (!no_more)
  153. {
  154. status = (*fct.f) (netname, uidp, gidp, gidlenp, gidlist);
  155. no_more = __nss_next2 (&nip, "netname2user", NULL, &fct.ptr, status, 0);
  156. }
  157. return status == NSS_STATUS_SUCCESS;
  158. }
  159. #ifdef EXPORT_RPC_SYMBOLS
  160. libc_hidden_def (netname2user)
  161. #else
  162. libc_hidden_nolink_sunrpc (netname2user, GLIBC_2_1)
  163. #endif
  164. int
  165. netname2host (const char netname[MAXNETNAMELEN + 1], char *hostname,
  166. const int hostlen)
  167. {
  168. char *p1, *p2;
  169. p1 = strchr (netname, '.');
  170. if (p1 == NULL)
  171. return 0;
  172. p1++;
  173. p2 = strchr (p1, '@');
  174. if (p2 == NULL)
  175. return 0;
  176. *p2 = '\0';
  177. if (hostlen > MAXNETNAMELEN)
  178. return 0;
  179. strncpy (hostname, p1, hostlen);
  180. hostname[hostlen] = '\0';
  181. return 1;
  182. }
  183. libc_hidden_nolink_sunrpc (netname2host, GLIBC_2_1)