Perforce log in as someone else - perforce

In the doc's I'm reading: To log in to Perforce automatically, you can save your password in a text file, and redirect p4 login to read from that file. For example, if you saved your password to a file called password.txt, the command would be:
Command:
p4 login < password.txt
However I've created a new local account using the following commands:
p4 user -f test
p4 passwd perforce-bot
When I now wan't to login with p4 login test < password.txt it complains about "Perforce password (P4PASSWD) invalid or unset".
How should I understand that? This operation works fine if I first do a p4 login (I'm an admin) and then run that snippet, it basically feels like sudo'ing as someone else, but I would want to use it without the need to login as my user first so I can run it as a cronjob on any machine.

These are two different commands:
p4 login: authenticates the current user ($P4USER) by prompting for a password
p4 login USER: authenticates an arbitrary other user IF already authenticated as a super user
Note also that if you run p4 passwd perforce-bot, you're setting the password for a user called perforce-bot (again, this is an admin command).
To temporarily switch the current user without resetting $P4USER, use the -u global flag:
p4 -u test user # creates a new user called "test"
p4 -u test passwd # sets the password for "test"
p4 -u test login # login "test" via stdin password prompt
p4 -u test <any command> # run any other command as "test"

Related

How to automatically pass my password in multiple password prompts in command line?

I'm using ubuntu and i want to pass my password in multiple password prompts in single command only. I know how to deal with single password prompt.
For example:
echo password | sudo -S MyCommand;
But when i'm runing this command then it will ask for new password and confirm password:
echo password | sudo -S htpasswd -m /home/user1/svn.passwd UserName
Q. How to respond to multiple password prompts in single command just like we respond to single sudo password prompt?

Ubuntu - Switch to root user from a script file without prompting for password

I am new to Linux. I want to switch to root user from a script file without prompting for password. I am writing this line
echo 'password' | sudo -S su
above runs nicely but does not change user to root user. I have seen that -S does not change user so i tried -i as well but then it prompts for password. Please help

Missing command prompt user name when a user account is created - Linux - Debian?

I am writing a bash script to create user account with password, that will expire. After creating the user account and then login using su - , I get the prompt, but the user id is missing in the prompt. Also, the tab auto complete is missing. Here is my bash script. Remember, I am using Debian 9 in Windows - WSL2.
#!/bin/bash
# This script creates an account on the local system.
# You will be prompted for the account name and password.
# Ask for the user name.
read -p 'Enter the username to create: ' USER_NAME
# Ask for the real name.
read -p 'Enter the name of the person who this account is for: ' COMMENT
# Ask for the password
read -p 'Enter the password to use for the account: ' PASSWORD
# Create the user
useradd -c "${COMMENT}" -m ${USER_NAME}
# Set the password for the user.
# echo ${PASSWORD} | passwd --stdin ${USER_NAME}
echo "${USER_NAME}:${PASSWORD}" | chpasswd
# Force password change on first login.
passwd -e ${USER_NAME}
After running this, I get a prompt which doesn't has a user-id in it on the left side. Also, the auto completion using tab isn't working. I am a bit surprised, am I doing something wrong here?
Here is what I am seeing.
Add a user with adduser command instead of useradd.
Inscript always user adduser.
tested the same script on the Debian box and it's working fine.

Perforce: run script as particular user and workspace

I have a powershell script that does various things, including running p4 commands such as syncing, creating a changelist and checking files into it, create a label etc. My script works fine when I run it on my development environment as my user.
Now I would like to run the script on another machine, against a specific workspace, and a specific Perforce user. Is there some set of commands I can add to the beginning of my script so that I can set the perforce user and use a specific workspace for the remainder of the script? If yes, I'd like to know 2 ways of doing this, one with the password in plain text (for testing and verifying), and one without it (for production use).
Releated: I think part of my problem is that I don't fully understand how the session user is determined. On the other machine, if I try to run any p4 command, I get the message: "Perforce password (P4PASSWD) is invalid or unset." I got that message even if I try:
p4 login abc
"abc" can be anything and I get the same message. I must be wrong in thinking that the "login" command can be used to login as a particular user. I was expecting it to prompt me for abc's password, rather than telling me I need to set a password before I can login as someone else.
But if I type in this:
p4 login
I am prompted to enter in a password. But for what user would that be? My Windows account user? What if I don't have a perforce user with the same name as my windows account user?
This is the doc page you need:
http://www.perforce.com/perforce/doc.current/manuals/cmdref/envars.html
In general, this should do the trick (with the current version of the command line client that supports "p4 set" on all platforms):
p4 set P4USER=username
p4 set P4CLIENT=clientname
p4 login
The "p4 login USERNAME" syntax is used when you've already logged in as a super user to get a login ticket for a different user (with no password prompt). Like all other commands, "p4 login" uses your P4USER to determine who you're running the command as.
To Auto login via powershell, login -a means allow login from any ip...
$USER = "My.User"
$PASS = "P455w0rd"
Out-File -FilePath C:\p4pass.txt -InputObject $PASS -Encoding ASCII -Width 50
cmd /c "p4 -u $USER login -a < `"C:\p4pass.txt`" " 2>&1
To specify the user, workspace and server:port append the below before the perforce command... e.g:
p4 -u ${USER} -c ${WORKSPACE} -p ${perforce:1666} login -a
p4 -u ${USER} -c ${WORKSPACE} -p ${perforce:1666} sync -f -q //...#head
To specify a ticket, get the ticket from the command:
p4 tickets
and specify it with an upper case -P, e.g:
p4 -u username -P 5E8ED75FB5086BE82D9BCD5561D32AEE sync...

How to prevent a user to log in, but allowing to change his password?

I want to have a database user on my RHEL6 server. Since a DB user is just a DB user, I don't want that user be able to login to shell. I know I can define the user as nologin but this way the user won't be able to change his/her password either.
So how can I prevent user to login, in the same time allowing his/her to change password?
Use IPA for centralized user management. It allows users to change passwords even if they cannot login.
I have figured out a quick and dirty way to do it.
First, user should have right to login.
Then, run these commands:
$ sudo useradd -m -d /home/username -s /bin/bash -c "login is forbidden for this user" username -N -g users
$ chown root:users /home/username
$ chmod 555 /home/username
$ echo "trap '' 2" >> /home/username/.bash_profile
$ echo "passwd" >> /home/username/.bash_profile
$ echo "logout" >> /home/username/.bash_profile
$ passwd username
Now the user 'username' can login to change the password. Then logs out right after changing the password. He/she can't do anything other than changing his/her password.
CTRL+C is also blocked.

Resources