/* xlogin-makeidentity.c	

	Free Software, Use at own risk.

	This code is designed to be fast with very few dependencies
	(c)2008 Jonathan Andrews

	Produce an encrypted file (called an identity) containging a username and password.
	The file can be used by xlogin for automatic logins

	Be warned this is only marginally better than passing the password on the commaand line.
	The identity file is encrypted with a static key thats readable in this source, users
	are reccomended to change the key.
*/


char PROGNAME[80]="xlogin-makeidentity\0";
char LASTCHANGED[80]="19 April 2015\0";
#define VERSION		"1.1"

#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <crypt.h>
#include "xlogin.h"
#include "key.h"

char usernamebuf[MAXUSERNAMESIZE*2];
char passwordbuf[MAXPASSWORDSIZE*2];
char filenamebuf[FILEPATHLENGTH];

void remove_newline(char *s) {
	if (!s) return;
	int len = strlen(s);
	if (!len) return;
	if (s[len-1]=='\n') s[len-1]=0;
}

int main (int argc, char *argv[])
{
	char *username = 0;
	char *password = 0;
	char *filename = 0;
	char st[512];
	int i;
	
	// process args
	for (i=1; i<argc; i++) {
		// help
		if (strcmp(argv[i],"-h")==0 ||
		    strcmp(argv[i],"-help")==0 ||
		    strcmp(argv[i],"--help")==0) 
		{
			fprintf(stderr,"%s: <option list>\n",PROGNAME);
			fprintf(stderr,"-user <name>\t Username for this identity\n");
			fprintf(stderr,"-password\t Password for this identity\n");
			fprintf(stderr,"-file\t\t Destination filename, default is "
			               DEFAULT_CONFIG_PATH "/identity/username.id\n");
			exit(0);
		}
		// username
		else if (strcmp(argv[i],"-u")==0 || 
		         strcmp(argv[i],"-user")==0 || 
		         strcmp(argv[i],"-username")==0)
		{
			i++; username=argv[i];
		}
		// password
		else if (strcmp(argv[i],"-p")==0 || 
		         strcmp(argv[i],"-pass")==0 || 
		         strcmp(argv[i],"-password")==0)
		{
			i++; password=argv[i];
		}
		// filename
		else if (strcmp(argv[i],"-f")==0 ||
		         strcmp(argv[i],"-file")==0 || 
		         strcmp(argv[i],"-filename")==0)
		{
			i++; filename=argv[i];
		}		
	}

	// if username not supplied, ask
	if (!username) {
		fprintf(stderr,"Username: ");
		fgets(usernamebuf,sizeof(usernamebuf), stdin);
		username = usernamebuf;
		remove_newline(username);
	}
	
	// if password not supplied, ask
	if (!password) {
		fprintf(stderr,"Password: ");
		fgets(passwordbuf,sizeof(passwordbuf), stdin);
		password = passwordbuf;
		remove_newline(password);
	}
	
	// if filename not given, use default
	if (!filename) {
		snprintf(filenamebuf, sizeof(filenamebuf), DEFAULT_CONFIG_PATH "/identity/%s.id",username);
		filename = filenamebuf;
		remove_newline(filename);
	}
	//fprintf(stderr,"username: %s password: %s file:%s \n", username,password,filename);	
		
	// Sanity checks - username cannot be empty
	if (strlen(username)<1)
	{
		fprintf(stderr,"username must be at least one character\n");
		exit(1);
	}

	// do it
	weakencrypt(username,password,filename);
	fprintf(stderr, "Created identity file %s \n",filename);
}
