lib/krb5_wrap: make sure smb_krb5_principal_get_realm returns a malloced string.
[samba.git] / lib / krb5_wrap / krb5_samba.c
index ec44f9ade2671dea9004f00fe0c253d0f990838d..8e560d2df3f8eb5eba8d21a17c1f827c8bb0a949 100644 (file)
@@ -734,6 +734,39 @@ void kerberos_free_data_contents(krb5_context context, krb5_data *pdata)
 #endif
 }
 
+/*
+ * @brief copy a buffer into a krb5_data struct
+ *
+ * @param[in] p                        The krb5_data
+ * @param[in] data             The data to copy
+ * @param[in] length           The length of the data to copy
+ * @return krb5_error_code
+ *
+ * Caller has to free krb5_data with kerberos_free_data_contents().
+ */
+
+krb5_error_code krb5_copy_data_contents(krb5_data *p,
+                                       const void *data,
+                                       size_t len)
+{
+#if defined(HAVE_KRB5_DATA_COPY)
+       return krb5_data_copy(p, data, len);
+#else
+       if (len) {
+               p->data = malloc(len);
+               if (p->data == NULL) {
+                       return ENOMEM;
+               }
+               memmove(p->data, data, len);
+       } else {
+               p->data = NULL;
+       }
+       p->length = len;
+       p->magic = KV5M_DATA;
+       return 0;
+#endif
+}
+
 /*
   get a kerberos5 ticket for the given service
 */
@@ -2264,22 +2297,68 @@ krb5_error_code smb_krb5_make_pac_checksum(TALLOC_CTX *mem_ctx,
  * @param[in] principal                The principal
  * @return pointer to the realm
  *
+ * Caller must free if the return value is not NULL.
+ *
  */
 
 char *smb_krb5_principal_get_realm(krb5_context context,
-                                  krb5_principal principal)
+                                  krb5_const_principal principal)
 {
 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
-       return discard_const_p(char, krb5_principal_get_realm(context, principal));
+       return strdup(discard_const_p(char, krb5_principal_get_realm(context, principal)));
 #elif defined(krb5_princ_realm) /* MIT */
        krb5_data *realm;
        realm = krb5_princ_realm(context, principal);
-       return discard_const_p(char, realm->data);
+       return strndup(realm->data, realm->length);
+#else
+#error UNKNOWN_GET_PRINC_REALM_FUNCTIONS
+#endif
+}
+
+/*
+ * smb_krb5_principal_set_realm
+ *
+ * @brief Get realm of a principal
+ *
+ * @param[in] context          The krb5_context
+ * @param[in] principal                The principal
+ * @param[in] realm            The realm
+ * @return                     0 on success, a krb5_error_code on error.
+ *
+ */
+
+krb5_error_code smb_krb5_principal_set_realm(krb5_context context,
+                                            krb5_principal principal,
+                                            const char *realm)
+{
+#ifdef HAVE_KRB5_PRINCIPAL_SET_REALM /* Heimdal */
+       return krb5_principal_set_realm(context, principal, realm);
+#elif defined(krb5_princ_realm) && defined(krb5_princ_set_realm) /* MIT */
+       krb5_error_code ret;
+       krb5_data data;
+       krb5_data *old_data;
+
+       old_data = krb5_princ_realm(context, principal);
+
+       ret = krb5_copy_data_contents(&data,
+                                     realm,
+                                     strlen(realm));
+       if (ret) {
+               return ret;
+       }
+
+       /* free realm before setting */
+       free(old_data->data);
+
+       krb5_princ_set_realm(context, principal, &data);
+
+       return ret;
 #else
-       return NULL;
+#error UNKNOWN_PRINC_SET_REALM_FUNCTION
 #endif
 }
 
+
 /************************************************************************
  Routine to get the default realm from the kerberos credentials cache.
  Caller must free if the return value is not NULL.
@@ -2495,6 +2574,49 @@ krb5_boolean smb_krb5_get_allowed_weak_crypto(krb5_context context)
 #error UNKNOWN_KRB5_CONFIG_ROUTINES
 #endif
 
+/**
+* @brief Return the type of a krb5_principal
+*
+* @param context       The krb5_context
+* @param principal     The const krb5_principal
+*
+* @return integer type of the principal
+*/
+int smb_krb5_principal_get_type(krb5_context context,
+                               krb5_const_principal principal)
+{
+#ifdef HAVE_KRB5_PRINCIPAL_GET_TYPE /* Heimdal */
+       return krb5_principal_get_type(context, principal);
+#elif defined(krb5_princ_type) /* MIT */
+       return krb5_princ_type(context, principal);
+#else
+#error UNKNOWN_PRINC_GET_TYPE_FUNCTION
+#endif
+}
+
+/**
+* @brief Generate a krb5 warning, forwarding to com_err
+*
+* @param context       The krb5_context
+* @param fmt           The message format
+* @param ...           The message arguments
+*
+* @return
+*/
+#if !defined(HAVE_KRB5_WARNX)
+krb5_error_code krb5_warnx(krb5_context context, const char *fmt, ...)
+{
+       va_list args;
+
+       va_start(args, fmt);
+       DEBUG(1,(fmt, args));
+       DEBUGADD(1,("\n"));
+       va_end(args);
+
+       return 0;
+}
+#endif
+
 #else /* HAVE_KRB5 */
  /* this saves a few linking headaches */
  int cli_krb5_get_ticket(TALLOC_CTX *mem_ctx,