Example #1
0
void notify_except_rlevel
(
    dbref loc,
    dbref player,
    dbref exception,
    const UTF8 *msg,
    int xflags
)
{
    if (  loc != exception
       && IsReal(loc, player))
    {
        notify_check(loc, player, msg,
            (MSG_ME_ALL | MSG_F_UP | MSG_S_INSIDE | MSG_NBR_EXITS_A| xflags));
    }

    dbref first;
    DOLIST(first, Contents(loc))
    {
        if (  first != exception
           && IsReal(first, player))
        {
            notify_check(first, player, msg,
                (MSG_ME | MSG_F_DOWN | MSG_S_OUTSIDE | xflags));
        }
    }
}
Example #2
0
void notify_except2_rlevel2
(
    dbref loc,
    dbref player,
    dbref exc1,
    dbref exc2,
    const UTF8 *msg
)
{
    if (  loc != exc1
       && loc != exc2
       && IsReal(loc, player))
    {
        notify_check(loc, player, msg,
            (MSG_ME_ALL | MSG_F_UP | MSG_S_INSIDE | MSG_NBR_EXITS_A));
    }

    dbref first;
    DOLIST(first, Contents(loc))
    {
        if (  first != exc1
           && first != exc2
           && IsReal(first, player)
           && IsReal(first, exc2))
        {
            notify_check(first, player, msg,
                (MSG_ME | MSG_F_DOWN | MSG_S_OUTSIDE));
        }
    }
}
Example #3
0
u_int32_t
FF_validate(void *c, char *v)
{
	agent_private *ap;
	int n;
	u_int32_t ts, status, check;
	struct stat st;
	char fpath[MAXPATHLEN + 1];

	if (c == NULL) return 0;
	if (v == NULL) return 0;

	ap = (agent_private *)c;

	n = sscanf(v, "%s %u", fpath, &ts);
	if (n != 2) return 0;

	if ((!strcmp(fpath, "/etc/hosts")) && (host_cache.notify_token != -1))
	{
		check = 1;
		status = notify_check(host_cache.notify_token, &check);
		if ((status == NOTIFY_STATUS_OK) && (check == 0)) return 1;
		host_cache.modtime = 0;
	}

	if (stat(fpath, &st) < 0) return 0;
	if (ts == st.st_mtime) return 1;

	return 0;
}
Example #4
0
void decompile_flags( dbref player, dbref thing, char *thingname ) {
    FLAG f1, f2, f3;

    FLAGENT *fp;

    /*
     * Report generic flags
     */

    f1 = Flags( thing );
    f2 = Flags2( thing );
    f3 = Flags3( thing );

    for( fp = gen_flags; fp->flagname; fp++ ) {

        /*
         * Skip if we shouldn't decompile this flag
         */

        if( fp->listperm & CA_NO_DECOMP ) {
            continue;
        }

        /*
         * Skip if this flag is not set
         */

        if( fp->flagflag & FLAG_WORD3 ) {
            if( !( f3 & fp->flagvalue ) ) {
                continue;
            }
        } else if( fp->flagflag & FLAG_WORD2 ) {
            if( !( f2 & fp->flagvalue ) ) {
                continue;
            }
        } else {
            if( !( f1 & fp->flagvalue ) ) {
                continue;
            }
        }

        /*
         * Skip if we can't see this flag
         */

        if( !check_access( player, fp->listperm ) ) {
            continue;
        }

        /*
         * We made it this far, report this flag
         */

        notify_check( player, player, MSG_PUP_ALWAYS|MSG_ME_ALL|MSG_F_DOWN, "@set %s=%s", strip_ansi( thingname ), fp->flagname );
    }
}
Example #5
0
void decompile_powers(dbref player, dbref thing, char *thingname)
{
    POWER f1, f2;
    POWERENT *fp;
    char *buf;
    /*
     * Report generic powers
     */
    f1 = Powers(thing);
    f2 = Powers2(thing);

    for (fp = gen_powers; fp->powername; fp++) {
	/*
	 * Skip if we shouldn't decompile this power
	 */
	if (fp->listperm & CA_NO_DECOMP) {
	    continue;
	}

	/*
	 * Skip if this power is not set
	 */

	if (fp->powerpower & POWER_EXT) {
	    if (!(f2 & fp->powervalue)) {
		continue;
	    }
	} else {
	    if (!(f1 & fp->powervalue)) {
		continue;
	    }
	}

	/*
	 * Skip if we can't see this power
	 */

	if (!check_access(player, fp->listperm)) {
	    continue;
	}

	/*
	 * We made it this far, report this power
	 */
	buf = strip_ansi(thingname);
	notify_check(player, player, MSG_PUP_ALWAYS | MSG_ME_ALL | MSG_F_DOWN, "@power %s=%s", buf, fp->powername);
	free_lbuf(buf);
    }
}
static void
timer_notify_check(void)
{
    int		check = 0;
    int		status;

    if (S_time_change_registered == FALSE) {
	return;
    }
    status = notify_check(S_time_change_token, &check);
    if (status != NOTIFY_STATUS_OK) {
	my_log(LOG_ERR, "timer: notify_check failed with %d", status);
    }
    else if (check != 0) {
	S_time_generation++;
    }
    return;
}
Example #7
0
int decode_power(dbref player, char *powername, POWERSET * pset)
{
    POWERENT *pent;
    pset->word1 = 0;
    pset->word2 = 0;
    pent = (POWERENT *) hashfind(powername, &mudstate.powers_htab);

    if (!pent) {
	notify_check(player, player, MSG_PUP_ALWAYS | MSG_ME_ALL | MSG_F_DOWN, "%s: Power not found.", powername);
	return 0;
    }

    if (pent->powerpower & POWER_EXT) {
	pset->word2 = pent->powervalue;
    } else {
	pset->word1 = pent->powervalue;
    }

    return 1;
}
Example #8
0
/*
 * Get the difference in milliseconds between this time zone and UTC (GMT)
 * NOT including DST.
 */
double getUTCOffset()
{
#if PLATFORM(DARWIN)
    if (s_haveCachedUTCOffset) {
        int notified;
        uint32_t status = notify_check(s_notificationToken, &notified);
        if (status == NOTIFY_STATUS_OK && !notified)
            return s_cachedUTCOffset;
    }
#endif

    int32_t utcOffset = calculateUTCOffset();

#if PLATFORM(DARWIN)
    // Theoretically, it is possible that several threads will be executing this code at once, in which case we will have a race condition,
    // and a newer value may be overwritten. In practice, time zones don't change that often.
    s_cachedUTCOffset = utcOffset;
#endif

    return utcOffset;
}
Example #9
0
static ff_cache_t *
prep_cache(int cat, ff_cache_t *cache)
{
	u_int32_t status, check;
	char *s;
	ff_cache_t *c;

	if (cache == NULL) return NULL;

	if (cache->notify_token == -1)
	{
		s = NULL;
		asprintf(&s, "%s.%s", NOTIFY_PREFIX, categoryFilename[cat]);
		status = notify_register_check(s, &(cache->notify_token));
		free(s);
		if (status != NOTIFY_STATUS_OK) return NULL;

		s = NULL;
		asprintf(&s, "%s/%s", DEFAULT_FF_DIR, categoryFilename[cat]);
		status = notify_monitor_file(cache->notify_token, s, 0);
		free(s);
		if (status != NOTIFY_STATUS_OK) return NULL;
	}

	pthread_mutex_lock(&(cache->lock));
	check = 1;
	if (cache->modtime != 0) 
	{
		status = notify_check(cache->notify_token, &check);
		if ((status == NOTIFY_STATUS_OK) && (check == 0))
		{
			pthread_mutex_unlock(&(cache->lock));
			return cache;
		}
	}

	c = load_cache(cat, cache);
	pthread_mutex_unlock(&(cache->lock));
	return c;
}
STATIC Boolean
prefs_did_change(uint32_t * gen_p)
{
    Boolean		changed;
    int			check = 0;
    uint32_t		notify_status;
    STATIC uint32_t	S_generation;
    STATIC int		S_token;
    STATIC Boolean	S_token_valid = FALSE;

    changed = FALSE;
    if (!S_token_valid) {
	notify_status = 
	    notify_register_check(kEAPSIMAKAPrefsChangedNotification,
				  &S_token);
	if (notify_status != NOTIFY_STATUS_OK) {
	    EAPLOG_FL(LOG_NOTICE,
		      "notify_register_check returned %d",
		      notify_status);
	    goto done;
	}
	S_token_valid = TRUE;
    }
    notify_status = notify_check(S_token, &check);
    if (notify_status != NOTIFY_STATUS_OK) {
	EAPLOG_FL(LOG_NOTICE, "notify_check returned %d",
		  notify_status);
	goto done;
    }
    if (check != 0) {
	S_generation++;
    }
    if (*gen_p != S_generation) {
	changed = TRUE;
    }
    *gen_p = S_generation;

 done:
    return (changed);
}
Example #11
0
static krb5_boolean
_krb5_need_to_reload(krb5_context context)
{
#ifdef HAVE_NOTIFY_H
    int check = 0, ret;

    if (check_token == -1)
        return FALSE;

    ret = notify_check(check_token, &check);

    if (ret == NOTIFY_STATUS_OK && check) {
        context->last_config_update = last_update = time(NULL);
        return TRUE;
    }
    /* check if some other thread have checked the notify_check() before us */
    if (last_update > context->last_config_update) {
        context->last_config_update = last_update;
        return TRUE;
    }
#endif
    /* because of the perfomance penalty we don't reload unless we know its needed */
    return FALSE;
}
Example #12
0
__private_extern__ int
si_inet_config(uint32_t *inet4, uint32_t *inet6)
{
	int status, checkit;
	struct ifaddrs *ifa, *ifap;

	pthread_mutex_lock(&net_config_mutex);

	checkit = 1;
#if NOTIFY_SUPPORT
	if (net_config_token < 0)
	{
		status = notify_register_check(kNotifySCNetworkChange, &net_config_token);
		if (status != 0) net_config_token = -1;
	}

	if (net_config_token >= 0)
	{
		status = notify_check(net_config_token, &checkit);
		if (status != 0) checkit = 1;
	}
#endif

	status = 0;

	if (checkit != 0)
	{
		if (getifaddrs(&ifa) < 0)
		{
			status = -1;
		}
		else
		{
			net_v4_count = 0;
			net_v6_count = 0;

			for (ifap = ifa; ifap != NULL; ifap = ifap->ifa_next)
			{
				if (ifap->ifa_addr == NULL) continue;
				if ((ifap->ifa_flags & IFF_UP) == 0) continue;

				if (ifap->ifa_addr->sa_family == AF_INET)
				{
					net_v4_count++;
				}
				else if (ifap->ifa_addr->sa_family == AF_INET6)
				{
					net_v6_count++;
				}
			}
		}

		freeifaddrs(ifa);
	}

	if (inet4 != NULL) *inet4 = net_v4_count;
	if (inet6 != NULL) *inet6 = net_v6_count;

	pthread_mutex_unlock(&net_config_mutex);

	return status;
}
Example #13
0
void do_dbclean( dbref player, dbref cause, int key ) {
    VATTR *vp, *vpx;

    dbref i, end;

    int ca, n_oldtotal, n_oldtop, n_deleted, n_renumbered, n_objt, n_atrt,
        got;
    char *as, *str;

    int *used_table;

    ATTR **new_table;

    UFUN *ufp;

    CMDENT *cmdp;

    ADDENT *addp;

    raw_broadcast( 0, "GAME: Cleaning database. Game may freeze for a few minutes." );

    used_table = ( int * ) XCALLOC( mudstate.attr_next, sizeof( int ),
                                    "dbclean.used_table" );

    n_oldtotal = mudstate.attr_next;
    n_oldtop = anum_alc_top;
    n_deleted = n_renumbered = n_objt = n_atrt = 0;

    /*
     * Non-user-defined attributes are always considered used.
     */

    for( i = 0; i < A_USER_START; i++ ) {
        used_table[i] = i;
    }

    /*
     * Walk the database. Mark all the attribute numbers in use.
     */

    atr_push();
    DO_WHOLE_DB( i ) {
        for( ca = atr_head( i, &as ); ca; ca = atr_next( &as ) ) {
            used_table[ca] = ca;
        }
    }
    atr_pop();

    /*
     * Walk the vattr table. If a number isn't in use, zorch it.
     */

    vp = vattr_first();
    while( vp ) {
        vpx = vp;
        vp = vattr_next( vp );
        if( used_table[vpx->number] == 0 ) {
            anum_set( vpx->number, NULL );
            hashdelete( vpx->name, &mudstate.vattr_name_htab );
            XFREE( vpx, "dbclean.vpx" );
            n_deleted++;
        }
    }

    /*
     * The user-defined function, added command, and hook structures embed
     * * attribute numbers. Clean out the ones we've deleted, resetting them
     * * to the *Invalid (A_TEMP) attr.
     */

    for( ufp = ( UFUN * ) hash_firstentry( &mudstate.ufunc_htab );
            ufp != NULL; ufp = ( UFUN * ) hash_nextentry( &mudstate.ufunc_htab ) ) {
        if( used_table[ufp->atr] == 0 ) {
            ufp->atr = A_TEMP;
        }
    }
    for( cmdp = ( CMDENT * ) hash_firstentry( &mudstate.command_htab );
            cmdp != NULL;
            cmdp = ( CMDENT * ) hash_nextentry( &mudstate.command_htab ) ) {
        if( cmdp->pre_hook ) {
            if( used_table[cmdp->pre_hook->atr] == 0 ) {
                cmdp->pre_hook->atr = A_TEMP;
            }
        }
        if( cmdp->post_hook ) {
            if( used_table[cmdp->post_hook->atr] == 0 ) {
                cmdp->post_hook->atr = A_TEMP;
            }
        }
        if( cmdp->userperms ) {
            if( used_table[cmdp->userperms->atr] == 0 ) {
                cmdp->userperms->atr = A_TEMP;
            }
        }
        if( cmdp->callseq & CS_ADDED ) {
            for( addp = ( ADDENT * ) cmdp->info.added;
                    addp != NULL; addp = addp->next ) {
                if( used_table[addp->atr] == 0 ) {
                    addp->atr = A_TEMP;
                }
            }
        }
    }

    /*
     * Walk the table we've created of used statuses. When we find free
     * * slots, walk backwards to the first used slot at the end of the
     * * table. Write the number of the free slot into that used slot.
     */

    for( i = A_USER_START, end = mudstate.attr_next - 1;
            ( i < mudstate.attr_next ) && ( i < end ); i++ ) {
        if( used_table[i] == 0 ) {
            while( ( end > i ) && ( used_table[end] == 0 ) ) {
                end--;
            }
            if( end > i ) {
                used_table[end] = used_table[i] = i;
                end--;
            }
        }
    }

    /*
     * Renumber the necessary attributes in the vattr tables.
     */

    for( i = A_USER_START; i < mudstate.attr_next; i++ ) {
        if( used_table[i] != i ) {
            vp = ( VATTR * ) anum_get( i );
            if( vp ) {
                vp->number = used_table[i];
                vp->flags |= AF_DIRTY;
                anum_set( used_table[i], ( ATTR * ) vp );
                anum_set( i, NULL );
                n_renumbered++;
            }
        }
    }

    /*
     * Now we walk the database. For every object, if we have an attribute
     * * we're renumbering (the slot number is not equal to the array value
     * * at that slot), we delete the old attribute and add the new one.
     */

    atr_push();
    DO_WHOLE_DB( i ) {
        got = 0;
        for( ca = atr_head( i, &as ); ca; ca = atr_next( &as ) ) {
            if( used_table[ca] != ca ) {
                str = atr_get_raw( i, ca );
                atr_add_raw( i, used_table[ca], str );
                atr_clr( i, ca );
                n_atrt++;
                got = 1;
            }
        }
        if( got ) {
            n_objt++;
        }
    }
    atr_pop();

    /*
     * The new end of the attribute table is the first thing we've
     * * renumbered.
     */

    for( end = A_USER_START;
            ( ( end == used_table[end] ) && ( end < mudstate.attr_next ) ); end++ );
    mudstate.attr_next = end;

    /*
     * We might be able to shrink the size of the attribute table.
     * * If the current size of the table is less than the initial
     * * size, shrink it back down to the initial size.
     * * Otherwise, shrink it down so it's the current top plus the
     * * initial size, as if we'd just called anum_extend() for it.
     */

    if( anum_alc_top > mudconf.init_size + A_USER_START ) {
        if( mudstate.attr_next < mudconf.init_size + A_USER_START ) {
            end = mudconf.init_size + A_USER_START;
        } else {
            end = mudstate.attr_next + mudconf.init_size;
        }
        if( end < anum_alc_top ) {
            new_table = ( ATTR ** ) XCALLOC( end + 1, sizeof( ATTR * ),
                                             "dbclean.new_table" );
            for( i = 0; i < mudstate.attr_next; i++ ) {
                new_table[i] = anum_table[i];
            }
            XFREE( anum_table, "dbclean.anum_table" );
            anum_table = new_table;
            anum_alc_top = end;
        }
    }

    /*
     * Go through the function and added command tables again, and
     * * take care of the attributes that got renumbered.
     */

    for( ufp = ( UFUN * ) hash_firstentry( &mudstate.ufunc_htab );
            ufp != NULL; ufp = ( UFUN * ) hash_nextentry( &mudstate.ufunc_htab ) ) {
        if( used_table[ufp->atr] != ufp->atr ) {
            ufp->atr = used_table[ufp->atr];
        }
    }
    for( cmdp = ( CMDENT * ) hash_firstentry( &mudstate.command_htab );
            cmdp != NULL;
            cmdp = ( CMDENT * ) hash_nextentry( &mudstate.command_htab ) ) {
        if( cmdp->pre_hook ) {
            if( used_table[cmdp->pre_hook->atr] !=
                    cmdp->pre_hook->atr )
                cmdp->pre_hook->atr =
                    used_table[cmdp->pre_hook->atr];
        }
        if( cmdp->post_hook ) {
            if( used_table[cmdp->post_hook->atr] !=
                    cmdp->post_hook->atr )
                cmdp->post_hook->atr =
                    used_table[cmdp->post_hook->atr];
        }
        if( cmdp->userperms ) {
            if( used_table[cmdp->userperms->atr] !=
                    cmdp->userperms->atr )
                cmdp->userperms->atr =
                    used_table[cmdp->userperms->atr];
        }
        if( cmdp->callseq & CS_ADDED ) {
            for( addp = ( ADDENT * ) cmdp->info.added;
                    addp != NULL; addp = addp->next ) {
                if( used_table[addp->atr] != addp->atr ) {
                    addp->atr = used_table[addp->atr];
                }
            }
        }
    }

    /*
     * Clean up.
     */

    XFREE( used_table, "dbclean.used_table" );

    if( anum_alc_top != n_oldtop ) {
        notify_check( player, player, MSG_PUP_ALWAYS|MSG_ME_ALL|MSG_F_DOWN, "Cleaned %d user attribute slots (reduced to %d): %d deleted, %d renumbered (%d objects and %d individual attrs touched). Table size reduced from %d to %d.", n_oldtotal - A_USER_START, mudstate.attr_next - A_USER_START, n_deleted, n_renumbered, n_objt, n_atrt, n_oldtop, anum_alc_top );
    } else {
        notify_check( player, player, MSG_PUP_ALWAYS|MSG_ME_ALL|MSG_F_DOWN, "Cleaned %d attributes (now %d): %d deleted, %d renumbered (%d objects and %d individual attrs touched).", n_oldtotal, mudstate.attr_next, n_deleted, n_renumbered, n_objt, n_atrt );
        
    }

    raw_broadcast( 0, "GAME: Database cleaning complete." );

}
Example #14
0
static void
_check_cache(sdns_handle_t *sdns)
{
	struct stat sb;
	int i, n, status;
	DIR *dp;
	struct direct *d;
	pdns_handle_t *c;
	struct timeval now;

	if (sdns == NULL) return;

	if ((sdns->stattime != 0) && (resolv1_token != -1) && (resolv2_token != -1))
	{
		n = 1;
		status = notify_check(resolv1_token, &n);
		if ((status == NOTIFY_STATUS_OK) && (n == 0))
		{
			n = 1;
			status = notify_check(resolv2_token, &n);
			if ((status == NOTIFY_STATUS_OK) && (n == 0)) return;
		}
	}

	gettimeofday(&now, NULL);

	if (sdns->dns_default != NULL)
	{
		/* Check default (/etc/resolv.conf) client */
		memset(&sb, 0, sizeof(struct stat));
		status = stat(_PATH_RESCONF, &sb);
		if ((status != 0) || (sb.st_mtimespec.tv_sec > sdns->dns_default->modtime))
		{
			_pdns_free(sdns->dns_default);
			sdns->dns_default = NULL;
		}
		else if (status == 0)
		{
			sdns->dns_default->stattime = now.tv_sec;
		}
	}
	
	if (sdns->dns_default == NULL)
	{
		sdns->dns_default = _pdns_open(NULL);
		if ((sdns->dns_default != NULL) && (sdns->flags & DNS_FLAG_DEBUG)) sdns->dns_default->res->options |= RES_DEBUG;
	}
	
	/* Check /etc/resolver clients */
	memset(&sb, 0, sizeof(struct stat));
	status = stat(DNS_RESOLVER_DIR, &sb);
	if ((status != 0) || (sb.st_mtimespec.tv_sec > sdns->modtime))
	{
		for (i = 0; i < sdns->client_count; i++)
		{
			_pdns_free(sdns->client[i]);
		}

		sdns->client_count = 0;
		if (sdns->client != NULL) free(sdns->client);
		sdns->client = NULL;
	}

	if ((status == 0) && (sb.st_mtimespec.tv_sec > sdns->modtime))
	{
		dp = opendir(DNS_RESOLVER_DIR);
		if (dp != NULL)
		{
			while (NULL != (d = readdir(dp)))
			{
				if (d->d_name[0] == '.') continue;

				c = _pdns_open(d->d_name);
				if (c == NULL) continue;
				if (sdns->flags & DNS_FLAG_DEBUG) c->res->options |= RES_DEBUG;

				if (sdns->client_count == 0)
				{
					sdns->client = (pdns_handle_t **)malloc(sizeof(pdns_handle_t *));
				}
				else
				{
					sdns->client = (pdns_handle_t **)realloc(sdns->client, (sdns->client_count + 1) * sizeof(pdns_handle_t *));
				}
			
				sdns->client[sdns->client_count] = c;
				sdns->client_count++;
			}
			closedir(dp);
		}
			
		sdns->modtime = sb.st_mtimespec.tv_sec;
		sdns->stattime = now.tv_sec;
	}
	else if (status == 0)
	{
		sdns->stattime = now.tv_sec;
	}
}
Example #15
0
int convert_flags( dbref player, char *flaglist, FLAGSET *fset, FLAG *p_type ) {
    int i, handled;

    char *s;

    FLAG flag1mask, flag2mask, flag3mask, type;

    FLAGENT *fp;

    flag1mask = flag2mask = flag3mask = 0;
    type = NOTYPE;

    for( s = flaglist; *s; s++ ) {
        handled = 0;

        /*
         * Check for object type
         */

        for( i = 0; ( i <= 7 ) && !handled; i++ ) {
            if( ( object_types[i].lett == *s ) &&
                    !( ( ( object_types[i].perm & CA_WIZARD ) &&
                         !Wizard( player ) ) ||
                       ( ( object_types[i].perm & CA_GOD ) &&
                         !God( player ) ) ) ) {
                if( ( type != NOTYPE ) && ( type != i ) ) {
                    notify_check( player, player, MSG_PUP_ALWAYS|MSG_ME_ALL|MSG_F_DOWN, "%c: Conflicting type specifications.", *s );
                    return 0;
                }
                type = i;
                handled = 1;
            }
        }

        /*
         * Check generic flags
         */

        if( handled ) {
            continue;
        }
        for( fp = gen_flags; ( fp->flagname ) && !handled; fp++ ) {
            if( ( fp->flaglett == *s ) &&
                    !( ( ( fp->listperm & CA_WIZARD ) &&
                         !Wizard( player ) ) ||
                       ( ( fp->listperm & CA_GOD ) && !God( player ) ) ) ) {
                if( fp->flagflag & FLAG_WORD3 ) {
                    flag3mask |= fp->flagvalue;
                } else if( fp->flagflag & FLAG_WORD2 ) {
                    flag2mask |= fp->flagvalue;
                } else {
                    flag1mask |= fp->flagvalue;
                }
                handled = 1;
            }
        }

        if( !handled ) {
            notify_check( player, player, MSG_PUP_ALWAYS|MSG_ME_ALL|MSG_F_DOWN, "%c: Flag unknown or not valid for specified object type", *s );
            return 0;
        }
    }

    /*
     * return flags to search for and type
     */

    ( *fset ).word1 = flag1mask;
    ( *fset ).word2 = flag2mask;
    ( *fset ).word3 = flag3mask;
    *p_type = type;
    return 1;
}
Example #16
0
int sql_query ( dbref player, char *q_string, char *buff, char **bufc, const Delim *row_delim, const Delim *field_delim )
{
    PGresult *pgres;
    ExecStatusType pgstat;
    PGconn *pgsql;
    char *pg_data;
    int num_rows, got_rows, got_fields;
    int i, j;
    int retries;
    /*
     * If we have no connection, and we don't have auto-reconnect on (or
     * we try to auto-reconnect and we fail), this is an error generating
     * a #-1. Notify the player, too, and set the return code.
     */
    pgsql = pgsql_struct;

    if ( ( !pgsql ) && ( mod_db_sql_config.reconnect != 0 ) ) {
        /*
         * Try to reconnect.
         */
        retries = 0;

        while ( ( retries < PGSQL_RETRY_TIMES ) && !pgsql ) {
            sleep ( 1 );
            sql_init ( 0, 0, NULL, NULL );
            pgsql = pgsql_struct;
            retries++;
        }
    }

    if ( !pgsql ) {
        notify ( player, "No SQL database connection." );

        if ( buff )
            safe_str ( "#-1", buff, bufc );

        return -1;
    }

    if ( !q_string || !*q_string )
        return 0;

    /*
     * Send the query.
     */
    pgres = PQexec ( pgsql, q_string );
    pgstat = PQresultStatus ( pgres );

    if ( pgstat != PGRES_COMMAND_OK && pgstat != PGRES_TUPLES_OK ) {
        notify ( player, PQresultErrorMessage ( pgres ) );

        if ( buff )
            safe_str ( "#-1", buff, bufc );

        PQclear ( pgres );
        return -1;
    }

    /*
     * A number of affected rows greater than 0 means it wasn't a SELECT
     */
    num_rows = atoi ( PQcmdTuples ( pgres ) );

    if ( num_rows > 0 ) {
        notify_check ( player, player, MSG_PUP_ALWAYS | MSG_ME_ALL | MSG_F_DOWN, "SQL query touched %d %s.", num_rows, ( num_rows == 1 ) ? "row" : "rows" );
        PQclear ( pgres );
        return 0;
    }

    /*
     * Check to make sure we got rows back.
     */
    got_rows = PQntuples ( pgres );

    if ( got_rows == 0 ) {
        PQclear ( pgres );
        return 0;
    }

    /*
     * Construct properly-delimited data.
     */

    if ( buff ) {
        for ( i = 0; i < got_rows; i++ ) {
            if ( i > 0 ) {
                print_sep ( row_delim, buff, bufc );
            }

            got_fields = PQnfields ( pgres );

            if ( got_fields ) {
                for ( j = 0; j < got_fields; j++ ) {
                    pg_data = PQgetvalue ( pgres, i, j );

                    if ( j > 0 ) {
                        print_sep ( field_delim, buff,
                                    bufc );
                    }

                    if ( pg_data && *pg_data )
                        safe_str ( pg_data, buff, bufc );
                }
            }
        }
    } else {
        for ( i = 0; i < got_rows; i++ ) {
            got_fields = PQnfields ( pgres );

            if ( got_fields ) {
                for ( j = 0; j < got_fields; j++ ) {
                    pg_data = PQgetvalue ( pgres, i, j );

                    if ( pg_data && *pg_data ) {
                        notify_check ( player, player, MSG_PUP_ALWAYS | MSG_ME_ALL | MSG_F_DOWN, "Row %d, Field %d: %s", i + 1, j + 1, pg_data );
                    } else {
                        notify_check ( player, player, MSG_PUP_ALWAYS | MSG_ME_ALL | MSG_F_DOWN, "Row %d, Field %d: NULL", i + 1, j + 1 );
                    }
                }
            } else {
                notify_check ( player, player, MSG_PUP_ALWAYS | MSG_ME_ALL | MSG_F_DOWN, "Row %d: NULL", i + 1 );
            }
        }
    }

    PQclear ( pgres );
    return 0;
}
Example #17
0
/*
 * GuizmOVPN_get_user_pass(char *username,char *password,const int capacity, char * prefix) :
 *      Request username/password from the user
 */
void GuizmOVPN_get_user_pass(char *username,char *password,const int capacity, char * prefix)
{
	int token, status, check;
	
	status = notify_register_check("com.guizmo.openvpn/ReceivedUserPass", &token);
	notify_check(token, &check);
	if (status != NOTIFY_STATUS_OK)
	{
		msg (M_FATAL, "Unable to receive authentification");
	}
	
	// Check which user/pass request to handle
	if(!strcmp(prefix,"token-insertion-request"))
	{
		notify_post("com.guizmo.openvpn/RequestTokenInsertionPass");
		
	} else if(!strcmp(prefix,"Auth")) {
		notify_post("com.guizmo.openvpn/RequestAuthUserPass");
		
	} else if(!strcmp(prefix,"HTTP Proxy")) {
		notify_post("com.guizmo.openvpn/RequestProxyUserPass");
		
	} else if(!strcmp(prefix,"pkcs11-id-request")) {
		notify_post("com.guizmo.openvpn/RequestPKCS11UserPass");
		
	} else if(!strcmp(prefix,"Private Key")) {
		notify_post("com.guizmo.openvpn/RequestPrivateKeyPass");
		
	} else if(!strstr(prefix," token")) {
		notify_post("com.guizmo.openvpn/RequestTokenPIN");
		
	} else {
		msg (M_FATAL, "Unknown user/pass request : %s",prefix);
		return;
	}
	
    msg (M_INFO, "Waiting for username/password (%s)",prefix);
	// May need to do a cleaner wait
	int received=0;
	while(!received)
	{
		status = notify_check(token, &check);
		if ((status == NOTIFY_STATUS_OK) && (check != 0))
		{
			msg (M_INFO,"Username/password received");
			received=1;
		}
		sleep(1);
	}
	
	// Read the username/password from file
	const char *path="/tmp/guizmovpn_temp_auth";
	FILE *fp = fopen (path, "r");
	if (!fp)
	{
		msg (M_FATAL, "Error receiving authentification");
	}
	
	if (fgets (username, capacity, fp) == NULL || fgets (password, capacity, fp) == NULL)
	{
		msg (M_FATAL, "Error receiving authentification");
	}
	
	fclose (fp);
	unlink(path);
	
	chomp (username);
	chomp (password);
    
	return;
}
Example #18
0
int sql_query(dbref player, char *q_string, char *buff, char **bufc, const Delim *row_delim, const Delim *field_delim) {
    sqlite3 *sqlite;

    const unsigned char *col_data;

    int num_rows, got_rows, got_fields;

    int i, j;

    int retries;

    int retval;

    sqlite3_stmt *stmt;

    const char *rest;

    /*
     * If we have no connection, and we don't have auto-reconnect on (or
     * we try to auto-reconnect and we fail), this is an error generating
     * a #-1. Notify the player, too, and set the return code.
     */

    sqlite = sqlite3_struct;
    if ((!sqlite) && (mod_db_sql_config.reconnect != 0))
    {
        /*
         * Try to reconnect.
         */
        retries = 0;
        while ((retries < SQLITE_RETRY_TIMES) && !sqlite)
        {
            sleep(1);
            sql_init(0,0,NULL,NULL);
            sqlite = sqlite3_struct;
            retries++;
        }
    }
    if (!sqlite)
    {
        notify_quiet(player, "No SQL database connection.");
        if (buff)
            safe_str("#-1", buff, bufc);
        return -1;
    }
    if (!q_string || !*q_string)
        return 0;

    /*
     * Prepare the query.
     */
    retval = sqlite3_prepare_v2(sqlite, q_string, -1, &stmt, &rest);
    if (retval != SQLITE_OK)
    {
        notify_quiet(player, sqlite3_errmsg(sqlite));
        if (buff)
            safe_str("#-1", buff, bufc);
        sqlite3_finalize(stmt);
        return -1;
    }
    /*
     * Construct properly-delimited data.
     */
    if (buff)
    {
        i = 0;
        while (sqlite3_step(stmt) == SQLITE_ROW)
        {
            if (i++ > 0)
            {
                print_sep(row_delim, buff, bufc);
            }
            got_fields = sqlite3_column_count(stmt);
            if (got_fields)
            {
                for (j = 0; j < got_fields; j++)
                {
                    col_data =
                        sqlite3_column_text(stmt, j);
                    if (j > 0)
                    {
                        print_sep(field_delim, buff,
                                  bufc);
                    }
                    if (col_data && *col_data)
                        safe_str((char *)col_data,
                                 buff, bufc);
                }
            }
        }
    }
    else
    {
        i = 0;
        while (sqlite3_step(stmt) == SQLITE_ROW)
        {
            if (i++ > 0)
            {
                print_sep(row_delim, buff, bufc);
            }
            got_fields = sqlite3_column_count(stmt);
            if (got_fields)
            {
                for (j = 0; j < got_fields; j++)
                {
                    col_data =
                        sqlite3_column_text(stmt, j);
                    if (j > 0)
                    {
                        notify_check(player, player, MSG_PUP_ALWAYS|MSG_ME_ALL|MSG_F_DOWN, "Row %d, Field %d: %s", i, j + 1, col_data);
                    }
                    if (col_data && *col_data)
                    {
                        notify_check(player, player, MSG_PUP_ALWAYS|MSG_ME_ALL|MSG_F_DOWN, "Row %d, Field %d: NULL", i, j + 1);
                    }
                }
            }
        }
    }

    if (i == 0)
    {
        num_rows = sqlite3_changes(sqlite);
        if (num_rows > 0)
        {
            notify_check(player, player, MSG_PUP_ALWAYS|MSG_ME_ALL|MSG_F_DOWN, "SQL query touched %d %s.", num_rows, (num_rows == 1) ? "row" : "rows");
        }
    }
    sqlite3_finalize(stmt);
    return 0;
}