first public release of samba4 code
[bbaumbach/samba-autobuild/.git] / source4 / passdb / pdb_unix.c
1 /*
2  * Unix password backend for samba
3  * Copyright (C) Jelmer Vernooij 2002
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  * 
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 675
17  * Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include "includes.h"
21
22 /******************************************************************
23   Lookup a name in the SAM database
24  ******************************************************************/
25
26 static NTSTATUS unixsam_getsampwnam (struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname)
27 {
28         struct passwd *pass;
29         if (!methods) {
30                 DEBUG(0,("invalid methods\n"));
31                 return NT_STATUS_UNSUCCESSFUL;
32         }
33         if (!sname) {
34                 DEBUG(0,("invalid name specified"));
35                 return NT_STATUS_UNSUCCESSFUL;
36         }
37         pass = Get_Pwnam(sname);
38
39         return pdb_fill_sam_pw(user, pass);
40 }
41
42
43 /***************************************************************************
44   Search by rid
45  **************************************************************************/
46
47 static NTSTATUS unixsam_getsampwrid (struct pdb_methods *methods, 
48                                  SAM_ACCOUNT *user, uint32 rid)
49 {
50         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
51         struct passwd *pass = NULL;
52         const char *guest_account = lp_guestaccount();
53         if (!(guest_account && *guest_account)) {
54                 DEBUG(1, ("NULL guest account!?!?\n"));
55                 return nt_status;
56         }
57
58         if (!methods) {
59                 DEBUG(0,("invalid methods\n"));
60                 return nt_status;
61         }
62         
63         if (rid == DOMAIN_USER_RID_GUEST) {
64                 pass = getpwnam_alloc(guest_account);
65                 if (!pass) {
66                         DEBUG(1, ("guest account %s does not seem to exist...\n", guest_account));
67                         return nt_status;
68                 }
69         } else if (pdb_rid_is_user(rid)) {
70                 pass = getpwuid_alloc(fallback_pdb_user_rid_to_uid (rid));
71         }
72
73         if (pass == NULL) {
74                 return nt_status;
75         }
76
77         nt_status = pdb_fill_sam_pw(user, pass);
78         passwd_free(&pass);
79
80         return nt_status;
81 }
82
83 static NTSTATUS unixsam_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
84 {
85         uint32 rid;
86         if (!sid_peek_check_rid(get_global_sam_sid(), sid, &rid))
87                 return NT_STATUS_UNSUCCESSFUL;
88         return unixsam_getsampwrid(my_methods, user, rid);
89 }
90
91 NTSTATUS pdb_init_unixsam(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
92 {
93         NTSTATUS nt_status;
94         
95         if (!pdb_context) {
96                 DEBUG(0, ("invalid pdb_context specified\n"));
97                 return NT_STATUS_UNSUCCESSFUL;
98         }
99
100         if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
101                 return nt_status;
102         }
103         
104         (*pdb_method)->name = "unixsam";
105         (*pdb_method)->getsampwnam = unixsam_getsampwnam;
106         (*pdb_method)->getsampwsid = unixsam_getsampwsid;
107         
108         /* There's not very much to initialise here */
109         return NT_STATUS_OK;
110 }