Replace sid_string_static by sid_string_dbg in DEBUGs
[amitay/samba.git] / source3 / lib / afs.c
index 5ff027ee01d50f98d563d08b80d68377ecc879b2..a7d6f6c9f7fecd6df62aa8be1a4e3099a5120a82 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/stds.h>
 #include <afs/afs.h>
 #include <afs/auth.h>
@@ -52,7 +53,7 @@ static char *afs_encode_token(const char *cell, const DATA_BLOB ticket,
 
        base64_key = base64_encode_data_blob(key);
        if (base64_key == NULL) {
-               free(base64_ticket);
+               TALLOC_FREE(base64_ticket);
                return NULL;
        }
 
@@ -62,8 +63,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);
+       TALLOC_FREE(base64_ticket);
+       TALLOC_FREE(base64_key);
 
        return result;
 }
@@ -71,7 +72,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,18 +208,44 @@ 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 userdom_struct current_user_info;
+       extern struct current_user current_user;
        DATA_BLOB ticket;
-       pstring afs_username;
-       char *cell;
-       BOOL result;
-       char *ticket_str;
+       char *afs_username = NULL;
+       char *cell = NULL;
+       bool result;
+       char *ticket_str = NULL;
+       const 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;
+       }
+
+       afs_username = talloc_sub_advanced(ctx,
+                               SNUM(conn), conn->user,
+                               conn->connectpath, conn->gid,
+                               get_current_username(),
+                               current_user_info.domain,
+                               afs_username);
+       if (!afs_username) {
+               return false;
+       }
+
+       user_sid = &current_user.nt_user_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. */
@@ -229,13 +256,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))
@@ -257,7 +284,7 @@ BOOL afs_login(connection_struct *conn)
 
 #else
 
-BOOL afs_login(connection_struct *conn)
+bool afs_login(connection_struct *conn)
 {
        return True;
 }