00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "includes.h"
00021 #include "utils/net.h"
00022
00023 #define ALLOC_CHECK(mem) do { \
00024 if (!mem) { \
00025 d_fprintf(stderr, "Out of memory!\n"); \
00026 talloc_free(ctx); \
00027 return -1; \
00028 } } while(0)
00029
00030
00031
00032
00033 static int net_idmap_dump_one_entry(TDB_CONTEXT *tdb,
00034 TDB_DATA key,
00035 TDB_DATA data,
00036 void *unused)
00037 {
00038 if (strcmp(key.dptr, "USER HWM") == 0) {
00039 printf("USER HWM %d\n", IVAL(data.dptr,0));
00040 return 0;
00041 }
00042
00043 if (strcmp(key.dptr, "GROUP HWM") == 0) {
00044 printf("GROUP HWM %d\n", IVAL(data.dptr,0));
00045 return 0;
00046 }
00047
00048 if (strncmp(key.dptr, "S-", 2) != 0)
00049 return 0;
00050
00051 printf("%s %s\n", data.dptr, key.dptr);
00052 return 0;
00053 }
00054
00055
00056
00057
00058 static int net_idmap_dump(int argc, const char **argv)
00059 {
00060 TDB_CONTEXT *idmap_tdb;
00061
00062 if ( argc != 1 )
00063 return net_help_idmap( argc, argv );
00064
00065 idmap_tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);
00066
00067 if (idmap_tdb == NULL) {
00068 d_fprintf(stderr, "Could not open idmap: %s\n", argv[0]);
00069 return -1;
00070 }
00071
00072 tdb_traverse(idmap_tdb, net_idmap_dump_one_entry, NULL);
00073
00074 tdb_close(idmap_tdb);
00075
00076 return 0;
00077 }
00078
00079
00080
00081
00082
00083 static int net_idmap_restore(int argc, const char **argv)
00084 {
00085 TALLOC_CTX *ctx;
00086 FILE *input;
00087
00088 if (! winbind_ping()) {
00089 d_fprintf(stderr, "To use net idmap Winbindd must be running.\n");
00090 return -1;
00091 }
00092
00093 ctx = talloc_new(NULL);
00094 ALLOC_CHECK(ctx);
00095
00096 if (argc == 1) {
00097 input = fopen(argv[0], "r");
00098 } else {
00099 input = stdin;
00100 }
00101
00102 while (!feof(input)) {
00103 char line[128], sid_string[128];
00104 int len;
00105 DOM_SID sid;
00106 struct id_map map;
00107 unsigned long idval;
00108
00109 if (fgets(line, 127, input) == NULL)
00110 break;
00111
00112 len = strlen(line);
00113
00114 if ( (len > 0) && (line[len-1] == '\n') )
00115 line[len-1] = '\0';
00116
00117 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2) {
00118 map.xid.type = ID_TYPE_GID;
00119 map.xid.id = idval;
00120 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2) {
00121 map.xid.type = ID_TYPE_UID;
00122 map.xid.id = idval;
00123 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
00124
00125 if (! winbind_set_uid_hwm(idval)) {
00126 d_fprintf(stderr, "Could not set USER HWM\n");
00127 }
00128 continue;
00129 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
00130
00131 if (! winbind_set_gid_hwm(idval)) {
00132 d_fprintf(stderr, "Could not set GROUP HWM\n");
00133 }
00134 continue;
00135 } else {
00136 d_fprintf(stderr, "ignoring invalid line [%s]\n", line);
00137 continue;
00138 }
00139
00140 if (!string_to_sid(&sid, sid_string)) {
00141 d_fprintf(stderr, "ignoring invalid sid [%s]\n", sid_string);
00142 continue;
00143 }
00144 map.sid = &sid;
00145
00146 if (!winbind_set_mapping(&map)) {
00147 d_fprintf(stderr, "Could not set mapping of %s %lu to sid %s\n",
00148 (map.xid.type == ID_TYPE_GID) ? "GID" : "UID",
00149 (unsigned long)map.xid.id, sid_string_static(map.sid));
00150 continue;
00151 }
00152
00153 }
00154
00155 if (input != stdin) {
00156 fclose(input);
00157 }
00158
00159 talloc_free(ctx);
00160 return 0;
00161 }
00162
00163
00164
00165
00166 static int net_idmap_delete(int argc, const char **argv)
00167 {
00168 d_printf("Not Implemented yet\n");
00169 return -1;
00170 }
00171
00172 static int net_idmap_set(int argc, const char **argv)
00173 {
00174 d_printf("Not Implemented yet\n");
00175 return -1;
00176 }
00177 BOOL idmap_store_secret(const char *backend, bool alloc,
00178 const char *domain, const char *identity,
00179 const char *secret)
00180 {
00181 char *tmp;
00182 int r;
00183 BOOL ret;
00184
00185 if (alloc) {
00186 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
00187 } else {
00188 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
00189 }
00190
00191 if (r < 0) return false;
00192
00193 strupper_m(tmp);
00194 ret = secrets_store_generic(tmp, identity, secret);
00195
00196 free(tmp);
00197 return ret;
00198 }
00199
00200
00201 static int net_idmap_secret(int argc, const char **argv)
00202 {
00203 TALLOC_CTX *ctx;
00204 const char *secret;
00205 const char *dn;
00206 char *domain;
00207 char *backend;
00208 char *opt = NULL;
00209 BOOL ret;
00210
00211 if (argc != 2) {
00212 return net_help_idmap(argc, argv);
00213 }
00214
00215 secret = argv[1];
00216
00217 ctx = talloc_new(NULL);
00218 ALLOC_CHECK(ctx);
00219
00220 if (strcmp(argv[0], "alloc") == 0) {
00221 domain = NULL;
00222 backend = lp_idmap_alloc_backend();
00223 } else {
00224 domain = talloc_strdup(ctx, argv[0]);
00225 ALLOC_CHECK(domain);
00226
00227 opt = talloc_asprintf(ctx, "idmap config %s", domain);
00228 ALLOC_CHECK(opt);
00229
00230 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
00231 ALLOC_CHECK(backend);
00232 }
00233
00234 if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
00235 d_fprintf(stderr, "The only currently supported backend is LDAP\n");
00236 talloc_free(ctx);
00237 return -1;
00238 }
00239
00240 if (domain) {
00241
00242 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
00243 if ( ! dn) {
00244 d_fprintf(stderr, "Missing ldap_user_dn option for domain %s\n", domain);
00245 talloc_free(ctx);
00246 return -1;
00247 }
00248
00249 ret = idmap_store_secret("ldap", false, domain, dn, secret);
00250 } else {
00251 dn = lp_parm_const_string(-1, "idmap alloc config", "ldap_user_dn", NULL);
00252 if ( ! dn) {
00253 d_fprintf(stderr, "Missing ldap_user_dn option for alloc backend\n");
00254 talloc_free(ctx);
00255 return -1;
00256 }
00257
00258 ret = idmap_store_secret("ldap", true, NULL, dn, secret);
00259 }
00260
00261 if ( ! ret) {
00262 d_fprintf(stderr, "Failed to store secret\n");
00263 talloc_free(ctx);
00264 return -1;
00265 }
00266
00267 d_printf("Secret stored\n");
00268 return 0;
00269 }
00270
00271 int net_help_idmap(int argc, const char **argv)
00272 {
00273 d_printf("net idmap dump <inputfile>\n"\
00274 " Dump current id mapping\n");
00275
00276 d_printf("net idmap restore\n"\
00277 " Restore entries from stdin\n");
00278
00279
00280
00281 d_printf("net idmap secret <DOMAIN>|alloc <secret>\n"\
00282 " Set the secret for the specified DOMAIN (or the alloc module)\n");
00283
00284 return -1;
00285 }
00286
00287
00288
00289
00290 int net_idmap(int argc, const char **argv)
00291 {
00292 struct functable func[] = {
00293 {"dump", net_idmap_dump},
00294 {"restore", net_idmap_restore},
00295 {"setmap", net_idmap_set },
00296 {"delete", net_idmap_delete},
00297 {"secret", net_idmap_secret},
00298 {"help", net_help_idmap},
00299 {NULL, NULL}
00300 };
00301
00302 return net_run_function(argc, argv, func, net_help_idmap);
00303 }
00304
00305