radattr.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /***********************************************************************
  2. *
  3. * radattr.c
  4. *
  5. * A plugin which is stacked on top of radius.so. This plugin writes
  6. * all RADIUS attributes from the server's authentication confirmation
  7. * into /var/run/radattr.pppN. These attributes are available for
  8. * consumption by /etc/ppp/ip-{up,down} scripts.
  9. *
  10. * Copyright (C) 2002 Roaring Penguin Software Inc.
  11. *
  12. * This plugin may be distributed according to the terms of the GNU
  13. * General Public License, version 2 or (at your option) any later version.
  14. *
  15. ***********************************************************************/
  16. static char const RCSID[] =
  17. "$Id: radattr.c,v 1.2 2004/10/28 00:24:40 paulus Exp $";
  18. #include "pppd.h"
  19. #include "radiusclient.h"
  20. #include <stdio.h>
  21. extern void (*radius_attributes_hook)(VALUE_PAIR *);
  22. static void print_attributes(VALUE_PAIR *);
  23. static void cleanup(void *opaque, int arg);
  24. char pppd_version[] = VERSION;
  25. /**********************************************************************
  26. * %FUNCTION: plugin_init
  27. * %ARGUMENTS:
  28. * None
  29. * %RETURNS:
  30. * Nothing
  31. * %DESCRIPTION:
  32. * Initializes radattr plugin.
  33. ***********************************************************************/
  34. void
  35. plugin_init(void)
  36. {
  37. radius_attributes_hook = print_attributes;
  38. #if 0
  39. /* calling cleanup() on link down is problematic because print_attributes()
  40. is called only after PAP or CHAP authentication, but not when the link
  41. should go up again for any other reason */
  42. add_notifier(&link_down_notifier, cleanup, NULL);
  43. #endif
  44. /* Just in case... */
  45. add_notifier(&exitnotify, cleanup, NULL);
  46. info("RADATTR plugin initialized.");
  47. }
  48. /**********************************************************************
  49. * %FUNCTION: print_attributes
  50. * %ARGUMENTS:
  51. * vp -- linked-list of RADIUS attribute-value pairs
  52. * %RETURNS:
  53. * Nothing
  54. * %DESCRIPTION:
  55. * Prints the attribute pairs to /var/run/radattr.pppN. Each line of the
  56. * file contains "name value" pairs.
  57. ***********************************************************************/
  58. static void
  59. print_attributes(VALUE_PAIR *vp)
  60. {
  61. FILE *fp;
  62. char fname[512];
  63. char name[2048];
  64. char value[2048];
  65. int cnt = 0;
  66. slprintf(fname, sizeof(fname), "/var/run/radattr.%s", ifname);
  67. fp = fopen(fname, "w");
  68. if (!fp) {
  69. warn("radattr plugin: Could not open %s for writing: %m", fname);
  70. return;
  71. }
  72. for (; vp; vp=vp->next) {
  73. if (rc_avpair_tostr(vp, name, sizeof(name), value, sizeof(value)) < 0) {
  74. continue;
  75. }
  76. fprintf(fp, "%s %s\n", name, value);
  77. cnt++;
  78. }
  79. fclose(fp);
  80. dbglog("RADATTR plugin wrote %d line(s) to file %s.", cnt, fname);
  81. }
  82. /**********************************************************************
  83. * %FUNCTION: cleanup
  84. * %ARGUMENTS:
  85. * opaque -- not used
  86. * arg -- not used
  87. * %RETURNS:
  88. * Nothing
  89. * %DESCRIPTION:
  90. * Deletes /var/run/radattr.pppN
  91. ***********************************************************************/
  92. static void
  93. cleanup(void *opaque, int arg)
  94. {
  95. char fname[512];
  96. slprintf(fname, sizeof(fname), "/var/run/radattr.%s", ifname);
  97. (void) remove(fname);
  98. dbglog("RADATTR plugin removed file %s.", fname);
  99. }