AuGetAddr.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Copyright 1988, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #include <X11/Xauth.h>
  24. #include <X11/Xos.h>
  25. #define binaryEqual(a, b, len) (memcmp(a, b, len) == 0)
  26. Xauth *
  27. XauGetAuthByAddr (
  28. #if NeedWidePrototypes
  29. unsigned int family,
  30. unsigned int address_length,
  31. #else
  32. unsigned short family,
  33. unsigned short address_length,
  34. #endif
  35. _Xconst char* address,
  36. #if NeedWidePrototypes
  37. unsigned int number_length,
  38. #else
  39. unsigned short number_length,
  40. #endif
  41. _Xconst char* number,
  42. #if NeedWidePrototypes
  43. unsigned int name_length,
  44. #else
  45. unsigned short name_length,
  46. #endif
  47. _Xconst char* name)
  48. {
  49. FILE *auth_file;
  50. char *auth_name;
  51. Xauth *entry;
  52. auth_name = XauFileName ();
  53. if (!auth_name)
  54. return NULL;
  55. if (access (auth_name, R_OK) != 0) /* checks REAL id */
  56. return NULL;
  57. auth_file = fopen (auth_name, "rb");
  58. if (!auth_file)
  59. return NULL;
  60. for (;;) {
  61. entry = XauReadAuth (auth_file);
  62. if (!entry)
  63. break;
  64. /*
  65. * Match when:
  66. * either family or entry->family are FamilyWild or
  67. * family and entry->family are the same and
  68. * address and entry->address are the same
  69. * and
  70. * either number or entry->number are empty or
  71. * number and entry->number are the same
  72. * and
  73. * either name or entry->name are empty or
  74. * name and entry->name are the same
  75. */
  76. if ((family == FamilyWild || entry->family == FamilyWild ||
  77. (entry->family == family &&
  78. address_length == entry->address_length &&
  79. binaryEqual (entry->address, address, address_length))) &&
  80. (number_length == 0 || entry->number_length == 0 ||
  81. (number_length == entry->number_length &&
  82. binaryEqual (entry->number, number, number_length))) &&
  83. (name_length == 0 || entry->name_length == 0 ||
  84. (entry->name_length == name_length &&
  85. binaryEqual (entry->name, name, name_length))))
  86. break;
  87. XauDisposeAuth (entry);
  88. }
  89. (void) fclose (auth_file);
  90. return entry;
  91. }