1 /*
2  * Samba Unix/Linux SMB client library
3  *
4  * Copyright (C) Gregor Beck 2011
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 3 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, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /**
21  * @brief  Notify smbd about idmap changes
22  * @file   msg_idmap.c
23  * @author Gregor Beck <gb@sernet.de>
24  * @date   Feb 2011
25  *
26  */
27 
28 #include "includes.h"
29 #include "messages.h"
30 #include "lib/id_cache.h"
31 #include "../lib/util/memcache.h"
32 #include "idmap_cache.h"
33 #include "../librpc/gen_ndr/ndr_security.h"
34 #include "../libcli/security/dom_sid.h"
35 
id_cache_ref_parse(const char * str,struct id_cache_ref * id)36 bool id_cache_ref_parse(const char* str, struct id_cache_ref* id)
37 {
38 	struct dom_sid sid;
39 	unsigned long ul;
40 	char c, trash;
41 
42 	if (sscanf(str, "%cID %lu%c", &c, &ul, &trash) == 2) {
43 		switch(c) {
44 		case 'G':
45 			id->id.gid = ul;
46 			id->type = GID;
47 			return true;
48 		case 'U':
49 			id->id.uid = ul;
50 			id->type = UID;
51 			return true;
52 		default:
53 			break;
54 		}
55 	} else if (string_to_sid(&sid, str)) {
56 		id->id.sid = sid;
57 		id->type = SID;
58 		return true;
59 	} else if (strncmp(str, "USER ", 5) == 0) {
60 		id->id.name = str + 5;
61 		id->type = USERNAME;
62 		return true;
63 	}
64 	return false;
65 }
66 
delete_getpwnam_cache(const char * username)67 static bool delete_getpwnam_cache(const char *username)
68 {
69 	DATA_BLOB name = data_blob_string_const_null(username);
70 	DEBUG(6, ("Delete passwd struct for %s from memcache\n",
71 		  username));
72 	memcache_delete(NULL, GETPWNAM_CACHE, name);
73 	return true;
74 }
75 
id_cache_delete_from_cache(const struct id_cache_ref * id)76 void id_cache_delete_from_cache(const struct id_cache_ref* id)
77 {
78 	switch(id->type) {
79 	case UID:
80 		idmap_cache_del_uid(id->id.uid);
81 		break;
82 	case GID:
83 		idmap_cache_del_gid(id->id.gid);
84 		break;
85 	case SID:
86 		idmap_cache_del_sid(&id->id.sid);
87 		break;
88 	case USERNAME:
89 		delete_getpwnam_cache(id->id.name);
90 	default:
91 		break;
92 	}
93 }
94 
id_cache_delete_message(struct messaging_context * msg_ctx,void * private_data,uint32_t msg_type,struct server_id server_id,DATA_BLOB * data)95 void id_cache_delete_message(struct messaging_context *msg_ctx,
96 			     void *private_data,
97 			     uint32_t msg_type,
98 			     struct server_id server_id,
99 			     DATA_BLOB* data)
100 {
101 	const char *msg = (data && data->data) ? (const char *)data->data : "<NULL>";
102 	struct id_cache_ref id;
103 
104 	if (!id_cache_ref_parse(msg, &id)) {
105 		DEBUG(0, ("Invalid ?ID: %s\n", msg));
106 		return;
107 	}
108 
109 	id_cache_delete_from_cache(&id);
110 }
111 
id_cache_register_msgs(struct messaging_context * ctx)112 void id_cache_register_msgs(struct messaging_context *ctx)
113 {
114 	messaging_register(ctx, NULL, ID_CACHE_DELETE, id_cache_delete_message);
115 }
116