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