Initial import
[samba] / source / auth / auth_rhosts.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Main SMB reply routines
4    Copyright (C) Andrew Tridgell 1992-1998
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_AUTH
25
26 /****************************************************************************
27  Create a SAM_ACCOUNT - either by looking in the pdb, or by faking it up from
28  unix info.
29 ****************************************************************************/
30
31 static NTSTATUS auth_get_sam_account(const char *user, SAM_ACCOUNT **account) 
32 {
33         BOOL pdb_ret;
34         NTSTATUS nt_status;
35         if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(account))) {
36                 return nt_status;
37         }
38         
39         become_root();
40         pdb_ret = pdb_getsampwnam(*account, user);
41         unbecome_root();
42
43         if (!pdb_ret) {
44                 
45                 struct passwd *pass = Get_Pwnam(user);
46                 if (!pass) 
47                         return NT_STATUS_NO_SUCH_USER;
48
49                 if (!NT_STATUS_IS_OK(nt_status = pdb_fill_sam_pw(*account, pass))) {
50                         return nt_status;
51                 }
52         }
53         return NT_STATUS_OK;
54 }
55
56 /****************************************************************************
57  Read the a hosts.equiv or .rhosts file and check if it
58  allows this user from this machine.
59 ****************************************************************************/
60
61 static BOOL check_user_equiv(const char *user, const char *remote, const char *equiv_file)
62 {
63   int plus_allowed = 1;
64   char *file_host;
65   char *file_user;
66   char **lines = file_lines_load(equiv_file, NULL);
67   int i;
68
69   DEBUG(5, ("check_user_equiv %s %s %s\n", user, remote, equiv_file));
70   if (! lines) return False;
71   for (i=0; lines[i]; i++) {
72     char *buf = lines[i];
73     trim_char(buf,' ',' ');
74
75     if (buf[0] != '#' && buf[0] != '\n') 
76     {
77       BOOL is_group = False;
78       int plus = 1;
79       char *bp = buf;
80       if (strcmp(buf, "NO_PLUS\n") == 0)
81       {
82         DEBUG(6, ("check_user_equiv NO_PLUS\n"));
83         plus_allowed = 0;
84       }
85       else {
86         if (buf[0] == '+') 
87         {
88           bp++;
89           if (*bp == '\n' && plus_allowed) 
90           {
91             /* a bare plus means everbody allowed */
92             DEBUG(6, ("check_user_equiv everybody allowed\n"));
93             file_lines_free(lines);
94             return True;
95           }
96         }
97         else if (buf[0] == '-')
98         {
99           bp++;
100           plus = 0;
101         }
102         if (*bp == '@') 
103         {
104           is_group = True;
105           bp++;
106         }
107         file_host = strtok(bp, " \t\n");
108         file_user = strtok(NULL, " \t\n");
109         DEBUG(7, ("check_user_equiv %s %s\n", file_host ? file_host : "(null)", 
110                  file_user ? file_user : "(null)" ));
111         if (file_host && *file_host) 
112         {
113           BOOL host_ok = False;
114
115 #if defined(HAVE_NETGROUP) && defined(HAVE_YP_GET_DEFAULT_DOMAIN)
116           if (is_group)
117             {
118               static char *mydomain = NULL;
119               if (!mydomain)
120                 yp_get_default_domain(&mydomain);
121               if (mydomain && innetgr(file_host,remote,user,mydomain))
122                 host_ok = True;
123             }
124 #else
125           if (is_group)
126             {
127               DEBUG(1,("Netgroups not configured\n"));
128               continue;
129             }
130 #endif
131
132           /* is it this host */
133           /* the fact that remote has come from a call of gethostbyaddr
134            * means that it may have the fully qualified domain name
135            * so we could look up the file version to get it into
136            * a canonical form, but I would rather just type it
137            * in full in the equiv file
138            */
139           if (!host_ok && !is_group && strequal(remote, file_host))
140             host_ok = True;
141
142           if (!host_ok)
143             continue;
144
145           /* is it this user */
146           if (file_user == 0 || strequal(user, file_user)) 
147             {
148               DEBUG(5, ("check_user_equiv matched %s%s %s\n",
149                         (plus ? "+" : "-"), file_host,
150                         (file_user ? file_user : "")));
151               file_lines_free(lines);
152               return (plus ? True : False);
153             }
154         }
155       }
156     }
157   }
158   file_lines_free(lines);
159   return False;
160 }
161
162 /****************************************************************************
163 check for a possible hosts equiv or rhosts entry for the user
164 ****************************************************************************/
165
166 static BOOL check_hosts_equiv(SAM_ACCOUNT *account)
167 {
168         uid_t uid;
169         char *fname = NULL;
170
171         fname = lp_hosts_equiv();
172         if (!NT_STATUS_IS_OK(sid_to_uid(pdb_get_user_sid(account), &uid)))
173                 return False;
174
175         /* note: don't allow hosts.equiv on root */
176         if (fname && *fname && uid != 0) {
177                 if (check_user_equiv(pdb_get_username(account),client_name(),fname))
178                         return True;
179         }
180   
181         return False;
182 }
183
184
185 /****************************************************************************
186  Check for a valid .rhosts/hosts.equiv entry for this user
187 ****************************************************************************/
188
189 static NTSTATUS check_hostsequiv_security(const struct auth_context *auth_context,
190                                           void *my_private_data, 
191                                           TALLOC_CTX *mem_ctx,
192                                           const auth_usersupplied_info *user_info, 
193                                           auth_serversupplied_info **server_info)
194 {
195         NTSTATUS nt_status;
196         SAM_ACCOUNT *account = NULL;
197         if (!NT_STATUS_IS_OK(nt_status = 
198                              auth_get_sam_account(user_info->internal_username.str, 
199                                                   &account))) {
200                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) 
201                         nt_status = NT_STATUS_NOT_IMPLEMENTED;
202                 return nt_status;
203         }
204
205         if (check_hosts_equiv(account)) {
206                 nt_status = make_server_info_sam(server_info, account);
207         } else {
208                 pdb_free_sam(&account);
209                 nt_status = NT_STATUS_NOT_IMPLEMENTED;
210         }
211
212         return nt_status;
213 }
214
215 /* module initialisation */
216 static NTSTATUS auth_init_hostsequiv(struct auth_context *auth_context, const char* param, auth_methods **auth_method) 
217 {
218         if (!make_auth_methods(auth_context, auth_method)) {
219                 return NT_STATUS_NO_MEMORY;
220         }
221
222         (*auth_method)->auth = check_hostsequiv_security;
223         (*auth_method)->name = "hostsequiv";
224         return NT_STATUS_OK;
225 }
226
227
228 /****************************************************************************
229  Check for a valid .rhosts/hosts.equiv entry for this user
230 ****************************************************************************/
231
232 static NTSTATUS check_rhosts_security(const struct auth_context *auth_context,
233                                       void *my_private_data, 
234                                       TALLOC_CTX *mem_ctx,
235                                       const auth_usersupplied_info *user_info, 
236                                       auth_serversupplied_info **server_info)
237 {
238         NTSTATUS nt_status;
239         SAM_ACCOUNT *account = NULL;
240         pstring rhostsfile;
241         const char *home;
242         
243         if (!NT_STATUS_IS_OK(nt_status = 
244                              auth_get_sam_account(user_info->internal_username.str, 
245                                                   &account))) {
246                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) 
247                         nt_status = NT_STATUS_NOT_IMPLEMENTED;
248                 return nt_status;
249         }
250
251         home = pdb_get_unix_homedir(account);
252
253         if (home) {
254                 slprintf(rhostsfile, sizeof(rhostsfile)-1, "%s/.rhosts", home);
255                 become_root();
256                 if (check_user_equiv(pdb_get_username(account),client_name(),rhostsfile)) {
257                         nt_status = make_server_info_sam(server_info, account);
258                 } else {
259                         pdb_free_sam(&account);
260                 }
261                 unbecome_root();
262         } else {
263                 pdb_free_sam(&account);
264                 nt_status = NT_STATUS_NOT_IMPLEMENTED;
265         }
266         
267         return nt_status;
268 }
269
270 /* module initialisation */
271 static NTSTATUS auth_init_rhosts(struct auth_context *auth_context, const char *param, auth_methods **auth_method) 
272 {
273         if (!make_auth_methods(auth_context, auth_method)) {
274                 return NT_STATUS_NO_MEMORY;
275         }
276
277         (*auth_method)->auth = check_rhosts_security;
278         (*auth_method)->name = "rhosts";
279         return NT_STATUS_OK;
280 }
281
282 NTSTATUS auth_rhosts_init(void)
283 {
284         smb_register_auth(AUTH_INTERFACE_VERSION, "rhosts", auth_init_rhosts);
285         smb_register_auth(AUTH_INTERFACE_VERSION, "hostsequiv", auth_init_hostsequiv);
286         return NT_STATUS_OK;
287 }