home *** CD-ROM | disk | FTP | other *** search
/ ftp.shrubbery.net / 2015-02-07.ftp.shrubbery.net.tar / ftp.shrubbery.net / pub / tac_plus / tacacs+-F4.0.4.27a.tar.gz / tacacs+-F4.0.4.27a.tar / tacacs+-F4.0.4.27a / pw.c < prev    next >
C/C++ Source or Header  |  2012-01-23  |  3KB  |  142 lines

  1. /*
  2.  * $Id: pw.c,v 1.7 2009-03-18 17:48:59 heas Exp $
  3.  *
  4.  * Copyright (c) 1995-1998 by Cisco systems, Inc.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software for
  7.  * any purpose and without fee is hereby granted, provided that this
  8.  * copyright and permission notice appear on all copies of the
  9.  * software and supporting documentation, the name of Cisco Systems,
  10.  * Inc. not be used in advertising or publicity pertaining to
  11.  * distribution of the program without specific prior permission, and
  12.  * notice be given in supporting documentation that modification,
  13.  * copying and distribution is by permission of Cisco Systems, Inc.
  14.  *
  15.  * Cisco Systems, Inc. makes no representations about the suitability
  16.  * of this software for any purpose.  THIS SOFTWARE IS PROVIDED ``AS
  17.  * IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  18.  * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  19.  * FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. /*
  23.  * Tacacs+ password lookup routine for those systems which don't have
  24.  * setpwfile.  Not for use on /etc/passwd files.
  25.  */
  26.  
  27. #include "tac_plus.h"
  28. #include <pwd.h>
  29. #include <string.h>
  30.  
  31. static struct passwd pw_passwd;
  32.  
  33. struct passwd *
  34. tac_passwd_lookup(char *name, char *file)
  35. {
  36.     FILE *passwd_fp = NULL;
  37.  
  38.     static char uname[512];
  39.     static char password[1024];
  40.     static char gecos[1024];
  41.     static char homedir[1024];
  42.     static char shell[1024];
  43.     char buf[1024];
  44.     char *s, *e;
  45.  
  46.     passwd_fp = fopen(file, "r");
  47.  
  48.     if (passwd_fp) {
  49.     if (debug & DEBUG_PASSWD_FLAG)
  50.         report(LOG_DEBUG, "tac_passwd_lookup: open %s %d",
  51.            file, fileno(passwd_fp));
  52.     } else {
  53.     report(LOG_ERR, "tac_passwd_lookup: cannot open file %s for reading",
  54.            file);
  55.     return(NULL);
  56.     }
  57.  
  58.     while (fgets(buf, sizeof(buf), passwd_fp)) {
  59.  
  60.     /* uname, password, uid, gid, gecos, homedir, shell */
  61.  
  62.     s = buf;
  63.     e = strchr(buf, ':');
  64.     if (!e)
  65.         break;
  66.  
  67.     strncpy(uname, s, e - s);
  68.     uname[e - s] = '\0';
  69.  
  70.     /* try next entry */
  71.     if (strcmp(uname, name))
  72.         continue;
  73.  
  74.     s = e + 1;
  75.     e = strchr(s, ':');
  76.     if (!e) {
  77.         break;
  78.     }
  79.     strncpy(password, s, e - s);
  80.     password[e - s] = '\0';
  81.  
  82.     s = e + 1;
  83.     e = strchr(s, ':');
  84.     if (!e) {
  85.         break;
  86.     }
  87.     pw_passwd.pw_uid = atoi(s);
  88.  
  89.     s = e + 1;
  90.     e = strchr(s, ':');
  91.     pw_passwd.pw_gid = atoi(s);
  92.  
  93.     s = e + 1;
  94.     e = strchr(s, ':');
  95.     if (!e) {
  96.         break;
  97.     }
  98.     strncpy(gecos, s, e - s);
  99.     gecos[e - s] = '\0';
  100.  
  101.     s = e + 1;
  102.     e = strchr(s, ':');
  103.     if (!e) {
  104.         break;
  105.     }
  106.     strncpy(homedir, s, e - s);
  107.     homedir[e - s] = '\0';
  108.  
  109.     s = e + 1;
  110.     e = strchr(s, '\n');
  111.     if (!e) {
  112.         break;
  113.     }
  114.     strncpy(shell, s, e - s);
  115.     shell[e - s] = '\0';
  116.  
  117.     pw_passwd.pw_name    = uname;
  118.     pw_passwd.pw_passwd  = password;
  119. #ifndef NO_PWAGE
  120.     pw_passwd.pw_age     = NULL;
  121.     pw_passwd.pw_comment = NULL;
  122. #endif /* NO_PWAGE */
  123.     pw_passwd.pw_gecos   = gecos;
  124.     pw_passwd.pw_dir     = homedir;
  125.     pw_passwd.pw_shell   = shell;
  126.  
  127.     if (debug & DEBUG_PASSWD_FLAG)
  128.         report(LOG_DEBUG, "tac_passwd_lookup: close %s %d",
  129.            file, fileno(passwd_fp));
  130.     fclose(passwd_fp);
  131.     return(&pw_passwd);
  132.     }
  133.  
  134.     /* no match found */
  135.     if (debug & DEBUG_PASSWD_FLAG)
  136.         report(LOG_DEBUG, "tac_passwd_lookup: close %s %d",
  137.            file, fileno(passwd_fp));
  138.     fclose(passwd_fp);
  139.  
  140.     return(NULL);
  141. }
  142.