s3-messages: only include messages.h where needed.
[amitay/samba.git] / source3 / lib / afs.c
index b8173f7cc1e0c8712d15ae1a90d3e11a6f5191ae..d73e9df4c0eb16c5d3da242809eae94623313520 100644 (file)
@@ -5,7 +5,7 @@
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation; either version 3 of the License, or
  *  (at your option) any later version.
  *  
  *  This program is distributed in the hope that it will be useful,
  *  GNU General Public License for more details.
  *  
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "includes.h"
 
 #ifdef WITH_FAKE_KASERVER
 
+#define NO_ASN1_TYPEDEFS 1
+
+#include <afs/param.h>
 #include <afs/stds.h>
 #include <afs/afs.h>
 #include <afs/auth.h>
@@ -41,20 +43,23 @@ static char *afs_encode_token(const char *cell, const DATA_BLOB ticket,
                              const struct ClearToken *ct)
 {
        char *base64_ticket;
-       char *result;
+       char *result = NULL;
 
        DATA_BLOB key = data_blob(ct->HandShakeKey, 8);
        char *base64_key;
+       TALLOC_CTX *mem_ctx;
+
+       mem_ctx = talloc_stackframe();
+       if (mem_ctx == NULL)
+               goto done;
 
-       base64_ticket = base64_encode_data_blob(ticket);
+       base64_ticket = base64_encode_data_blob(mem_ctx, ticket);
        if (base64_ticket == NULL)
-               return NULL;
+               goto done;
 
-       base64_key = base64_encode_data_blob(key);
-       if (base64_key == NULL) {
-               free(base64_ticket);
-               return NULL;
-       }
+       base64_key = base64_encode_data_blob(mem_ctx, key);
+       if (base64_key == NULL)
+               goto done;
 
        asprintf(&result, "%s\n%u\n%s\n%u\n%u\n%u\n%s\n", cell,
                 ct->AuthHandle, base64_key, ct->ViceId, ct->BeginTimestamp,
@@ -62,8 +67,8 @@ static char *afs_encode_token(const char *cell, const DATA_BLOB ticket,
 
        DEBUG(10, ("Got ticket string:\n%s\n", result));
 
-       free(base64_ticket);
-       free(base64_key);
+done:
+       TALLOC_FREE(mem_ctx);
 
        return result;
 }
@@ -71,7 +76,7 @@ static char *afs_encode_token(const char *cell, const DATA_BLOB ticket,
 /* Create a ClearToken and an encrypted ticket. ClearToken has not yet the
  * ViceId set, this should be set by the caller. */
 
-static BOOL afs_createtoken(const char *username, const char *cell,
+static bool afs_createtoken(const char *username, const char *cell,
                            DATA_BLOB *ticket, struct ClearToken *ct)
 {
        fstring clear_ticket;
@@ -207,23 +212,42 @@ char *afs_createtoken_str(const char *username, const char *cell)
   For the comments "Alice" is the User to be auth'ed, and "Bob" is the
   AFS server.  */
 
-BOOL afs_login(connection_struct *conn)
+bool afs_login(connection_struct *conn)
 {
-       extern struct current_user current_user;
        DATA_BLOB ticket;
-       pstring afs_username;
-       char *cell;
-       BOOL result;
-       char *ticket_str;
-       const DOM_SID *user_sid;
+       char *afs_username = NULL;
+       char *cell = NULL;
+       bool result;
+       char *ticket_str = NULL;
+       const struct dom_sid *user_sid;
+       TALLOC_CTX *ctx = talloc_tos();
 
        struct ClearToken ct;
 
-       pstrcpy(afs_username, lp_afs_username_map());
-       standard_sub_conn(conn, afs_username, sizeof(afs_username));
+       afs_username = talloc_strdup(ctx,
+                               lp_afs_username_map());
+       if (!afs_username) {
+               return false;
+       }
 
-       user_sid = &current_user.nt_user_token->user_sids[0];
-       pstring_sub(afs_username, "%s", sid_string_static(user_sid));
+       afs_username = talloc_sub_advanced(ctx,
+                               SNUM(conn), conn->session_info->unix_name,
+                               conn->connectpath, conn->session_info->utok.gid,
+                               conn->session_info->sanitized_username,
+                               pdb_get_domain(conn->session_info->sam_account),
+                               afs_username);
+       if (!afs_username) {
+               return false;
+       }
+
+       user_sid = &conn->session_info->security_token->user_sids[0];
+       afs_username = talloc_string_sub(talloc_tos(),
+                                       afs_username,
+                                       "%s",
+                                       sid_string_tos(user_sid));
+       if (!afs_username) {
+               return false;
+       }
 
        /* The pts command always generates completely lower-case user
         * names. */
@@ -234,13 +258,13 @@ BOOL afs_login(connection_struct *conn)
        if (cell == NULL) {
                DEBUG(1, ("AFS username doesn't contain a @, "
                          "could not find cell\n"));
-               return False;
+               return false;
        }
 
        *cell = '\0';
        cell += 1;
 
-       DEBUG(10, ("Trying to log into AFS for user %s@%s\n", 
+       DEBUG(10, ("Trying to log into AFS for user %s@%s\n",
                   afs_username, cell));
 
        if (!afs_createtoken(afs_username, cell, &ticket, &ct))
@@ -262,14 +286,14 @@ BOOL afs_login(connection_struct *conn)
 
 #else
 
-BOOL afs_login(connection_struct *conn)
+bool afs_login(connection_struct *conn)
 {
        return True;
 }
 
 char *afs_createtoken_str(const char *username, const char *cell)
 {
-       return False;
+       return NULL;
 }
 
 #endif /* WITH_FAKE_KASERVER */