"referenced entity .* not found" message fixed to be fatal
[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
24 #include "tac_plus.h"
25
26 #include <stdlib.h>
27 #include <pwd.h>
28 #include <string.h>
29
30 #include "pw.h"
31 #include "report.h"
32 #include "main.h"
33
34
35 static struct passwd pw_passwd;
36
37 struct passwd *tac_passwd_lookup TAC_ARGS((const char *name, const char *file));
38
39 struct passwd *
40 tac_passwd_lookup(name, file)
41 const char *name;
42 const char *file;
43 {
44     FILE *passwd_fp = NULL;
45
46     static char uname[512];
47     static char password[1024];
48     static char gecos[1024];
49     static char homedir[1024];
50     static char shell[1024];
51     char buf[1024];
52     char *s, *e;
53
54     passwd_fp = fopen(file, "r");
55
56     if (passwd_fp) {
57         if (debug & DEBUG_PASSWD_FLAG)
58             report(LOG_DEBUG, "tac_passwd_lookup: open %s %d",
59                    file, fileno(passwd_fp));
60     } else {
61         report(LOG_ERR, "tac_passwd_lookup: cannot open file %s for reading",
62                file);
63         return(NULL);
64     }
65
66     while (fgets(buf, sizeof(buf), passwd_fp)) {
67
68         /* uname, password, uid, gid, gecos, homedir, shell */
69
70         s = buf;
71         e = index(buf, ':');
72         if (!e)
73             break;
74
75         strncpy(uname, s, e - s);
76         uname[e - s] = '\0';
77
78         /* try next entry */
79         if (strcmp(uname, name))
80             continue;
81
82         s = e + 1;
83         e = index(s, ':');
84         if (!e) {
85             break;
86         }
87         strncpy(password, s, e - s);
88         password[e - s] = '\0';
89
90         s = e + 1;
91         e = index(s, ':');
92         if (!e) {
93             break;
94         }
95         pw_passwd.pw_uid = atoi(s);
96
97         s = e + 1;
98         e = index(s, ':');
99         pw_passwd.pw_gid = atoi(s);
100
101         s = e + 1;
102         e = index(s, ':');
103         if (!e) {
104             break;
105         }
106         strncpy(gecos, s, e - s);
107         gecos[e - s] = '\0';
108
109         s = e + 1;
110         e = index(s, ':');
111         if (!e) {
112             break;
113         }
114         strncpy(homedir, s, e - s);
115         homedir[e - s] = '\0';
116
117         s = e + 1;
118         e = index(s, '\n');
119         if (!e) {
120             break;
121         }
122         strncpy(shell, s, e - s);
123         shell[e - s] = '\0';
124
125         pw_passwd.pw_name    = uname;
126         pw_passwd.pw_passwd  = password;
127 #ifdef HAVE_PASSWD_PW_AGE
128         pw_passwd.pw_age     = NULL;
129 #endif
130 #ifdef HAVE_PASSWD_PW_COMMENT
131         pw_passwd.pw_comment = NULL;
132 #endif
133         pw_passwd.pw_gecos   = gecos;
134         pw_passwd.pw_dir     = homedir;
135         pw_passwd.pw_shell   = shell;
136
137         if (debug & DEBUG_PASSWD_FLAG)
138             report(LOG_DEBUG, "tac_passwd_lookup: close %s %d",
139                    file, fileno(passwd_fp));
140         fclose(passwd_fp);
141         return(&pw_passwd);
142     }
143
144     /* no match found */
145     if (debug & DEBUG_PASSWD_FLAG)
146             report(LOG_DEBUG, "tac_passwd_lookup: close %s %d",
147                    file, fileno(passwd_fp));
148     fclose(passwd_fp);
149     return(NULL);
150 }