package com.theorem.radius3.examples; import com.theorem.radius3.*; import com.theorem.radius3.dictionary.*; import java.io.*; /** * RADIUS Dictionary examples. *
* The dictionary class reads FreeRadius and earlier style dictionary files. * Dictonary values can be useful in creating standard and Vendor-Specific attributes. *
* The primary dictionary may include other dictionaries. Usually the main dictionary will * be the RADIUS dictionary of standard attributes like NAS-IP-Address and may include * vendor dictionaries as well. If is possible to just read a vendor dictionary by itself. */ public class DictionaryExamples { /** * FreeRadius dictionary implements Dictionary. */ FreeRadius d; /** * Dictonary file path. * This is the standard dictionary that may include other files. * This is standard Linux placement of the dictionaries but your path may be different. */ String dictFile = "/etc/raddb/dictionary"; /** * Example of reading a dictionary file. */ public DictionaryExamples() { d = new FreeRadius(dictFile); // Read the contents of the file. // The default is to also read any included files. try { d.read(); } catch (IOException ioe) { System.err.println("Error reading radius file: " + ioe.getMessage()); } // Show the files read. String fl[] = d.getFileList(); System.out.println("Files read:"); for (int i = 0; i < fl.length; i++) System.out.println("\t" + fl[i]); // Display the list of vendors read. String vn[] = d.getVendorNames(); System.out.println(); System.out.println("Known vendors:"); for (int i = 0; i < vn.length; i++) System.out.println("\t" + vn[i]); } /** * Create some attributes using the standard dictionary entries. */ public void createStandardAttributes() { AttributeList aList = new AttributeList(); int tag; int value; tag = d.getTag("Acct-Status-Type"); value = d.getIntValue(tag, "Accounting-On"); aList.addAttribute(tag, value); tag = d.getTag("Login-TCP-Port"); value = d.getIntValue(tag, "Telnet"); aList.addAttribute(tag, value); System.out.println("AttributeList:\n" + aList); // Display how often the last attribute may occur in an Access-Request. int type = d.getOccurrence(PacketType.Access_Request, tag); String typeName = d.getOccurrenceName(type); System.out.println("The attribute 'Login-TCP-Port' may occur " + typeName + " times in an Access-Request packet."); // Display how often the last attribute may occur in an Access-Request. type = d.getOccurrence(PacketType.Access_Accept, tag); typeName = d.getOccurrenceName(type); System.out.println("The attribute 'Login-TCP-Port' may occur " + typeName + " times in an Access-Accept packet."); } /** * Create some attributes using the standard dictionary entries. */ public void createVSAttributes() { int vendorId; int tag; int value; vendorId = d.getVendorId("Redback"); if (vendorId == RADIUSDictionary.UNKNOWN_VENDOR) System.out.println("Can't find the vendorId for RedBack"); VendorSpecific vs = new VendorSpecific(vendorId); tag = d.getTag(vendorId, "Context-Name"); vs.addAttribute(tag, "Some Context Name"); tag = d.getTag(vendorId, "PVC_Encapsulation_Type"); value = d.getIntValue(vendorId, tag, "AAA_ENCAPS_ATM_PPP_AUTO"); vs.addAttribute(tag, value); System.out.println("Redback VSA:\n" + vs); // Alternatively we can switch to the Redback vendor to shorten calls. // This is a repeat of the above code without the vendorId in each method. d.setVendorId(vendorId); tag = d.getTag("Context-Name"); vs.addAttribute(tag, "Some Context Name"); tag = d.getTag("PVC_Encapsulation_Type"); value = d.getIntValue(tag, "AAA_ENCAPS_ATM_PPP_AUTO"); vs.addAttribute(tag, value); System.out.println("Redback VSA:\n" + vs); } /** * Display some Vendor information. */ public void displayVendorAttributes() { // Create a VSA as in the example createVSAttributes(). int vendorId; int tag; int value; Attribute a; vendorId = d.getVendorId("Redback"); VendorSpecific vs = new VendorSpecific(vendorId); tag = d.getTag(vendorId, "Context-Name"); vs.addAttribute(tag, "Some Context Name"); tag = d.getTag(vendorId, "PVC_Encapsulation_Type"); value = d.getIntValue(vendorId, "AAA_ENCAPS_ATM_PPP_AUTO"); vs.addAttribute(tag, value); // We could also create the VSA attribute above this way: a = d.createAttribute(vendorId, "PVC_Encapsulation_Type", "AAA_ENCAPS_ATM_PPP_AUTO"); System.out.println("Redback VSA:\n" + vs); // Now decode the VSA: java.util.Enumeration e = vs.elements(); while (e.hasMoreElements()) { String sValue = null; a = (Attribute) e.nextElement(); tag = a.getTag(); // Get the attibute's name from it's tag value. String attrName = d.getName(vendorId, tag); // If the attribute is an integer see if it has a known value. if (d.getDataType(vendorId, tag) == Dictionary.INTEGER) { if (d.hasValue(vendorId, tag)) { value = a.getInt(); sValue = d.getValueName(vendorId, tag, value); } } if (sValue != null) System.out.println(attrName + ": " + sValue); else switch (d.getDataType(vendorId, tag)) { case Dictionary.INTEGER: System.out.println(attrName + " contains an Integer"); break; case Dictionary.IPADDRESS : System.out.println(attrName + " contains an IP Address"); break; case Dictionary.STRING: System.out.println(attrName + " contains a string value"); break; default: System.out.println(attrName + " contains something else."); } } } public static void main(String a[]) { DictionaryExamples de = new DictionaryExamples(); System.out.println("----------------------------\n\n"); de.createStandardAttributes(); System.out.println("----------------------------\n\n"); de.createVSAttributes(); System.out.println("----------------------------\n\n"); de.displayVendorAttributes(); System.out.println("----------------------------\n\n"); } }