Back to index

im-sdk  12.3.91
IMUserMgr.cpp
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 #ifdef HAVE_CONFIG_H
00044 #include <config.h>
00045 #endif
00046 #include <stdlib.h>
00047 #include <string.h>
00048 
00049 #ifdef HAVE_CRYPT
00050 #ifdef HAVE_CRYPT_H
00051 #include <crypt.h>
00052 #else
00053 #include <unistd.h>
00054 #endif
00055 #endif
00056 
00057 #ifdef HAVE_PWENT
00058 #include <sys/types.h>
00059 #include <pwd.h>
00060 #endif
00061 
00062 #include "iwrap.h"
00063 #include "IMUserMgr.hh"
00064 
00065 int
00066 IMUserMgr::
00067 adjust_pue_slot_size(int num)
00068 {
00069   alloced_ue_num = num;
00070   pue = (userentry*) realloc(pue, sizeof(userentry) * alloced_ue_num);
00071   if (!pue) return 0;
00072   return 1;
00073 }
00074 
00075 int
00076 IMUserMgr::
00077 check_password_with_crypt(
00078     const char *pwd,
00079     const char *enc_pwd
00080 )
00081 {
00082   char *p;
00083 #ifdef HAVE_CRYPT
00084   if (!(p = crypt(pwd, enc_pwd)))
00085     return 0;
00086   if (!strcmp(p, enc_pwd))
00087     return 1;
00088   return 0;
00089 #else
00090   return 0;
00091 #endif
00092 }
00093 
00094 
00095 IMAuth::access_type
00096 IMUserMgr::
00097 get_internal_userentry(
00098     const char *user,
00099     const char *password
00100 )
00101 {
00102   int i;
00103   userentry *pe;
00104 
00105   pe = pue;
00106   for (i = 0;i < ue_num;i++, pe++)
00107     {
00108       if (strcmp(pe->username, user) != 0) continue;
00109       if (pe->at == IMAuth::PASSWORD)
00110        {
00111          if (password
00112              && pe->password
00113              && (check_password_with_crypt(password, pe->password)))
00114            return IMAuth::PERMIT;
00115          return IMAuth::DENY;
00116        }
00117       else
00118        return pe->at;
00119     }
00120   return IMAuth::UNKNOWN;
00121 }
00122 
00123 int
00124 IMUserMgr::
00125 check_password(
00126     int fd,
00127     const char *user,
00128     const char *password
00129 )
00130 {
00131   IMAuth::access_type at;
00132 
00133   at = get_internal_userentry(user, password);
00134 
00135 #ifdef HAVE_UNIX_SOCKET
00136   // when a user connects with unix domain,
00137   // try to authenicate with unix credential.
00138   int auth_type = get_auth_type();
00139   if (auth_type == IMAuth::UNIX) {
00140     // in the case of user doesn't specify a configuration file
00141     // we use at instead of sysuser_at.
00142     if (at != IMAuth::DENY) {
00143       int ret = authenticate_with_unix(fd, user, NULL,
00144                                   get_command_name(),
00145                                   get_from_hostname());
00146       if (ret >= 0) {
00147        return ret;
00148       }
00149       // if this system doesn't support it,
00150       // fall through to the normal operation.
00151     } else {
00152         return 0;
00153     }
00154   }
00155 #endif
00156   if (at == IMAuth::PERMIT) return 1;
00157   else if (at == IMAuth::DENY) return 0;
00158 #ifdef HAVE_PAM
00159   if (sysuser_at == IMAuth::PERMIT)
00160     return authenticate_with_pam(user, NULL,
00161                              get_command_name(),
00162                              get_from_hostname());
00163   else if (password && (sysuser_at == IMAuth::PASSWORD))
00164     return authenticate_with_pam(user, password,
00165                              get_command_name(),
00166                              get_from_hostname());
00167   else
00168     return 0;
00169 #else
00170 #ifdef HAVE_PWENT
00171   {
00172     struct passwd *pwd;
00173 #ifdef HAVE_PWNAM_R
00174 #ifndef NSS_BUFLEN_PASSWD
00175 #define NSS_BUFLEN_PASSWD 1024
00176 #endif
00177     struct passwd pwd_buf;
00178     char buf[NSS_BUFLEN_PASSWD];
00179 
00180     if ((getpwnam_r(user, &pwd_buf, buf, sizeof(buf), &pwd)) != 0) return 0;
00181 #else
00182     if (!(pwd = getpwnam(user))) return 0;
00183 #endif
00184     if (sysuser_at == IMAuth::PERMIT) return 1;
00185     if (sysuser_at == IMAuth::PASSWORD)
00186       {
00187        if (password
00188            &&(check_password_with_crypt(password, pwd->pw_passwd)))
00189          return 1;
00190        return 0;
00191       }
00192   }
00193 #endif
00194 #endif
00195   return 0;
00196 }
00197 
00198 int
00199 IMUserMgr::
00200 add_user(
00201     const char *user,
00202     const char *password,
00203     access_type at
00204 )
00205 {
00206   char *u1, *p1;
00207 
00208   if (alloced_ue_num <= ue_num)
00209     {
00210       if (!adjust_pue_slot_size((alloced_ue_num + 1) * 2))
00211        return 0;
00212     }
00213 
00214   u1= strdup(user);
00215   if (!u1) return 0;
00216   if (password)
00217     {
00218       p1 = strdup(password);
00219       if (!p1)
00220        {
00221          free(u1);
00222          return 0;
00223        }
00224     }
00225   else
00226     p1 = NULL;
00227   pue[ue_num].username = u1;
00228   pue[ue_num].password = p1;
00229   pue[ue_num].at = at;
00230   ue_num++;
00231 
00232   return 1;
00233 }
00234 
00235 IMUser*
00236 IMUserMgr::
00237 create_imuser(
00238     int fd,
00239     const char *user,
00240     const char *password
00241 )
00242 {
00243     IMUser *puser;
00244     IMUserMap::iterator it;
00245 
00246     if (!auth_fd(fd, user, password)) return NULL;
00247     u16string ustr = user;
00248     
00249     it = usermap.find(ustr);
00250     if (it != usermap.end()) return it->second;
00251     puser = new IMUser(u16string(user));
00252 
00253     pair<IMUserMap::iterator, bool> r =  usermap.insert(IMUserMap::value_type(ustr, puser));
00254     if (!r.second) return NULL;
00255 
00256     return puser;
00257 }
00258 
00259 
00260 IMUserMgr::
00261 IMUserMgr()
00262   : IMAuth()
00263 {
00264   pue = NULL;
00265   ue_num = alloced_ue_num = 0;
00266   sysuser_at = IMAuth::DENY;
00267 }
00268 
00269 IMUserMgr::
00270 ~IMUserMgr()
00271 {
00272   int i;
00273   for (i = 0;i < ue_num;i++)
00274     {
00275       if (pue[i].username) free(pue[i].username);
00276       if (pue[i].password) free(pue[i].password);
00277     }
00278   if (pue) free(pue);
00279 
00280   delete_all(usermap);
00281 }
00282 
00283 /* Local Variables: */
00284 /* c-file-style: "iiim-project" */
00285 /* End: */