Skip to content

Commit

Permalink
netcmds.h, netuser.{c,h}, PyNUT.py.in: deprecate protocol "MASTER" co…
Browse files Browse the repository at this point in the history
…mmand in favor of "PRIMARY" [issue networkupstools#840]
  • Loading branch information
jimklimov committed Mar 11, 2022
1 parent e8400e1 commit b9e3f6c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
15 changes: 10 additions & 5 deletions scripts/python/module/PyNUT.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,22 @@ Returns OK on success or raises an error

Returns OK on success or raises an error

TODO: API change pending to replace MASTER with PRIMARY
NOTE: API changed since NUT 2.8.0 to replace MASTER with PRIMARY
(and backwards-compatible alias handling)
"""

if self.__debug :
print( "[DEBUG] MASTER called..." )
print( "[DEBUG] "PRIMARY called..." )

self.__srv_handler.write( ("MASTER %s\n" % ups).encode('ascii') )
self.__srv_handler.write( ("PRIMARY %s\n" % ups).encode('ascii') )
result = self.__srv_handler.read_until( b"\n" )
if ( result != b"OK MASTER-GRANTED\n" ) :
raise PyNUTError( ( "Master level function are not available", "" ) )
if ( result != b"OK PRIMARY-GRANTED\n" ) :
if self.__debug :
print( "[DEBUG] Retrying: "MASTER called..." )
self.__srv_handler.write( ("MASTER %s\n" % ups).encode('ascii') )
result = self.__srv_handler.read_until( b"\n" )
if ( result != b"OK MASTER-GRANTED\n" ) :
raise PyNUTError( ( "Primary level functions are not available", "" ) )

if self.__debug :
print( "[DEBUG] FSD called..." )
Expand Down
5 changes: 3 additions & 2 deletions server/netcmds.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ static struct {

{ "LOGIN", net_login, FLAG_USER },
{ "LOGOUT", net_logout, 0 },
/* FIXME: Protocol update needed to handle master/primary alias
* and probably an API bump also, to rename/alias the routine.
/* NOTE: Protocol in NUT 2.8.0 allows to handle
* master/primary to rename/alias the routine.
*/
{ "PRIMARY", net_primary, FLAG_USER },
{ "MASTER", net_master, FLAG_USER },

{ "FSD", net_fsd, FLAG_USER },
Expand Down
40 changes: 30 additions & 10 deletions server/netuser.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,34 +85,54 @@ void net_logout(nut_ctype_t *client, size_t numarg, const char **arg)
client->last_heard = 0;
}

/* MASTER <upsname> */
/* FIXME: Protocol update needed to handle master/primary alias
* and probably an API bump also, to rename/alias the routine.
/* NOTE: Protocol updated since NUT 2.8.0 to handle master/primary
* and API bumped, to rename/alias the routine.
*/
void net_master(nut_ctype_t *client, size_t numarg, const char **arg)
static int do_net_primary(nut_ctype_t *client, size_t numarg, const char **arg)
{
upstype_t *ups;

if (numarg != 1) {
send_err(client, NUT_ERR_INVALID_ARGUMENT);
return;
return -1;
}

ups = get_ups_ptr(arg[0]);

if (!ups) {
send_err(client, NUT_ERR_UNKNOWN_UPS);
return;
return -1;
}

/* make sure this user is allowed to do MASTER */
if (!user_checkaction(client->username, client->password, "MASTER")) {
/* make sure this user is allowed to do PRIMARY or MASTER */
if (!user_checkaction(client->username, client->password, "PRIMARY")
&& !user_checkaction(client->username, client->password, "MASTER")
) {
send_err(client, NUT_ERR_ACCESS_DENIED);
return;
return -1;
}

/* this is just an access level check */
sendback(client, "OK MASTER-GRANTED\n");
/* sendback() will be worded by caller below */
return 0;
sendback(client, "OK PRIMARY-GRANTED\n");
}

/* MASTER <upsname> (deprecated) */
void net_master(nut_ctype_t *client, size_t numarg, const char **arg) {
/* Allow existing binaries linked against this file to still work */
upsdebugx(1, "WARNING: net_master() is deprecated in favor of net_primary() since NUT 2.8.0");
if (0 == do_net_primary(client, numarg, arg)) {
sendback(client, "OK MASTER-GRANTED\n");
}
}

/* PRIMARY <upsname> (since NUT 2.8.0) */
void net_primary(nut_ctype_t *client, size_t numarg, const char **arg)
{
if (0 == do_net_primary(client, numarg, arg)) {
sendback(client, "OK PRIMARY-GRANTED\n");
}
}

/* USERNAME <username> */
Expand Down
8 changes: 6 additions & 2 deletions server/netuser.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ extern "C" {

void net_login(nut_ctype_t *client, size_t numarg, const char **arg);
void net_logout(nut_ctype_t *client, size_t numarg, const char **arg);
/* FIXME: Protocol update needed to handle master/primary alias
* and probably an API bump also, to rename/alias the routine.

/* NOTE: Since NUT 2.8.0 we handle master as alias for primary
* Header keyword kept for building older consumers, but
* the implementation will warn that it is deprecated.
*/
void net_master(nut_ctype_t *client, size_t numarg, const char **arg);
void net_primary(nut_ctype_t *client, size_t numarg, const char **arg);

void net_username(nut_ctype_t *client, size_t numarg, const char **arg);
void net_password(nut_ctype_t *client, size_t numarg, const char **arg);

Expand Down

0 comments on commit b9e3f6c

Please sign in to comment.