9b6e592062b2a27462307665c4c05328cb6e01cf
[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 int
33 afs_authenticate(char *userName, char *response, int *reenter, char **message)
34 {
35     char *reason, *pword, prompt[256];
36     struct passwd *pwd;
37     int code, unixauthneeded, password_expires = -1;
38
39     *reenter = 0;
40     *message = (char *)0;
41     if (response) {
42         pword = response;
43     } else {
44         sprintf(prompt, "Enter AFS password for %s: ", userName);
45         pword = getpass(prompt);
46         if (strlen(pword) == 0) {
47             printf
48                 ("Unable to read password because zero length passord is illegal\n");
49             *message = (char *)malloc(256);
50             sprintf(*message,
51                     "Unable to read password because zero length passord is illegal\n");
52             return AUTH_FAILURE;
53         }
54     }
55     if ((pwd = getpwnam(userName)) == NULL) {
56         *message = (char *)malloc(256);
57         sprintf(*message, "getpwnam for user failed\n");
58         return AUTH_FAILURE;
59     }
60     if (code =
61         ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG,
62                                    userName, (char *)0, (char *)0, pword, 0,
63                                    &password_expires, 0, &reason)) {
64         if (code == KANOENT)
65             return AUTH_NOTFOUND;
66         *message = (char *)malloc(1024);
67         sprintf(*message, "Unable to authenticate to AFS because %s.\n",
68                 reason);
69         return AUTH_FAILURE;
70     }
71 #if defined(AFS_KERBEROS_ENV)
72     setup_ticket_file(userName);
73 #endif
74     return AUTH_SUCCESS;
75 }
76
77 int
78 afs_chpass(char *userName, char *oldPasswd, char *newPasswd, char **message)
79 {
80     return AUTH_SUCCESS;
81 }
82
83 int
84 afs_passwdexpired(char *userName, char **message)
85 {
86     return AUTH_SUCCESS;
87 }
88
89 int
90 afs_passwdrestrictions(char *userName, char *newPasswd, char *oldPasswd,
91                        char **message)
92 {
93     return AUTH_SUCCESS;
94 }
95
96 int
97 afs_getgrset(char *userName)
98 {
99     return NULL;
100 }
101
102 struct group *
103 afs_getgrgid(int id)
104 {
105 #ifdef AFS_AIX51_ENV
106     static char name[64];
107     static char passwd[64];
108     static struct group grp;
109     struct group *g;
110     char *mem = NULL;
111
112     while ((g = getgrent()) != NULL) {
113         if (g->gr_gid == id) {
114             strncpy(&name, g->gr_name, sizeof(name));
115             strncpy(&passwd, g->gr_passwd, sizeof(passwd));
116             grp.gr_name = &name;
117             grp.gr_passwd = &passwd;
118             grp.gr_gid = g->gr_gid;
119             grp.gr_mem = &mem;
120             break;
121         }
122     }
123     endgrent();
124     if (g)
125         return &grp;
126 #endif
127     return NULL;
128 }
129
130 struct group *
131 afs_getgrnam(char *name)
132 {
133     return NULL;
134 }
135
136 #ifdef AFS_AIX51_ENV
137 struct passwd *
138 afs_getpwnam(char *user)
139 {
140     static char name[64];
141     static char passwd[64];
142     static char gecos[256];
143     static char dir[256];
144     static char shell[256];
145     static struct passwd pwd;
146     struct passwd *p;
147
148     while ((p = getpwent()) != NULL) {
149         if (!strcmp(p->pw_name, user)) {
150             strncpy(&name, p->pw_name, sizeof(name));
151             strncpy(&passwd, p->pw_passwd, sizeof(passwd));
152             strncpy(&gecos, p->pw_gecos, sizeof(gecos));
153             strncpy(&dir, p->pw_dir, sizeof(dir));
154             strncpy(&shell, p->pw_shell, sizeof(shell));
155             pwd.pw_name = &name;
156             pwd.pw_passwd = &passwd;
157             pwd.pw_uid = p->pw_uid;
158             pwd.pw_gid = p->pw_gid;
159             pwd.pw_gecos = &gecos;
160             pwd.pw_dir = &dir;
161             pwd.pw_shell = &shell;
162             break;
163         }
164     }
165     endpwent();
166     return &pwd;
167 }
168 #else
169 int
170 afs_getpwnam(int id)
171 {
172     return NULL;
173 }
174 #endif
175
176 #ifdef AFS_AIX52_ENV
177 struct passwd *
178 afs_getpwuid(uid_t uid)
179 {
180     return pwd;
181 }
182 #else
183 int
184 afs_getpwuid(char *name)
185 {
186     return NULL;
187 }
188 #endif
189
190 int
191 afs_initialize(struct secmethod_table *meths)
192 {
193     /*
194      * Initialize kauth package here so we don't have to call it
195      * each time we call the authenticate routine.      
196      */
197     ka_Init(0);
198     memset(meths, 0, sizeof(struct secmethod_table));
199     /*
200      * Initialize the exported interface routines. Except the authenticate one
201      * the others are currently mainly noops.
202      */
203     meths->method_chpass = afs_chpass;
204     meths->method_authenticate = afs_authenticate;
205     meths->method_passwdexpired = afs_passwdexpired;
206     meths->method_passwdrestrictions = afs_passwdrestrictions;
207     /*
208      * These we need to bring in because, for afs users, /etc/security/user's
209      * "registry" must non-local (i.e. DCE) since otherwise it assumes it's a
210      * local domain and uses valid_crypt(passwd) to validate the afs passwd
211      * which, of course, will fail. NULL return from these routine simply
212      * means use the local version ones after all.
213      */
214     meths->method_getgrgid = afs_getgrgid;
215     meths->method_getgrset = afs_getgrset;
216     meths->method_getgrnam = afs_getgrnam;
217     meths->method_getpwnam = afs_getpwnam;
218     meths->method_getpwuid = afs_getpwuid;
219     return (0);
220 }
221
222 #if defined(AFS_KERBEROS_ENV)
223
224 setup_ticket_file(userName)
225      char *userName;
226 {
227     extern char *ktc_tkt_string();
228     struct passwd *pwd;
229
230     setpwent();                 /* open the pwd database */
231     pwd = getpwnam(userName);
232     if (pwd) {
233         if (chown(ktc_tkt_string(), pwd->pw_uid, pwd->pw_gid) < 0)
234             perror("chown: ");
235     } else
236         perror("getpwnam : ");
237     endpwent();                 /* close the pwd database */
238 }
239 #endif /* AFS_KERBEROS_ENV */
240
241 #endif