def view_cifs_share(request):

  return_dict = {}
  try:
    template = 'logged_in_error.html'
  
    if request.method != "GET":
      raise Exception("Incorrect access method. Please use the menus")
  
    if "index" not in request.GET or "access_mode" not in request.GET:
      raise Exception("Insufficient parameters. Please use the menus")
  
    access_mode = request.GET["access_mode"]
    index = request.GET["index"]
  
    if "action" in request.GET and request.GET["action"] == "saved":
      return_dict["conf_message"] = "Information updated successfully"
  
    valid_users_list = None
    share, err = cifs_common.load_share_info(access_mode, index)
    if err:
      raise Exception(err)
    if not share:
      raise Exception('Specified share not found')

    valid_users_list, err = cifs_common.load_valid_users_list(share["share_id"])
    if err:
      raise Exception(err)
    if not share:
      raise Exception("Error retrieving share information for  %s" %share_name)

    return_dict["share"] = share
    if valid_users_list:
        return_dict["valid_users_list"] = valid_users_list
    template = 'view_cifs_share.html'
  
    return django.shortcuts.render_to_response(template, return_dict, context_instance=django.template.context.RequestContext(request))
  except Exception, e:
    return_dict['base_template'] = "shares_base.html"
    return_dict["page_title"] = 'CIFS share details'
    return_dict['tab'] = 'view_cifs_shares_tab'
    return_dict["error"] = 'Error loading CIFS share details'
    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 generate_smb_conf():
  try:
    d, err = cifs_common.load_auth_settings()
    if err:
      raise Exception(err)
    smb_conf_path, err = common.get_smb_conf_path()
    if err:
      raise Exception(err)
    with open("%s/smb.conf"%smb_conf_path, "w+") as f:
      ret, err = cifs_common.generate_global_header(f)
      if err:
        raise Exception(err)
      ret, err = _generate_unicell_specific_global_section(f)
      if err:
        raise Exception(err)
      ret, err = cifs_common.generate_common_global_section(f, d)
      if err:
        raise Exception(err)
      shl, err = cifs_common.load_shares_list()
      if err:
        raise Exception(err)
      if shl:
        for share in shl:
          ul = []
          gl = []
          if not share["guest_ok"]:
            vul, err = cifs_common.load_valid_users_list(share["share_id"])
            if err:
              raise Exception(err)
            if vul:
              for vu in vul:
                if vu["grp"]:
                  gl.append(vu["name"])
                else:
                  ul.append(vu["name"])
          ret, err = _generate_share_section(f, share["name"], d["workgroup"], share["path"], share["read_only"], share["browseable"], share["guest_ok"], ul, gl, share["comment"], d["security"])
          if err:
            raise Exception(err)
    ret, errors = _reload_config()
    if errors:
      raise Exception(errors)
  except Exception, e:
    return False, 'Error generating CIFS configuration : %s'%str(e)
def edit_cifs_share(request):

  return_dict = {}
  try:
    user_list, err = cifs_unicell.get_user_list()
    if err:
      raise Exception(err)
    group_list, err = cifs_unicell.get_group_list()
    if err:
      raise Exception(err)

    if request.method == "GET":
      # Shd be an edit request
      if "share_id" not in request.GET:
        raise Exception("Unknown share specified")

      share_id = request.GET["share_id"]
      share_dict, err = cifs_common.load_share_info("by_id", share_id)
      if err:
        raise Exception(err)
      valid_users_list, err = cifs_common.load_valid_users_list(share_dict["share_id"])
      if err:
        raise Exception(err)
  
      # Set initial form values
      initial = {}
      initial["share_id"] = share_dict["share_id"]
      initial["name"] = share_dict["name"]
      initial["path"] = share_dict["path"]
      if share_dict["guest_ok"]:
        initial["guest_ok"] = True
      else:
        initial["guest_ok"] = False
      if share_dict["browseable"]:
        initial["browseable"] = True
      else:
        initial["browseable"] = False
      if share_dict["read_only"]:
        initial["read_only"] = True
      else:
        initial["read_only"] = False
      initial["comment"] = share_dict["comment"]
  
      if valid_users_list:
        vgl = []
        vul = []
        for u in valid_users_list:
          if u["grp"]:
            vgl.append(u["name"])
          else:
            vul.append(u["name"])
        initial["users"] = vul
        initial["groups"] = vgl
  
      form = samba_shares_forms.EditShareForm(initial = initial, user_list = user_list, group_list = group_list)
  
      return_dict["form"] = form
      return django.shortcuts.render_to_response('edit_cifs_share.html', return_dict, context_instance=django.template.context.RequestContext(request))
  
    else:
  
      # Shd be an save request
      form = samba_shares_forms.EditShareForm(request.POST, user_list = user_list, group_list = group_list)
      return_dict["form"] = form
      if form.is_valid():
        cd = form.cleaned_data
        name = cd["name"]
        share_id = cd["share_id"]
        path = cd["path"]
        if "comment" in cd:
          comment = cd["comment"]
        else:
          comment = None
        if "read_only" in cd:
          read_only = cd["read_only"]
        else:
          read_only = False
        if "browseable" in cd:
          browseable = cd["browseable"]
        else:
          browseable = False
        if "guest_ok" in cd:
          guest_ok = cd["guest_ok"]
        else:
          guest_ok = False
        if "users" in cd:
          users = cd["users"]
        else:
          users = None
        if "groups" in cd:
          groups = cd["groups"]
        else:
          groups = None
        #logger.debug("Save share request, name %s path %s, comment %s, read_only %s, browseable %s, guest_ok %s, users %s, groups %s, vol %s"%(name, path, comment, read_only, browseable, guest_ok, users, groups))
        ret, err = cifs_common.save_share(share_id, name, comment, guest_ok, read_only, path, browseable, users, groups)
        if err:
          raise Exception(err)
        ret, err = cifs_unicell.generate_smb_conf()
        if err:
          raise Exception(err)
  
        audit_str = "Modified share %s"%cd["name"]
        audit.audit("modify_cifs_share", audit_str, request.META["REMOTE_ADDR"])
  
        return django.http.HttpResponseRedirect('/view_cifs_share?access_mode=by_id&index=%s&action=saved'%cd["share_id"])
  
      else:
        #Invalid form
        return django.shortcuts.render_to_response('edit_cifs_share.html', return_dict, context_instance=django.template.context.RequestContext(request))
  except Exception, e:
    return_dict['base_template'] = "shares_base.html"
    return_dict["page_title"] = 'Modify a CIFS share'
    return_dict['tab'] = 'view_cifs_shares_tab'
    return_dict["error"] = 'Error modifying CIFS share'
    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))