b9fa36e5a395def82360c156f026382d9220ca18
[openafs.git] / src / tsm41 / aix41_auth.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #if defined(AFS_AIX41_ENV)
17 #include <sys/types.h>
18 #include <sys/param.h>
19 #include <stdio.h>
20 #include <locale.h>
21 #include <nl_types.h>
22 #include <pwd.h>
23 #include <netdb.h>
24 #include <sys/socket.h>
25 #include <sys/file.h>
26 #include <errno.h>
27 #include <usersec.h>
28
29 #include <afs/kauth.h>
30 #include <afs/kautils.h>
31
32 struct passwd *afs_getpwnam_int(char *, int);
33
34 int
35 afs_authenticate(char *userName, char *response, int *reenter, char **message)
36 {
37     char *reason, *pword, prompt[256];
38     struct passwd *pwd;
39     int code, unixauthneeded, password_expires = -1;
40
41     *reenter = 0;
42     *message = (char *)0;
43     if (response) {
44         pword = response;
45     } else {
46         sprintf(prompt, "Enter AFS password for %s: ", userName);
47         pword = getpass(prompt);
48         if (strlen(pword) == 0) {
49             printf
50                 ("Unable to read password because zero length passord is illegal\n");
51             *message = (char *)malloc(256);
52             sprintf(*message,
53                     "Unable to read password because zero length passord is illegal\n");
54             return AUTH_FAILURE;
55         }
56     }
57 #ifdef AFS_AIX51_ENV
58     if ((pwd = afs_getpwnam_int(userName, 1)) == NULL) 
59 #else
60     if ((pwd = getpwnam(userName)) == NULL) 
61 #endif
62       {
63         *message = (char *)malloc(256);
64         sprintf(*message, "getpwnam for user failed\n");
65         return AUTH_FAILURE;
66     }
67     if (code =
68         ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG,
69                                    userName, (char *)0, (char *)0, pword, 0,
70                                    &password_expires, 0, &reason)) {
71         if (code == KANOENT)
72             return AUTH_NOTFOUND;
73         *message = (char *)malloc(1024);
74         sprintf(*message, "Unable to authenticate to AFS because %s.\n",
75                 reason);
76         return AUTH_FAILURE;
77     }
78 #if defined(AFS_KERBEROS_ENV)
79     setup_ticket_file(userName);
80 #endif
81     return AUTH_SUCCESS;
82 }
83
84 int
85 afs_chpass(char *userName, char *oldPasswd, char *newPasswd, char **message)
86 {
87     return AUTH_SUCCESS;
88 }
89
90 int
91 afs_passwdexpired(char *userName, char **message)
92 {
93     return AUTH_SUCCESS;
94 }
95
96 int
97 afs_passwdrestrictions(char *userName, char *newPasswd, char *oldPasswd,
98                        char **message)
99 {
100     return AUTH_SUCCESS;
101 }
102
103 int
104 afs_getgrset(char *userName)
105 {
106     return NULL;
107 }
108
109 struct group *
110 afs_getgrgid(int id)
111 {
112 #ifdef AFS_AIX51_ENV
113     static char name[64];
114     static char passwd[64];
115     static struct group grp;
116     struct group *g;
117     char *mem = NULL;
118
119     while ((g = getgrent()) != NULL) {
120         if (g->gr_gid == id) {
121             strncpy(&name, g->gr_name, sizeof(name));
122             strncpy(&passwd, g->gr_passwd, sizeof(passwd));
123             grp.gr_name = &name;
124             grp.gr_passwd = &passwd;
125             grp.gr_gid = g->gr_gid;
126             grp.gr_mem = &mem;
127             break;
128         }
129     }
130     endgrent();
131     if (g)
132         return &grp;
133 #endif
134     return NULL;
135 }
136
137 struct group *
138 afs_getgrnam(char *name)
139 {
140     return NULL;
141 }
142
143 #ifdef AFS_AIX51_ENV
144 struct passwd *
145 afs_getpwnam(char *user)
146 {
147     return (struct passwd *) afs_getpwnam_int(user, 0);
148 }
149
150 struct passwd *
151 afs_getpwnam_int(char *user, int ignore)
152 {
153     static char name[64];
154     static char passwd[64];
155     static char gecos[256];
156     static char dir[256];
157     static char shell[256];
158     static struct passwd pwd;
159     struct passwd *p;
160
161     pwd.pw_uid = 4294967294;
162     pwd.pw_gid = 4294967294;
163     strcpy((char *)&shell, "/bin/false");
164     if (!user)
165        return &pwd;
166
167     while ((p = getpwent()) != NULL) {
168         if (!strcmp(p->pw_name, user)) {
169             strncpy(&name, p->pw_name, sizeof(name));
170             strncpy(&passwd, p->pw_passwd, sizeof(passwd));
171             strncpy(&gecos, p->pw_gecos, sizeof(gecos));
172             strncpy(&dir, p->pw_dir, sizeof(dir));
173             strncpy(&shell, p->pw_shell, sizeof(shell));
174             pwd.pw_name = &name;
175             pwd.pw_passwd = &passwd;
176             pwd.pw_uid = p->pw_uid;
177             pwd.pw_gid = p->pw_gid;
178             pwd.pw_gecos = &gecos;
179             pwd.pw_dir = &dir;
180             pwd.pw_shell = &shell;
181             break;
182         }
183     }
184     endpwent();
185     if (ignore && (p == NULL))
186        return NULL;
187     return &pwd;
188 }
189 #else
190 int
191 afs_getpwnam(int id)
192 {
193     return NULL;
194 }
195 #endif
196
197 int
198 afs_getpwuid(char *name)
199 {
200     return NULL;
201 }
202
203 int
204 afs_initialize(struct secmethod_table *meths)
205 {
206     /*
207      * Initialize kauth package here so we don't have to call it
208      * each time we call the authenticate routine.      
209      */
210     ka_Init(0);
211     memset(meths, 0, sizeof(struct secmethod_table));
212     /*
213      * Initialize the exported interface routines. Except the authenticate one
214      * the others are currently mainly noops.
215      */
216     meths->method_chpass = afs_chpass;
217     meths->method_authenticate = afs_authenticate;
218     meths->method_passwdexpired = afs_passwdexpired;
219     meths->method_passwdrestrictions = afs_passwdrestrictions;
220     /*
221      * These we need to bring in because, for afs users, /etc/security/user's
222      * "registry" must non-local (i.e. DCE) since otherwise it assumes it's a
223      * local domain and uses valid_crypt(passwd) to validate the afs passwd
224      * which, of course, will fail. NULL return from these routine simply
225      * means use the local version ones after all.
226      */
227     meths->method_getgrgid = afs_getgrgid;
228     meths->method_getgrset = afs_getgrset;
229     meths->method_getgrnam = afs_getgrnam;
230     meths->method_getpwnam = afs_getpwnam;
231     meths->method_getpwuid = afs_getpwuid;
232     return (0);
233 }
234
235 #if defined(AFS_KERBEROS_ENV)
236
237 setup_ticket_file(userName)
238      char *userName;
239 {
240     extern char *ktc_tkt_string();
241     struct passwd *pwd;
242
243     setpwent();                 /* open the pwd database */
244     pwd = getpwnam(userName);
245     if (pwd) {
246         if (chown(ktc_tkt_string(), pwd->pw_uid, pwd->pw_gid) < 0)
247             perror("chown: ");
248     } else
249         perror("getpwnam : ");
250     endpwent();                 /* close the pwd database */
251 }
252 #endif /* AFS_KERBEROS_ENV */
253
254 #endif