def view_local_groups(request): return_dict = {} try: group_list, err = local_users.get_local_groups() if err: raise Exception(err) nfs_group, err = config.get_local_nfs_group_name() if err: raise Exception(err) # Do not display local NFS group for idx, group in enumerate(group_list[:]): if group['grpname'] == str(nfs_group): group_list.pop(idx) return_dict["group_list"] = group_list if "ack" in request.GET: if request.GET["ack"] == "created": return_dict['ack_message'] = "Local group successfully created" elif request.GET["ack"] == "deleted": return_dict['ack_message'] = "Local group successfully deleted" elif request.GET["ack"] == "set_membership": return_dict['ack_message'] = "Local group membership successfully modified" return django.shortcuts.render_to_response('view_local_groups.html', return_dict, context_instance=django.template.context.RequestContext(request)) except Exception, e: return_dict['base_template'] = 'users_groups_base.html' return_dict["page_title"] = 'Local groups' return_dict['tab'] = 'view_local_groups_tab' return_dict["error"] = 'Error loading local groups list' return_dict["error_details"] = str(e) return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def create_local_user(request): return_dict = {} try: group_list, err = local_users.get_local_groups() if err: raise Exception(err) if request.method == "GET": # Return the form #form = local_user_forms.LocalUserForm(group_list = group_list) form = local_user_forms.LocalUserForm() return_dict["form"] = form return django.shortcuts.render_to_response("create_local_user.html", return_dict, context_instance=django.template.context.RequestContext(request)) else: # Form submission so create return_dict = {} #form = local_user_forms.LocalUserForm(request.POST, group_list = group_list) form = local_user_forms.LocalUserForm(request.POST) if form.is_valid(): cd = form.cleaned_data #ret, err = local_users.create_local_user(cd["username"], cd["name"], cd["password"], cd['gid']) group_name, err = config.get_users_default_group() if err: raise Exception(err) default_gid, err = config.get_system_uid_gid( group_name, 'group') if err: raise Exception(err) ret, err = local_users.create_local_user( cd["username"], cd["name"], cd["password"], default_gid) if not ret: if err: raise Exception(err) else: raise Exception("Error creating the local user.") audit_str = "Created a local user %s" % cd["username"] audit.audit("create_local_user", audit_str, request) if group_list: url = '/users_groups/update_local_user_group_membership/?username=%s&ack=created' % cd['username'] else: url = '/users_groups/view_local_users?ack=created' return django.http.HttpResponseRedirect(url) else: return_dict["form"] = form return django.shortcuts.render_to_response("create_local_user.html", return_dict, context_instance=django.template.context.RequestContext(request)) except Exception, e: return_dict['base_template'] = 'users_groups_base.html' return_dict["page_title"] = 'Create a local users' return_dict['tab'] = 'view_local_users_tab' return_dict["error"] = 'Error creating a local user' return_dict["error_details"] = str(e) return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def create_local_user(request): return_dict = {} try: group_list, err = local_users.get_local_groups() if err: raise Exception(err) if request.method == "GET": # Return the form #form = local_user_forms.LocalUserForm(group_list = group_list) form = local_user_forms.LocalUserForm() return_dict["form"] = form return django.shortcuts.render_to_response("create_local_user.html", return_dict, context_instance=django.template.context.RequestContext(request)) else: # Form submission so create return_dict = {} #form = local_user_forms.LocalUserForm(request.POST, group_list = group_list) form = local_user_forms.LocalUserForm(request.POST) if form.is_valid(): cd = form.cleaned_data #ret, err = local_users.create_local_user(cd["username"], cd["name"], cd["password"], cd['gid']) group_name, err = config.get_users_default_group() if err: raise Exception(err) default_gid, err = config.get_system_uid_gid( group_name, 'group') if err: raise Exception(err) ret, err = local_users.create_local_user( cd["username"], cd["name"], cd["password"], default_gid) if not ret: if err: raise Exception(err) else: raise Exception("Error creating the local user.") audit_str = "Created a local user %s" % cd["username"] audit.audit("create_local_user", audit_str, request) if group_list: url = '/update_local_user_group_membership/?username=%s&ack=created' % cd['username'] else: url = '/view_local_users?ack=created' return django.http.HttpResponseRedirect(url) else: return_dict["form"] = form return django.shortcuts.render_to_response("create_local_user.html", return_dict, context_instance=django.template.context.RequestContext(request)) except Exception, e: return_dict['base_template'] = 'users_groups_base.html' return_dict["page_title"] = 'Create a local users' return_dict['tab'] = 'view_local_users_tab' return_dict["error"] = 'Error creating a local user' return_dict["error_details"] = str(e) return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def download_sys_info(request): return_dict = {} try: config_dir, err = config.get_config_dir() if err: raise Exception(err) zf_name = "system_info.zip" try: out = io.BytesIO() zf = zipfile.ZipFile(out, 'w') abs_src = os.path.abspath(config_dir) lu, err = local_users.get_local_users() if err: raise Exception(err) lg, err = local_users.get_local_groups() if err: raise Exception(err) with open('/tmp/local_users_tmp', 'w') as fd: json.dump(lu, fd, indent=2) with open('/tmp/local_groups_tmp', 'w') as fd: json.dump(lg, fd, indent=2) zf.write('/tmp/local_users_tmp', 'local_users') zf.write('/tmp/local_groups_tmp', 'local_groups') for conf_subdir in upload_download_conf_dirs: for dirname, subdirs, files in os.walk( '%s/%s' % (config_dir, conf_subdir)): for filename in files: absname = os.path.abspath( os.path.join(dirname, filename)) arcname = absname[len(abs_src) + 1:] zf.write(absname, arcname) for key, value in upload_download_logs.iteritems(): if os.path.isfile(value): zf.write(value, key) zf.close() audit_str = 'Downloaded system configuration.' audit.audit('download_configuration', audit_str, request) except Exception as e: raise Exception("Error compressing remote log file : %s" % str(e)) response = django.http.HttpResponse( out.getvalue(), content_type='application/x-compressed') response['Content-disposition'] = 'attachment; filename=%s' % (zf_name) return response except Exception as e: return_dict["base_template"] = 'system_base.html' return_dict['tab'] = 'system_info_tab' return_dict["page_title"] = 'Download system configuration' return_dict["error"] = 'Error downloading system configuration' return_dict["error_details"] = str(e) return django.shortcuts.render_to_response( "logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def download_sys_info(request): return_dict = {} try: config_dir, err = config.get_config_dir() if err: raise Exception(err) zf_name = "system_info.zip" try: out = io.BytesIO() zf = zipfile.ZipFile(out, 'w') abs_src = os.path.abspath(config_dir) lu, err = local_users.get_local_users() if err: raise Exception(err) lg, err = local_users.get_local_groups() if err: raise Exception(err) with open('/tmp/local_users_tmp', 'w') as fd: json.dump(lu, fd, indent=2) with open('/tmp/local_groups_tmp', 'w') as fd: json.dump(lg, fd, indent=2) zf.write('/tmp/local_users_tmp', 'local_users') zf.write('/tmp/local_groups_tmp', 'local_groups') for conf_subdir in upload_download_conf_dirs: for dirname, subdirs, files in os.walk('%s/%s' % (config_dir, conf_subdir)): for filename in files: absname = os.path.abspath( os.path.join(dirname, filename)) arcname = absname[len(abs_src) + 1:] zf.write(absname, arcname) for key, value in upload_download_logs.iteritems(): if os.path.isfile(value): zf.write(value, key) zf.close() audit_str = 'Downloaded system configuration.' audit.audit('download_configuration', audit_str, request) except Exception as e: raise Exception("Error compressing remote log file : %s" % str(e)) response = django.http.HttpResponse( out.getvalue(), content_type='application/x-compressed') response['Content-disposition'] = 'attachment; filename=%s' % ( zf_name) return response except Exception as e: return_dict["base_template"] = 'system_base.html' return_dict['tab'] = 'system_info_tab' return_dict["page_title"] = 'Download system configuration' return_dict["error"] = 'Error downloading system configuration' return_dict["error_details"] = str(e) return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def update_dir_owner(request): return_dict = {} try: users, err = local_users.get_local_users() if err: raise Exception('Error retrieving local user list : %s' % err) if not users: raise Exception( 'No local users seem to be created. Please create at least one local user before performing this operation.') groups, err = local_users.get_local_groups() if err: raise Exception('Error retrieving local group list : %s' % err) if not groups: raise Exception( 'No local groups seem to be created. Please create at least one local group before performing this operation.') if request.method == "GET": if 'path' not in request.GET: raise Exception('Invalid request. Please use the menus.') path = request.GET['path'] try: stat_info = os.stat(path) except Exception, e: raise Exception('Error accessing specified path : %s' % str(e)) uid = stat_info.st_uid gid = stat_info.st_gid user_name = pwd.getpwuid(uid)[0] group_name = grp.getgrgid(gid)[0] initial = {} initial['path'] = path initial['uid'] = uid initial['gid'] = gid initial['user_name'] = user_name initial['group_name'] = group_name form = folder_management_forms.ModifyOwnershipForm( initial=initial, user_list=users, group_list=groups) return_dict["form"] = form return django.shortcuts.render_to_response('update_dir_ownership.html', return_dict, context_instance=django.template.context.RequestContext(request)) else:
def _get_user_or_group_list(type): ret = None try: d, err = get_auth_settings() if err: raise Exception(err) if not d: raise Exception( "Unspecified authentication method. Could not retrieve users") elif d["security"] == "users": if type and type == "users": l, err = local_users.get_local_users() if err: raise Exception(err) if l: ret = [] for ld in l: ret.append(ld["username"]) else: l, err = local_users.get_local_groups() if err: raise Exception(err) if l: ret = [] for ld in l: ret.append(ld["grpname"]) elif d["security"] == "ads": if type and type == "users": ret, err = get_ad_users_or_groups("users") if err: raise Exception(err) elif type and type == "groups": ret, err = get_ad_users_or_groups("groups") if err: raise Exception(err) else: raise Exception( "Unsupported authentication method. Could not retrieve users") except Exception, e: return None, 'Error retrieving user of group list : %s' % str(e)
def update_dir_permissions(request): return_dict = {} try: if not "error" in return_dict: if "ack" in request.GET: if request.GET["ack"] == "ace_deleted": return_dict['ack_message'] = "ACL entry successfully removed" elif request.GET["ack"] == "aces_added": return_dict['ack_message'] = "ACL entries successfully added" elif request.GET["ack"] == "aces_modified": return_dict['ack_message'] = "ACL entries successfully modified" elif request.GET["ack"] == "created_dir": return_dict['ack_message'] = "Directory successfully created" elif request.GET["ack"] == "deleted_dir": return_dict['ack_message'] = "Directory successfully deleted" users, err = local_users.get_local_users() if err: raise Exception('Error retrieving local user list : %s' % err) if not users: raise Exception( 'No local users seem to be created. Please create at least one local user before performing this operation.') groups, err = local_users.get_local_groups() if err: raise Exception('Error retrieving local group list : %s' % err) if not groups: raise Exception( 'No local groups seem to be created. Please create at least one local group before performing this operation.') pools, err = zfs.get_pools() ds_list = [] for pool in pools: for ds in pool["datasets"]: if ds['properties']['type']['value'] == 'filesystem': ds_list.append(ds["name"]) if not ds_list: raise Exception( 'No ZFS datasets available. Please create a dataset before creating shares.') req_ret, err = django_utils.get_request_parameter_values(request, [ 'path']) if err: raise Exception(err) if 'path' not in req_ret: path = "/" + pools[0]["datasets"][0]["name"] else: path = req_ret['path'] try: stat_info = os.stat(path) except Exception, e: raise Exception('Error accessing specified path : %s' % str(e)) uid = stat_info.st_uid gid = stat_info.st_gid username = pwd.getpwuid(uid)[0] grpname = grp.getgrgid(gid)[0] return_dict["username"] = username return_dict["grpname"] = grpname aces, err = acl.get_all_aces(path) if err: raise Exception(err) minimal_aces, err = acl.get_minimal_aces(aces) if err: raise Exception(err) user_aces, err = acl.get_ug_aces(aces, None, 'user') if err: raise Exception(err) group_aces, err = acl.get_ug_aces(aces, None, 'group') if err: raise Exception(err) return_dict['aces'] = aces return_dict['minimal_aces'] = minimal_aces if user_aces: return_dict['user_aces'] = user_aces if group_aces: return_dict['group_aces'] = group_aces return_dict['path'] = path return_dict["dataset"] = ds_list if request.method == "GET": # Shd be an edit request # Set initial form values initial = {} initial['path'] = path initial['owner_read'] = _owner_readable(stat_info) initial['owner_write'] = _owner_writeable(stat_info) initial['owner_execute'] = _owner_executeable(stat_info) initial['group_read'] = _group_readable(stat_info) initial['group_write'] = _group_writeable(stat_info) initial['group_execute'] = _group_executeable(stat_info) initial['other_read'] = _other_readable(stat_info) initial['other_write'] = _other_writeable(stat_info) initial['other_execute'] = _other_executeable(stat_info) if 'dataset' in request.GET: initial['dataset'] = request.GET['dataset'] form = folder_management_forms.SetFileOwnerAndPermissionsForm( initial=initial, user_list=users, group_list=groups) return_dict["form"] = form return django.shortcuts.render_to_response('update_dir_permissions.html', return_dict, context_instance=django.template.context.RequestContext(request)) elif request.method == "POST": path = request.POST.get("path") # Shd be an save request if request.POST.get("action") == "add_folder": folder_name = request.POST.get("new_folder_name") directory = path + "/" + folder_name if not os.path.exists(directory): os.makedirs(directory) audit_str = "Creating %s" % directory audit.audit("modify_dir_owner_permissions", audit_str, request) elif request.POST.get("action") == "delete_folder": delete = "false" if len(path.split("/")) > 2: delete = "true" # Need to also check if the path is a share or not. If share, dont delete again. # Checking NFS exports, err = nfs.load_exports_list() if exports: for export in exports: print id(export["path"]), id(path) if export["path"] == path: delete = "false" break else: delete = "true" if delete: print delete # shutil.rmtree(path,ignore_errors=True) audit_str = "Deleting directory %s" % path audit.audit("modify_dir_owner_permissions", audit_str, request) else: raise Exception( "Cannot delete folder. It is either a dataset of a share") else: form = folder_management_forms.SetFileOwnerAndPermissionsForm( request.POST, user_list=users, group_list=groups) return_dict["form"] = form if form.is_valid(): cd = form.cleaned_data ret, err = file_processing.update_dir_ownership_and_permissions( cd) if not ret: if err: raise Exception(err) else: raise Exception( "Error setting directory ownership/permissions.") audit_str = "Modified directory ownsership/permissions for %s" % cd["path"] audit.audit("modify_dir_owner_permissions", audit_str, request) return django.http.HttpResponseRedirect('/storage/update_dir_permissions/?ack=set_permissions') else: return django.shortcuts.render_to_response('update_dir_permissions.html', return_dict, context_instance=django.template.context.RequestContext(request))
def update_local_user_group_membership(request): return_dict = {} try: if "ack" in request.GET: if request.GET["ack"] == "created": return_dict['ack_message'] = "Local user successfully created" t_group_list, err = local_users.get_local_groups() if err: raise Exception(err) req_ret, err = django_utils.get_request_parameter_values(request, [ 'username']) if err: raise Exception(err) if 'username' not in req_ret: raise Exception('Invalid request, please use the menus.') username = req_ret['username'] ud, err = local_users.get_local_user(username) if err: raise Exception(err) if not ud: raise Exception("Specified user information not found") group_list = [] if t_group_list: for g in t_group_list: if g['grpname'] == ud['grpname']: continue else: group_list.append(g) if request.method == "GET": # Shd be an edit request # Set initial form values initial = {} initial['username'] = ud['username'] initial['groups'] = ud['other_groups'] form = local_user_forms.EditLocalUserGroupMembershipForm( initial=initial, group_list=group_list) return_dict["form"] = form return django.shortcuts.render_to_response('update_local_user_group_membership.html', return_dict, context_instance=django.template.context.RequestContext(request)) else: # Shd be an save request form = local_user_forms.EditLocalUserGroupMembershipForm( request.POST, group_list=group_list) return_dict["form"] = form if form.is_valid(): cd = form.cleaned_data ret, err = local_users.set_local_user_group_membership(cd) if not ret: if err: raise Exception(err) else: raise Exception( "Error saving user's group membership information") audit_str = "Modified local user group membership information %s" % cd[ "username"] audit.audit("modify_local_user_grp_membership", audit_str, request) return django.http.HttpResponseRedirect('/users_groups/view_local_users?ack=groups_changed') else: # Invalid form return django.shortcuts.render_to_response('update_local_user_group_membership.html', return_dict, context_instance=django.template.context.RequestContext(request)) except Exception, e: return_dict['base_template'] = 'users_groups_base.html' return_dict["page_title"] = 'Local users additional group membership' return_dict['tab'] = 'view_local_users_tab' return_dict["error"] = 'Error modifying local users additional group membership' return_dict["error_details"] = str(e) return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def update_local_user_group_membership(request): return_dict = {} try: if "ack" in request.GET: if request.GET["ack"] == "created": return_dict['ack_message'] = "Local user successfully created" t_group_list, err = local_users.get_local_groups() if err: raise Exception(err) req_ret, err = django_utils.get_request_parameter_values( request, ['username']) if err: raise Exception(err) if 'username' not in req_ret: raise Exception('Invalid request, please use the menus.') username = req_ret['username'] ud, err = local_users.get_local_user(username) if err: raise Exception(err) if not ud: raise Exception("Specified user information not found") group_list = [] if t_group_list: for g in t_group_list: if g['grpname'] == ud['grpname']: continue else: group_list.append(g) if request.method == "GET": # Shd be an edit request # Set initial form values initial = {} initial['username'] = ud['username'] initial['groups'] = ud['other_groups'] form = local_user_forms.EditLocalUserGroupMembershipForm( initial=initial, group_list=group_list) return_dict["form"] = form return django.shortcuts.render_to_response( 'update_local_user_group_membership.html', return_dict, context_instance=django.template.context.RequestContext( request)) else: # Shd be an save request form = local_user_forms.EditLocalUserGroupMembershipForm( request.POST, group_list=group_list) return_dict["form"] = form if form.is_valid(): cd = form.cleaned_data ret, err = local_users.set_local_user_group_membership(cd) if not ret: if err: raise Exception(err) else: raise Exception( "Error saving user's group membership information") audit_str = "Modified local user group membership information %s" % cd[ "username"] audit.audit("modify_local_user_grp_membership", audit_str, request) return django.http.HttpResponseRedirect( '/users_groups/view_local_users?ack=groups_changed') else: # Invalid form return django.shortcuts.render_to_response( 'update_local_user_group_membership.html', return_dict, context_instance=django.template.context.RequestContext( request)) except Exception, e: return_dict['base_template'] = 'users_groups_base.html' return_dict["page_title"] = 'Local users additional group membership' return_dict['tab'] = 'view_local_users_tab' return_dict[ "error"] = 'Error modifying local users additional group membership' return_dict["error_details"] = str(e) return django.shortcuts.render_to_response( "logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))