Whamcloud - gitweb
LU-3289 gss: Add Shared key and GSS Null functionality
[fs/lustre-release.git] / lustre / ptlrpc / sec_config.c
index ee2c277..e9f429a 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2012, Intel Corporation.
+ * Copyright (c) 2011, 2015, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 #define DEBUG_SUBSYSTEM S_SEC
 
 #include <libcfs/libcfs.h>
-#ifndef __KERNEL__
-#include <liblustre.h>
-#include <libcfs/list.h>
-#else
 #include <linux/crypto.h>
 #include <linux/key.h>
-#endif
 
 #include <obd.h>
 #include <obd_class.h>
@@ -93,7 +88,6 @@ enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd)
         CERROR("unknown target %p(%s)\n", obd, type);
         return LUSTRE_SP_ANY;
 }
-EXPORT_SYMBOL(sptlrpc_target_sec_part);
 
 /****************************************
  * user supplied flavor string parsing  *
@@ -114,8 +108,7 @@ int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
                 return 0;
         }
 
-        strncpy(buf, str, sizeof(buf));
-        buf[sizeof(buf) - 1] = '\0';
+       strlcpy(buf, str, sizeof(buf));
 
         bulk = strchr(buf, '-');
         if (bulk)
@@ -292,7 +285,6 @@ int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
         rset->srs_nslot = nslot;
         return 0;
 }
-EXPORT_SYMBOL(sptlrpc_rule_set_expand);
 
 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
 {
@@ -440,7 +432,6 @@ int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
 
         return 0;
 }
-EXPORT_SYMBOL(sptlrpc_rule_set_choose);
 
 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *rset)
 {
@@ -453,7 +444,6 @@ void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *rset)
                        r->sr_from, r->sr_to, r->sr_netid, r->sr_flvr.sf_rpc);
         }
 }
-EXPORT_SYMBOL(sptlrpc_rule_set_dump);
 
 static int sptlrpc_rule_set_extract(struct sptlrpc_rule_set *gen,
                                     struct sptlrpc_rule_set *tgt,
@@ -500,23 +490,23 @@ static int sptlrpc_rule_set_extract(struct sptlrpc_rule_set *gen,
  **********************************/
 
 struct sptlrpc_conf_tgt {
-        cfs_list_t              sct_list;
+       struct list_head              sct_list;
         char                    sct_name[MAX_OBD_NAME];
         struct sptlrpc_rule_set sct_rset;
 };
 
 struct sptlrpc_conf {
-        cfs_list_t              sc_list;
-        char                    sc_fsname[MTI_NAME_MAXLEN];
-        unsigned int            sc_modified;  /* modified during updating */
-        unsigned int            sc_updated:1, /* updated copy from MGS */
-                                sc_local:1;   /* local copy from target */
-        struct sptlrpc_rule_set sc_rset;      /* fs general rules */
-        cfs_list_t              sc_tgts;      /* target-specific rules */
+       struct list_head        sc_list;
+       char                    sc_fsname[MTI_NAME_MAXLEN];
+       unsigned int            sc_modified;    /* modified during updating */
+       unsigned int            sc_updated:1,   /* updated copy from MGS */
+                               sc_local:1;     /* local copy from target */
+       struct sptlrpc_rule_set sc_rset;        /* fs general rules */
+       struct list_head        sc_tgts;        /* target-specific rules */
 };
 
 static struct mutex sptlrpc_conf_lock;
-static CFS_LIST_HEAD(sptlrpc_confs);
+static struct list_head sptlrpc_confs;
 
 static inline int is_hex(char c)
 {
@@ -551,29 +541,29 @@ static void target2fsname(const char *tgt, char *fsname, int buflen)
 
 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
 {
-        struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
+       struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
 
-        sptlrpc_rule_set_free(&conf->sc_rset);
+       sptlrpc_rule_set_free(&conf->sc_rset);
 
-        cfs_list_for_each_entry_safe(conf_tgt, conf_tgt_next,
-                                     &conf->sc_tgts, sct_list) {
-                sptlrpc_rule_set_free(&conf_tgt->sct_rset);
-                cfs_list_del(&conf_tgt->sct_list);
-                OBD_FREE_PTR(conf_tgt);
-        }
-        LASSERT(cfs_list_empty(&conf->sc_tgts));
+       list_for_each_entry_safe(conf_tgt, conf_tgt_next,
+                                &conf->sc_tgts, sct_list) {
+               sptlrpc_rule_set_free(&conf_tgt->sct_rset);
+               list_del(&conf_tgt->sct_list);
+               OBD_FREE_PTR(conf_tgt);
+       }
+       LASSERT(list_empty(&conf->sc_tgts));
 
-        conf->sc_updated = 0;
-        conf->sc_local = 0;
+       conf->sc_updated = 0;
+       conf->sc_local = 0;
 }
 
 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
 {
-        CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
+       CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
 
-        sptlrpc_conf_free_rsets(conf);
-        cfs_list_del(&conf->sc_list);
-        OBD_FREE_PTR(conf);
+       sptlrpc_conf_free_rsets(conf);
+       list_del(&conf->sc_list);
+       OBD_FREE_PTR(conf);
 }
 
 static
@@ -583,7 +573,7 @@ struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
 {
         struct sptlrpc_conf_tgt *conf_tgt;
 
-        cfs_list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
+       list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
                 if (strcmp(conf_tgt->sct_name, name) == 0)
                         return conf_tgt;
         }
@@ -595,7 +585,7 @@ struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
         if (conf_tgt) {
                strlcpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
                 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
-                cfs_list_add(&conf_tgt->sct_list, &conf->sc_tgts);
+               list_add(&conf_tgt->sct_list, &conf->sc_tgts);
         }
 
         return conf_tgt;
@@ -607,7 +597,7 @@ struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
 {
         struct sptlrpc_conf *conf;
 
-        cfs_list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
+       list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
                 if (strcmp(conf->sc_fsname, fsname) == 0)
                         return conf;
         }
@@ -619,10 +609,14 @@ struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
         if (conf == NULL)
                 return NULL;
 
-        strcpy(conf->sc_fsname, fsname);
+       if (strlcpy(conf->sc_fsname, fsname, sizeof(conf->sc_fsname)) >=
+           sizeof(conf->sc_fsname)) {
+               OBD_FREE_PTR(conf);
+               return NULL;
+       }
         sptlrpc_rule_set_init(&conf->sc_rset);
-        CFS_INIT_LIST_HEAD(&conf->sc_tgts);
-        cfs_list_add(&conf->sc_list, &sptlrpc_confs);
+       INIT_LIST_HEAD(&conf->sc_tgts);
+       list_add(&conf->sc_list, &sptlrpc_confs);
 
         CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
         return conf;
@@ -844,6 +838,14 @@ static void inline flavor_set_flags(struct sptlrpc_flavor *sf,
                 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
                         sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
         }
+
+       /* Some flavors use a single uid (0) context */
+       if (flvr_is_rootonly(sf->sf_rpc))
+               sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
+
+       /* User descriptor might need to be cleared */
+       if (flvr_allows_user_desc(sf->sf_rpc) == 0)
+               sf->sf_flags &= ~PTLRPC_SEC_FL_UDESC;
 }
 
 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
@@ -901,7 +903,6 @@ void sptlrpc_target_choose_flavor(struct sptlrpc_rule_set *rset,
         if (sptlrpc_rule_set_choose(rset, from, LUSTRE_SP_ANY, nid, sf) == 0)
                 get_default_flavor(sf);
 }
-EXPORT_SYMBOL(sptlrpc_target_choose_flavor);
 
 #define SEC_ADAPT_DELAY         (10)
 
@@ -914,9 +915,11 @@ void sptlrpc_conf_client_adapt(struct obd_device *obd)
         struct obd_import  *imp;
         ENTRY;
 
-        LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
-                strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) ==0);
-        CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
+       LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
+               strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0 ||
+               strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) == 0 ||
+               strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) == 0);
+       CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
 
         /* serialize with connect/disconnect import */
        down_read(&obd->u.cli.cl_sem);
@@ -935,18 +938,15 @@ void sptlrpc_conf_client_adapt(struct obd_device *obd)
 }
 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
 
-#ifdef __KERNEL__
 
 static void rule2string(struct sptlrpc_rule *r, char *buf, int buflen)
 {
-        char    dirbuf[8];
-        char   *net;
-        char   *ptr = buf;
+       char     dirbuf[8];
+       char     net[LNET_NIDSTR_SIZE] = "default";
+       char    *ptr = buf;
 
-        if (r->sr_netid == LNET_NIDNET(LNET_NID_ANY))
-                net = "default";
-        else
-                net = libcfs_net2str(r->sr_netid);
+       if (r->sr_netid != LNET_NIDNET(LNET_NID_ANY))
+               libcfs_net2str_r(r->sr_netid, net, sizeof(net));
 
         if (r->sr_from == LUSTRE_SP_ANY && r->sr_to == LUSTRE_SP_ANY)
                 dirbuf[0] = '\0';
@@ -965,32 +965,26 @@ static int sptlrpc_record_rule_set(struct llog_handle *llh,
                                    char *target,
                                    struct sptlrpc_rule_set *rset)
 {
-        struct lustre_cfg_bufs  bufs;
-        struct lustre_cfg      *lcfg;
-        struct llog_rec_hdr     rec;
-        int                     buflen;
-        char                    param[48];
-        int                     i, rc;
-
-        for (i = 0; i < rset->srs_nrule; i++) {
-                rule2string(&rset->srs_rules[i], param, sizeof(param));
-
-                lustre_cfg_bufs_reset(&bufs, NULL);
-                lustre_cfg_bufs_set_string(&bufs, 1, target);
-                lustre_cfg_bufs_set_string(&bufs, 2, param);
-                lcfg = lustre_cfg_new(LCFG_SPTLRPC_CONF, &bufs);
-                LASSERT(lcfg);
-
-                buflen = lustre_cfg_len(lcfg->lcfg_bufcount,
-                                        lcfg->lcfg_buflens);
-                rec.lrh_len = llog_data_len(buflen);
-                rec.lrh_type = OBD_CFG_REC;
-               rc = llog_write(NULL, llh, &rec, NULL, 0, (void *)lcfg, -1);
-                if (rc)
-                        CERROR("failed to write a rec: rc = %d\n", rc);
-                lustre_cfg_free(lcfg);
-        }
-        return 0;
+       struct llog_cfg_rec     *lcr;
+       struct lustre_cfg_bufs   bufs;
+       char                     param[48];
+       int                      i, rc;
+
+       for (i = 0; i < rset->srs_nrule; i++) {
+               rule2string(&rset->srs_rules[i], param, sizeof(param));
+
+               lustre_cfg_bufs_reset(&bufs, NULL);
+               lustre_cfg_bufs_set_string(&bufs, 1, target);
+               lustre_cfg_bufs_set_string(&bufs, 2, param);
+               lcr = lustre_cfg_rec_new(LCFG_SPTLRPC_CONF, &bufs);
+               if (lcr == NULL)
+                       return -ENOMEM;
+               rc = llog_write(NULL, llh, &lcr->lcr_hdr, LLOG_NEXT_IDX);
+               lustre_cfg_rec_free(lcr);
+               if (rc)
+                       return rc;
+       }
+       return 0;
 }
 
 static int sptlrpc_record_rules(struct llog_handle *llh,
@@ -1000,7 +994,7 @@ static int sptlrpc_record_rules(struct llog_handle *llh,
 
         sptlrpc_record_rule_set(llh, conf->sc_fsname, &conf->sc_rset);
 
-        cfs_list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
+       list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
                 sptlrpc_record_rule_set(llh, conf_tgt->sct_name,
                                         &conf_tgt->sct_rset);
         }
@@ -1021,11 +1015,11 @@ int sptlrpc_target_local_copy_conf(struct obd_device *obd,
         int                   rc;
         ENTRY;
 
-        ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
+       ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
        if (ctxt == NULL)
-                RETURN(-EINVAL);
+               RETURN(-EINVAL);
 
-        push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
+       push_ctxt(&saved, &obd->obd_lvfs_ctxt);
 
        dentry = ll_lookup_one_len(MOUNT_CONFIGS_DIR, current->fs->pwd.dentry,
                                    strlen(MOUNT_CONFIGS_DIR));
@@ -1060,13 +1054,13 @@ out_close:
                rc = lustre_rename(dentry, obd->obd_lvfs_ctxt.pwdmnt,
                                   LOG_SPTLRPC_TMP, LOG_SPTLRPC);
 out_dput:
-        l_dput(dentry);
+       dput(dentry);
 out_ctx:
-        pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
-        llog_ctxt_put(ctxt);
-        CDEBUG(D_SEC, "target %s: write local sptlrpc conf: rc = %d\n",
-               obd->obd_name, rc);
-        RETURN(rc);
+       pop_ctxt(&saved, &obd->obd_lvfs_ctxt);
+       llog_ctxt_put(ctxt);
+       CDEBUG(D_SEC, "target %s: write local sptlrpc conf: rc = %d\n",
+               obd->obd_name, rc);
+       RETURN(rc);
 }
 
 static int local_read_handler(const struct lu_env *env,
@@ -1118,7 +1112,7 @@ int sptlrpc_target_local_read_conf(struct obd_device *obd,
                 RETURN(-EINVAL);
         }
 
-        push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
+       push_ctxt(&saved, &obd->obd_lvfs_ctxt);
 
        rc = llog_open(NULL, ctxt, &llh, NULL, LOG_SPTLRPC, LLOG_OPEN_EXISTS);
        if (rc < 0) {
@@ -1147,14 +1141,13 @@ int sptlrpc_target_local_read_conf(struct obd_device *obd,
 out_close:
        llog_close(NULL, llh);
 out_pop:
-        pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
-        llog_ctxt_put(ctxt);
-        CDEBUG(D_SEC, "target %s: read local sptlrpc conf: rc = %d\n",
-               obd->obd_name, rc);
-        RETURN(rc);
+       pop_ctxt(&saved, &obd->obd_lvfs_ctxt);
+       llog_ctxt_put(ctxt);
+       CDEBUG(D_SEC, "target %s: read local sptlrpc conf: rc = %d\n",
+               obd->obd_name, rc);
+       RETURN(rc);
 }
 
-#endif /* __KRENEL__ */
 
 /**
  * called by target devices, extract sptlrpc rules which applies to
@@ -1191,7 +1184,6 @@ int sptlrpc_conf_target_get_rules(struct obd_device *obd,
                 GOTO(out, rc);
         }
 
-#ifdef __KERNEL__
         if (conf->sc_updated  == 0) {
                 /*
                  * always read from local copy. here another option is
@@ -1211,7 +1203,6 @@ int sptlrpc_conf_target_get_rules(struct obd_device *obd,
                 else
                         CDEBUG(D_SEC, "unchanged, skip updating local copy\n");
         }
-#endif
 
         /* extract rule set for this target */
         conf_tgt = sptlrpc_conf_get_tgt(conf, obd->obd_name, 0);
@@ -1223,22 +1214,21 @@ out:
        mutex_unlock(&sptlrpc_conf_lock);
         RETURN(rc);
 }
-EXPORT_SYMBOL(sptlrpc_conf_target_get_rules);
 
 int  sptlrpc_conf_init(void)
 {
+       INIT_LIST_HEAD(&sptlrpc_confs);
        mutex_init(&sptlrpc_conf_lock);
-        return 0;
+       return 0;
 }
 
 void sptlrpc_conf_fini(void)
 {
-        struct sptlrpc_conf  *conf, *conf_next;
+       struct sptlrpc_conf  *conf, *conf_next;
 
        mutex_lock(&sptlrpc_conf_lock);
-        cfs_list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list) {
-                sptlrpc_conf_free(conf);
-        }
-        LASSERT(cfs_list_empty(&sptlrpc_confs));
+       list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list)
+               sptlrpc_conf_free(conf);
+       LASSERT(list_empty(&sptlrpc_confs));
        mutex_unlock(&sptlrpc_conf_lock);
 }