Back to index

im-sdk  12.3.91
IMAuth.hh
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 
00043 #ifndef IMAUTH_HH_
00044 #define IMAUTH_HH_
00045 
00046 #include <stdlib.h>
00047 #include <string.h>
00048 
00049 class symbol_mapper
00050 {
00051   private:
00052     struct sym {
00053        char *symname;
00054        char *value;
00055     };
00056     int alloced_size;
00057     int size;
00058     sym *psym;
00059     void expand_psym_slot(
00060        int aSize
00061     )
00062     { 
00063        psym = (sym*) realloc(psym, sizeof(sym) * aSize);
00064        alloced_size = aSize;
00065     }
00066   
00067 public:
00068     void set_symbol_value(
00069        const char *sn,
00070        const char *val
00071     )
00072     {
00073        if (size >= alloced_size)
00074            expand_psym_slot(size * 2);
00075        psym[size].symname = strdup(sn);
00076        psym[size].value = strdup(val);
00077        size++;
00078     }
00079     char *get_symbol_value(const char *sn)
00080     {
00081        int i;
00082        for (i = 0;i < size;i++)
00083            if (!strcmp(sn, psym[i].symname))
00084               return psym[i].value;
00085        return NULL;
00086     }
00087     symbol_mapper()
00088     {
00089        size = 0;
00090        alloced_size = 10;
00091        psym = (sym*) malloc(sizeof(sym) * alloced_size);
00092     }
00093     ~symbol_mapper()
00094     {
00095        int i;
00096        for (i = 0;i < size;i++) {
00097            if (psym[i].symname) free(psym[i].symname);
00098            if (psym[i].value) free(psym[i].value);
00099        }
00100        free(psym);
00101     }
00102 };
00103 
00104 class IMAuth
00105 {
00106   public:
00107     enum pattern_type {
00108            HOSTNAME,
00109            ADDR,
00110            ADDR6
00111     };
00112     enum access_type {
00113            PERMIT,
00114            DENY,
00115            CHECKUSER,
00116            PASSWORD,
00117            NO_ENTRY,
00118            UNKNOWN
00119     };
00120     enum auth_type {
00121            UNDEFINED,
00122            TCP,
00123            UNIX
00124     };
00125 private:
00126     struct auth_entry {
00127        pattern_type pt;
00128        access_type at;
00129        char *host_pattern;
00130        int *addr_pattern;
00131        int bitlen;
00132     };
00133 
00134   int ae_num;
00135   int alloced_ae_num;
00136   int auth_type;
00137   auth_entry *pae;
00138   access_type def_at;
00139   symbol_mapper sm;
00140 
00141     int adjust_pae_slot_size(
00142        int newsize
00143     );
00144 
00145     int *from_address;
00146     int from_address_bitlen;
00147     char *from_hostname;
00148 
00149     char *command_name;
00150     char *domainname;
00151     int set_hostinfo();
00152 
00153     pattern_type check_pattern_type(
00154        const char *pat
00155     );
00156 
00157     int match_hostname_entry(
00158        const char *pat,
00159        const char *hostname
00160     );
00161     char *normalize_hostname_pattern(
00162        const char *pat
00163     );
00164     int addr_element_to_number(
00165        const char **addr
00166     );
00167     int store_addr_pattern(
00168        const char *pattern,
00169        int *addr_pattern
00170     );
00171     int match_addr46_entry(
00172        const int *pat,
00173        int patbitlen,
00174        const int *addr,
00175        int addrbitlen
00176     );
00177     int addr6_element_to_number(
00178        const char **addr
00179     );
00180     int store_addr6_pattern(
00181        const char *pattern,
00182        int *addr_pattern
00183     );
00184     int match_entry(auth_entry *p);
00185 
00186     access_type get_access_type(int fd);
00187 
00188   protected:
00189     virtual int set_fd_from(int fd);
00190     virtual int check_password(
00191        int fd,
00192        const char *user,
00193        const char *password
00194     ) { return 0; }
00195     char *get_command_name()
00196     { return command_name; }
00197     char *get_from_hostname()
00198     { return from_hostname; }
00199     int get_auth_type()
00200     { return auth_type; }
00201 
00202 public:
00203     int set_entry(
00204        const char *pattern,
00205        access_type at
00206     );
00207     int set_default_entry(
00208        access_type at
00209     );
00210     void set_command_name(
00211        const char *cmdname
00212     );
00213     int clear_all_entries();
00214     int fd_ok(
00215        int fd
00216     );
00217     int auth_fd(
00218        int fd,
00219        const char *user = NULL,
00220        const char *password = NULL
00221     );
00222 
00223     IMAuth();
00224     virtual ~IMAuth();
00225 };
00226 
00227 #endif /* IMAUTH_HH_ */
00228 
00229 /* Local Variables: */
00230 /* c-file-style: "iiim-project" */
00231 /* End: */