/* 
   authentication client for podfuk-smb

   Copyright (C) Tim Potter 2000
   
   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
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   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.
*/

#ifndef PODFUK_SMB_AUTH_H
#define PODFUK_SMB_AUTH_H

#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>

#define PODFUK_SOCKET_DIR  "/tmp/.podfuk-smb"  /* Name of PF_UNIX dir */
#define PODFUK_SOCKET_NAME "pipe"              /* Name of PF_UNIX socket */

#define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
#define ZERO_STRUCTP(x) { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); }

// #define DEBUG(body) (void)0
#define DEBUG(body) printf body

#define False 0
#define True 1

/* Linked list stuff from samba/source/include/dlinklist.h */

#define DLIST_ADD(list, p) \
{ \
        if (!(list)) { \
		(list) = (p); \
		(p)->next = (p)->prev = NULL; \
	} else { \
		(list)->prev = (p); \
		(p)->next = (list); \
		(p)->prev = NULL; \
		(list) = (p); \
	}\
}

/* remove an element from a list */
#define DLIST_REMOVE(list, p) \
{ \
	if ((p) == (list)) { \
		(list) = (p)->next; \
		if (list) (list)->prev = NULL; \
	} else { \
		(p)->prev->next = (p)->next; \
		if ((p)->next) (p)->next->prev = (p)->prev; \
	} \
}

enum auth_cmd {

	/* client commands */

	AUTH_ADD_TOKEN,                  /* Set username/password */
	AUTH_REMOVE_TOKEN,               /* Clear username/password */
	AUTH_CLEAR_TOKENS,               /* Clear all username/passwords */
	AUTH_LIST_TOKENS,                /* List tokens for this user */

	/* podfuk-smb (or root) commands */

	AUTH_FETCH_TOKEN,                 /* Get username/password */
	AUTH_LIST_ALL_TOKENS,             /* List all tokens available */
	AUTH_CLEAR_ALL_TOKENS,            /* Clear all username/passwords */
};

enum auth_result {
	AUTH_CMD_OK,
	AUTH_CMD_FAILED,
};	

/*
 * Client request/response structures 
 */

#include "podfuk-smb-auth-cli.h"

struct cli_request {

	/* Header stuff */

	int pid;                    /* Client pid (insecure) */
	int command;                /* Command */

	/* Request data */

	union {

		/* AUTH_CLEAR_{ALL_}TOKEN{S} */

		struct {
			fstring sharename;
		} clear_token;

		/* AUTH_FETCH_TOKEN */

		struct {
			uid_t uid;
			fstring servername;
			fstring sharename;
		} fetch_token;

		/* AUTH_ADD_TOKEN */

		struct auth_token add_token;
	} data;
};

struct cli_response {

	/* Header stuff */

	int pid;                    /* Server pid (insecure) */
	int result;                 /* What happened */

	/* Fixed length response data */

 	/* Variable length response data */

	int extra_data_len;
	char *extra_data;
};

/* smbw function prototypes */

int smbw_open(const char *fname, int flags, mode_t mode);
ssize_t smbw_read(int fd, void *buf, size_t count);
ssize_t smbw_write(int fd, void *buf, size_t count);
int smbw_close(int fd);
int smbw_stat(const char *fname, struct stat *st);
DIR *smbw_opendir(const char *fname);
struct dirent *smbw_readdir(DIR *dirp);
int smbw_closedir(DIR *dirp);
void smbw_setup_shared(void);
void smbw_setshared(const char *name, const char *val);
int smbw_chmod(const char *fname, mode_t newmode);
int smbw_access(const char *name, int mode);
int smbw_mkdir(const char *fname, mode_t mode);
int smbw_rmdir(const char *fname);
int smbw_unlink(const char *fname);
int smbw_rename(const char *oldname, const char *newname);

typedef void (*smbw_get_auth_data_fn)(char *server, char *share,
				      char **workgroup, char **username,
				      char **password);

void smbw_set_auth_data_fn(smbw_get_auth_data_fn fn);

/* Misc other prototypes necessary for a happy compile */

void charset_initialise(void);
void strlower(char *s);
int lp_load(char *fname, int global_only, int save_defaults, int add_ipc);
void codepage_initialise(int client_codepage);
int lp_client_code_page(void);
char *lp_workgroup(void);

#endif /* PODFUK_SMB_AUTH_H */
